我制作了这个销售点程序,其中包含三个硬编码的项目,当我输入数量时,变量a_book
,a_bat
和a_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;
}
答案 0 :(得分: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。
答案 1 :(得分:0)
对于变量,仅在selectionMain = 1
时分配。
如果selectionMain = 3
,则打印。
但是,您始终通过main()
反复拨打return main()
,这将定义另一组:
int a_book;
int a_bat;
int a_hat;
未初始化。
因此,先前输入的变量的值是&#34;丢失&#34;在本地范围。
答案 2 :(得分: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;