在int main,未初始化的内存,未初始化的局部变量中使用函数的问题

小弟弟

我遇到了一个问题,我正在学习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函数中设置的值。

我也尝试过在函数本身中使用返回值,但是也没有成功。

我想念什么?我尝试通过互联网查找任何类型的解决方案,但到目前为止都没有帮助。

dbush

在中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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

未初始化的最终局部变量VS未初始化的最后实例变量

局部变量初始化null和未初始化之间的区别?

未初始化的int与Integer

int数组初始化

未初始化变量的后果:int vs unsigned char

杰克逊序列化:忽略未初始化的int

未初始化的局部变量,c ++ 11默认

如何使用int数组[] []初始化向量<vector <int >>?

如何正确初始化const int const *变量?

C:未初始化的int类型大小必须大于

使用C ++的未初始化局部变量

发送单个未初始化的int到VBO

从MutableList <Int>初始化Array <Int>

零初始化,未命名,临时未签名的int

未初始化的局部变量c4700

尝试使i = i * i时,为什么出现错误“使用未初始化的内存'i'”和“使用了未初始化的局部变量'i'”的错误

int变量初始化功能失败

在switch语句中初始化变量(int32)

局部变量保持未初始化

使用memset初始化int数组

函数中的局部变量未初始化并记住以前的函数调用

使用了未初始化的局部变量“xAxis”

使用了未初始化的局部变量“lc”,但我已将其初始化

C++ 未初始化的局部(非全局)int 数组中的元素类型究竟是什么?

如果未初始化“私有静态 int”实例变量,它是否等于零?

使用了未初始化的局部变量“totalPrice”

使用结构内部的 int 变量初始化静态数组

为什么`main`函数内的局部int变量会被自动初始化?

C++ 中未初始化的局部变量