2

我已经制作了这个销售点程序,其中硬编码了三个项目,变量a_book,a_bata_hat在我输入数量时不会更新。为什么?

#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <cstdlib>

using namespace std;

int main(void)
{


    int selectionMain;

    int selection;

    int a_book;
    int a_bat;
    int a_hat;






    cout << "\n\nHello, welcome to mIke's Convenience! How may I help you?" << endl;

    printf("\n\n\n");

    cout << "1. add products\n\n" << endl;
    cout << "2. remove products\n\n" << endl;
    cout << "3. procede to receipt\n\n\n" << endl;
    cout << "4. exit\n\n\n" << endl;


    printf("Please make a selection: ");
    scanf(" %d", &selectionMain);

    printf("\n\n\n");


    if(selectionMain == 1){



            cout << "You have selected 1.\n\n\n" << endl;

            cout << "1. book  -  $5.00 ea\n\n" << endl;
            cout << "2. baseball bat  -  $11.00 ea\n\n" << endl;
            cout << "3. hat  -  $7.00 ea\n\n\n" << endl;


            printf("please select a product to add by typing a number (1, 2, 3): ");
            scanf(" %d", &selection);

            if(selectionMain == 1){


                    printf("select desired quantity of books to add: ");
                    scanf(" %d", &a_book);

                }else if(selectionMain == 2){

                    printf("select desired quantity of baseball bats to add: ");
                    scanf(" %d", &a_bat);

                }else{

                    printf("select desired quantity of hats to add: ");
                    scanf(" %d", &a_hat);

        }



            return main();





        }else if(selectionMain == 2){

            cout << "You have selected 2.\n\n\n" << endl;

            cout << "1. book  -  $5.00 ea\n\n" << endl;
            cout << "2. baseball bat  -  $11.00 ea\n\n" << endl;
            cout << "3. hat  -  $7.00 ea\n\n\n" << endl;


            printf("please select a product to remove by typing a number (1, 2, 3): ");
            scanf(" %d", &selectionMain);

            return main();





        }else if(selectionMain == 3){

            cout << "You have selected 3.\n\n\n" << endl;
            cout << "-------your receipt-------\n\n\n" << endl;


            printf("book(s) x %d!\n", a_book);


            printf("baseball bat(s) x %d!\n", a_bat);


            printf("hat(s) x %d!\n\n", a_hat);



            cout << "-----Thank you for shopping at mIke's Convenience. Please play responsibly?-----\n\n\n" << endl;

            printf("press any letter key and then 'Enter' to return to main screen ");
            scanf(" %d", &selectionMain);

            return main();





        }else{
            void exit();
        }




    return 0;
}
4

3 回答 3

0

我希望这可能会有所帮助。

while(true)  // enclose your if-else statements with in a loop
{
    // ...

    if(selectionMain == 1)
    {
        //...
    }
    else if(selectionMain == 2)
    {
        // ...
        //return main();        // remove this line
    }
    else if(selectionMain == 3)
    {
        // ...
        scanf(" %d", &selectionMain);
        //return main();    // remove this line, so that the variables will not be re-setted
    }
    else
        break;      // break out of the loop.
}
return 0;
于 2018-04-27T08:02:52.107 回答
0

对于变量,只有在selectionMain = 1.

如果 . 则打印selectionMain = 3

但是,您总是main()一遍又一遍地调用,return main()这将定义另一组:

int a_book;
int a_bat;
int a_hat;

这是未初始化的。

因此,先前输入的变量值在本地范围内“丢失” 。

于 2018-04-27T04:04:47.640 回答
0

主要原因是您在每个选项之后都对 main 进行了递归调用。每次进行递归调用时,每个调用都会有自己的三个变量的值集。您可以通过添加一个 while 循环并运行它直到用户输入 4 来解决这个问题。

#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <cstdlib>
using namespace std;

int main(void) {

int selectionMain;

int selection;

int a_book=0;
int a_bat=0;
int a_hat=0;

cout << "\n\nHello, welcome to mIke's Convenience! How may I help you?" << endl;
while(1)
{
cout << "1. add products\n\n" << endl;
cout << "2. remove products\n\n" << endl;
cout << "3. procede to receipt\n\n" << endl;
cout << "4. exit\n\n" << endl;
cout<<"Please make a selection: ";

cin>>selectionMain;


if(selectionMain == 1){



        cout << "You have selected 1.\n\n\n" << endl;

        cout << "1. book  -  $5.00 ea\n\n" << endl;
        cout << "2. baseball bat  -  $11.00 ea\n\n" << endl;
        cout << "3. hat  -  $7.00 ea\n\n\n" << endl;


        cout<<"please select a product to add by typing a number (1, 2, 3): ";
        cin>>selection;

    if(selection == 1){
                cout<<"select desired quantity of books to add: ";
                int temp;
                cin>>temp;
                a_book+=temp;

            }else if(selection == 2){

                cout<<"select desired quantity of baseball bat to add: ";
                int temp;
                cin>>temp;
                a_bat+=temp;

            }else{

                cout<<"select desired quantity of hats to add: ";
                int temp;
                cin>>temp;
                a_hat+=temp;
    }

    }else if(selectionMain == 2){

        cout << "You have selected 2.\n\n\n" << endl;

        cout << "1. book  -  $5.00 ea\n\n" << endl;
        cout << "2. baseball bat  -  $11.00 ea\n\n" << endl;
        cout << "3. hat  -  $7.00 ea\n\n\n" << endl;

        cin>>selection;
        //Same logic as addition
        continue;

    }else if(selectionMain == 3){

        cout << "You have selected 3.\n\n\n" << endl;
        cout << "-------your receipt-------\n\n\n" << endl;


        printf("book(s) x %d!\n", a_book);


        printf("baseball bat(s) x %d!\n", a_bat);


        printf("hat(s) x %d!\n\n", a_hat);



        cout << "-----Thank you for shopping at mIke's Convenience. Please play responsibly?-----\n\n\n" << endl;

        printf("press any letter key and then 'Enter' to return to main screen ");
        scanf(" %d", &selectionMain);
        continue;
    }
    else
        break;

}
return 0;
}

编辑 :

此外,如果您真的想更新,则不应覆盖相同的变量。它应该是

int temp;
scanf("%d",temp);
a_book+=temp;

删除也是如此。您必须为所有三个变量执行此操作。

编辑 2:你为什么同时使用 cout 和 printf?它会产生一个错误。要么只使用 cout 和 cin,要么只使用 printf 和 scanf。

于 2018-04-27T02:21:17.333 回答