【问题标题】:Variables Not Updating变量未更新
【发布时间】:2018-10-07 19:25:43
【问题描述】:

我在这个销售点程序中硬编码了三个项目,当我输入数量时变量a_booka_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;
}

【问题讨论】:

  • 调用main 是未定义的,所以不知道程序会做什么。使用循环。

标签: c++ logic pos


【解决方案1】:

主要原因是您在每个选项之后都对 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。

【讨论】:

    【解决方案2】:

    只有在selectionMain = 1 时才分配变量。

    如果是selectionMain = 3,则打印出来。

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

    int a_book;
    int a_bat;
    int a_hat;
    

    这是未初始化的

    因此,之前为提及的变量输入的值在局部范围中“丢失”了。

    【讨论】:

      【解决方案3】:

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

      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;
      

      【讨论】:

        猜你喜欢
        • 2019-09-05
        • 2016-07-03
        • 2021-02-20
        • 1970-01-01
        • 1970-01-01
        • 2021-09-07
        • 2013-04-14
        • 2014-03-14
        • 1970-01-01
        相关资源
        最近更新 更多