我遇到了一个问题,我正在学习C语言中函数的用法。我尝试使用函数将其实现到我的代码中,但是,在初始化内存和初始化局部变量的过程中,我总是遇到错误。
编码:
#include <stdio.h>
int getItemPrice(int applePrice, int orangePrice, int pearPrice);
int displayMenu(int applePrice, int orangePrice, int pearPrice);
int withinBudget(int budget, char purchase, int applePrice, int orangePrice, int pearPrice);
int purchaseItem(int budget, char purchase, int applePrice, int orangePrice, int pearPrice);
int main()
{
int orangePrice, applePrice, pearPrice, budget, calculation, items;
char input;
printf("*****************\n");
printf("Item prefixes\n");
printf("A: Apple\n");
printf("O: Orange\n");
printf("P: Pear\n");
printf("*****************\n\n");
printf("***************\n");
printf("*** MyStore ***\n");
printf("***************\n\n");
printf("**** SHOPKEEPER PANEL ****\n");
getItemPrice(applePrice, orangePrice, pearPrice);
displayMenu(applePrice, orangePrice, pearPrice);
printf("**** Customer menu ****\n");
printf("Please enter your budget: \x9C");
scanf_s("%d", &budget); // User is being prompted to enter budget for their purchases
printf("Please enter the item you would like to purchase using the item Prefix: ");
scanf_s(" %c", &input, 1); // User is being prompted to enter the item prefix to purchase items
printf("\n\n");
if (input == 'A') {
items = applePrice; // Will make input 'A' = to the price of apples.
}
else if (input == 'O') {
items = orangePrice; // Will make input 'O' = to the price of oranges.
}
else if (input == 'P') {
items = pearPrice; // Will make input 'P' = to the price of pear.
}
else {
items = -1; // Invalid ITEM!
}
if (items != -1) { // If the value of item is anything else than -1 the program will continue to code below.
calculation = budget - items;
if (calculation >= 0) { // Program will display purchase details if the value of calculation is equal or greater than 0.
printf("Purchase was a success!\n");
printf("Purchase details\n");
printf("----------------------\n");
printf("Item: %c\n", input);
printf("Price: \x9C%d\n", items);
printf("Remaining budget: \x9C%d\n\n", calculation);
printf("Thanks for shopping with us!");
}
else { // If the calculation will have a negative number it will make the purchase not possible.
items = -1;
}
}
if (items == -1) { // If user inputs wrong item prefix or budget will be too low for purchase the program will execute following purchase failed code.
printf("Purchase FAILED!\n");
printf("Low budget or invalid item!\n\n");
printf("Thanks for shopping with us!");
}
return 0;
};
int getItemPrice(int applePrice, int orangePrice, int pearPrice)
{
printf("Welcome to the store. Please enter the prices for the following products: \n");
printf("Please enter the price for the Orange: \x9C");
scanf_s("%d", &orangePrice); // Requests shopkeeper to enter price for for orange variable
printf("Please enter the price for the Apple: \x9C");
scanf_s("%d", &applePrice); // Reqeustes shopkeeper to enter price for apple variable
printf("Please enter the price for the Pear: \x9C");
scanf_s("%d", &pearPrice); // Requests shopkeeper to enter price for pear variable
printf("\n\n");
return 0;
}
int displayMenu(int applePrice, int orangePrice, int pearPrice) {
printf("**** Shop Menu ****\n");
printf("Item:\t\tPrice\n");
printf("A:\t\t\x9C%d\n", applePrice); // Displays price previously assigned by shopkeeper to the apple variable
printf("O:\t\t\x9C%d\n", orangePrice); // Displays price previously assigned by shopkeeper to the orange variable
printf("P:\t\t\x9C%d\n", pearPrice); // Displays price previously assigned by shopkeeper to the pear variable
printf("\n\n");
return 0;
}
到目前为止,似乎已经尝试解决此问题的方法是更改int main中的值以使用以下方法初始化它们:
orangePrice = 1;
applePrice = 1;
pearPrice = 1;
但是,当我尝试运行该程序时,这些功能将起作用,并且在尝试编译该程序时没有任何问题。但是displayMenu函数中的值显示所有价格均为£1,而不是我在getItemPrice函数中设置的值。
我也尝试过在函数本身中使用返回值,但是也没有成功。
我想念什么?我尝试通过互联网查找任何类型的解决方案,但到目前为止都没有帮助。
在中getItemPrice
,您正在将值读入局部变量。这些更改在函数外部不可见。您需要将函数的参数更改为指针:
int getItemPrice(int *applePrice, int *orangePrice, int *pearPrice)
{
printf("Welcome to the store. Please enter the prices for the following products: \n");
printf("Please enter the price for the Orange: \x9C");
scanf_s("%d", orangePrice); // Requests shopkeeper to enter price for for orange variable
printf("Please enter the price for the Apple: \x9C");
scanf_s("%d", applePrice); // Reqeustes shopkeeper to enter price for apple variable
printf("Please enter the price for the Pear: \x9C");
scanf_s("%d", pearPrice); // Requests shopkeeper to enter price for pear variable
printf("\n\n");
return 0;
}
并传入要更新的变量的地址:
getItemPrice(&applePrice, &orangePrice, &pearPrice);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句