gpt4 book ai didi

c++ - 变量不更新

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:41:13 26 4
gpt4 key购买 nike

我在这个销售点程序中硬编码了三个项目,变量 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 进行了递归调用。每次进行递归调用时,每个调用都会为三个变量拥有自己的一组值您可以通过添加一个 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。

关于c++ - 变量不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50054105/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com