如果用户选择“是”,则执行while循环以重新启动程序,但操作不正确 我学习C++,我们还没有在C++中覆盖循环,但我觉得有必要进入新的领域,所以我从头开始做了一个程序,有点像杂货店,在程序的最后,它询问用户是否想多购物,如果他/她键入Y,那么它重新启动,如果他/她键入N,那么它退出。当我在节目结束时输入Y时,它会一直问“你想继续购物吗?是[Y]还是否[N]”。怎么了 #include <iostream> #include <sstream> #include <string> using namespace std; char shoppingMenu() { char chrSelect = ' '; cout << "%%%%%%%%%%%%" << endl; cout << "% Shopping %" << endl; cout << "%%%%%%%%%%%%" << endl << endl; cout << "Prepared Foods:" << endl; cout << "A.) Lettuce" << endl; cout << "B.) Noodles" << endl; cout << "C.) Bottled Water" << endl; //shopSelection = cin.get(); cin >> chrSelect; return toupper(chrSelect); } double lettuce(int numWeight, double numQuant) { system("CLS"); cout << "%%%%%%%%%%%" << endl; cout << "% Lettuce %" << endl; cout << "%%%%%%%%%%%" << endl; cout << "Lbs: "; cin >> numWeight; cout << endl << "Quantity: "; cin >> numQuant; return numQuant * (numWeight * .75); } double noodles(double numQuant) { system("CLS"); cout << "%%%%%%%%%%%" << endl; cout << "% Noodles %" << endl; cout << "%%%%%%%%%%%" << endl << endl; cout << endl << "Number of 1/2lb packs: "; cin >> numQuant; return numQuant * 1.50; } double water(char chrSelection, int numQuant) { system("CLS"); cout << "%%%%%%%%%" << endl; cout << "% Water %" << endl; cout << "%%%%%%%%%" << endl << endl; cout << endl << "A.) 12pk;"; cout << endl << "B.) 24pk;" << endl; cout << "Please enter [A] or [B]" << endl; cin >> chrSelection; //cout << '\b' << endl; chrSelection = toupper(chrSelection); cout << "Quantity: "; cin >> numQuant; if (chrSelection == 'A') { return numQuant * 1.49; } else if (chrSelection == 'B') { return numQuant * 2.49; } } int main() { char selection = ' '; int numberLettuce = 0; double lettuceWeight = 0.0; int lettuceQuant = 0; double lettuceTotal = 0.0; int noodleQuant = 0; double noodleTotal = 0.0; char waterSelection = ' '; int waterQuant = 0; double waterTotal = 0.0; char keepShopping = ' '; cout << "#################" << endl; cout << "# Grocery Store #" << endl; cout << "#################" << endl << endl; do { cout << "How may i help you?" << endl; cout << "A.) Shopping Program" << endl; selection = cin.get(); selection = toupper(selection); system("CLS"); if (selection == 'A') { char shopSelection = shoppingMenu(); // Display Menu cout << "(Selection == " << selection << ")" << endl; if (shopSelection == 'A') { lettuceTotal = lettuce(lettuceWeight, lettuceQuant); } else if (shopSelection == 'B') { noodleTotal = noodles(noodleQuant); } else if (shopSelection == 'C') { waterTotal = water(waterSelection, waterQuant); } system("CLS"); cout.precision(2); cout << fixed; cout << " Lettuce total: $" << lettuceTotal << endl; cout << " Noodle total: $" << noodleTotal << endl; cout << " + Water total: $" << waterTotal << endl; cout << "----------------------------" << endl; cout << " = $" << (lettuceTotal + noodleTotal + waterTotal); } cout << endl << endl << "Do you want to continue shopping? Yes[Y] or No[N]" << endl; cin >> keepShopping; keepShopping = toupper(keepShopping); system("CLS"); } while (keepShopping == 'Y'); system("pause"); } #包括 #包括 #包括 使用名称空间std; char shoppingMenu() { char chrSelect=''; 问题是 cin >> keepShopping;

如果用户选择“是”,则执行while循环以重新启动程序,但操作不正确 我学习C++,我们还没有在C++中覆盖循环,但我觉得有必要进入新的领域,所以我从头开始做了一个程序,有点像杂货店,在程序的最后,它询问用户是否想多购物,如果他/她键入Y,那么它重新启动,如果他/她键入N,那么它退出。当我在节目结束时输入Y时,它会一直问“你想继续购物吗?是[Y]还是否[N]”。怎么了 #include <iostream> #include <sstream> #include <string> using namespace std; char shoppingMenu() { char chrSelect = ' '; cout << "%%%%%%%%%%%%" << endl; cout << "% Shopping %" << endl; cout << "%%%%%%%%%%%%" << endl << endl; cout << "Prepared Foods:" << endl; cout << "A.) Lettuce" << endl; cout << "B.) Noodles" << endl; cout << "C.) Bottled Water" << endl; //shopSelection = cin.get(); cin >> chrSelect; return toupper(chrSelect); } double lettuce(int numWeight, double numQuant) { system("CLS"); cout << "%%%%%%%%%%%" << endl; cout << "% Lettuce %" << endl; cout << "%%%%%%%%%%%" << endl; cout << "Lbs: "; cin >> numWeight; cout << endl << "Quantity: "; cin >> numQuant; return numQuant * (numWeight * .75); } double noodles(double numQuant) { system("CLS"); cout << "%%%%%%%%%%%" << endl; cout << "% Noodles %" << endl; cout << "%%%%%%%%%%%" << endl << endl; cout << endl << "Number of 1/2lb packs: "; cin >> numQuant; return numQuant * 1.50; } double water(char chrSelection, int numQuant) { system("CLS"); cout << "%%%%%%%%%" << endl; cout << "% Water %" << endl; cout << "%%%%%%%%%" << endl << endl; cout << endl << "A.) 12pk;"; cout << endl << "B.) 24pk;" << endl; cout << "Please enter [A] or [B]" << endl; cin >> chrSelection; //cout << '\b' << endl; chrSelection = toupper(chrSelection); cout << "Quantity: "; cin >> numQuant; if (chrSelection == 'A') { return numQuant * 1.49; } else if (chrSelection == 'B') { return numQuant * 2.49; } } int main() { char selection = ' '; int numberLettuce = 0; double lettuceWeight = 0.0; int lettuceQuant = 0; double lettuceTotal = 0.0; int noodleQuant = 0; double noodleTotal = 0.0; char waterSelection = ' '; int waterQuant = 0; double waterTotal = 0.0; char keepShopping = ' '; cout << "#################" << endl; cout << "# Grocery Store #" << endl; cout << "#################" << endl << endl; do { cout << "How may i help you?" << endl; cout << "A.) Shopping Program" << endl; selection = cin.get(); selection = toupper(selection); system("CLS"); if (selection == 'A') { char shopSelection = shoppingMenu(); // Display Menu cout << "(Selection == " << selection << ")" << endl; if (shopSelection == 'A') { lettuceTotal = lettuce(lettuceWeight, lettuceQuant); } else if (shopSelection == 'B') { noodleTotal = noodles(noodleQuant); } else if (shopSelection == 'C') { waterTotal = water(waterSelection, waterQuant); } system("CLS"); cout.precision(2); cout << fixed; cout << " Lettuce total: $" << lettuceTotal << endl; cout << " Noodle total: $" << noodleTotal << endl; cout << " + Water total: $" << waterTotal << endl; cout << "----------------------------" << endl; cout << " = $" << (lettuceTotal + noodleTotal + waterTotal); } cout << endl << endl << "Do you want to continue shopping? Yes[Y] or No[N]" << endl; cin >> keepShopping; keepShopping = toupper(keepShopping); system("CLS"); } while (keepShopping == 'Y'); system("pause"); } #包括 #包括 #包括 使用名称空间std; char shoppingMenu() { char chrSelect=''; 问题是 cin >> keepShopping;,c++,C++,其中,keepShopping是一个char。当您键入YEnter时,您已经键入了两个字符。第一个字符进入keepShopping,您的程序正确地循环回到do循环的顶部。然后,selection=cin.get()读取输入缓冲区中的下一个字符,即回车键,它不是A,因此它会下降到if块的末尾,询问您是否要再次继续购物 解决方案是避免使用cin,而是使用getline() string str; getline(cin, str); keepShopping = str[0]; 这将消耗您键入的所

其中,
keepShopping
是一个
char
。当您键入YEnter时,您已经键入了两个字符。第一个字符进入
keepShopping
,您的程序正确地循环回到
do
循环的顶部。然后,
selection=cin.get()
读取输入缓冲区中的下一个字符,即回车键,它不是
A
,因此它会下降到
if
块的末尾,询问您是否要再次继续购物

解决方案是避免使用
cin
,而是使用
getline()

string str;
getline(cin, str);
keepShopping = str[0];
这将消耗您键入的所有内容,包括Enter键,并且您不会得到这些奇怪的输入缓冲效果。使用
cin>
进行交互式输入确实非常麻烦,如果您避免它,您将省去很多头痛。

问题是

cin >> keepShopping;
其中,
keepShopping
是一个
char
。当您键入YEnter时,您已经键入了两个字符。第一个字符进入
keepShopping
,您的程序正确地循环回到
do
循环的顶部。然后,
selection=cin.get()
读取输入缓冲区中的下一个字符,即回车键,它不是
A
,因此它会下降到
if
块的末尾,询问您是否要再次继续购物

解决方案是避免使用
cin
,而是使用
getline()

string str;
getline(cin, str);
keepShopping = str[0];

这将消耗您键入的所有内容,包括回车键,并且您不会得到这些奇怪的输入缓冲效果。使用
cin>
进行交互式输入确实非常麻烦,如果您避免使用它,您将省去很多头痛。

格雷格说的是正确的,但您也可以使用
getch();
但是如果需要包含c-headers,getch()将只读取键盘上的一个按键笔划,因此如果单击“y”,它将自动识别它,而无需按enter键

keepShopping = getch();

我不确定它位于哪个库中,但是像
这样的标准c库是以
#include
的形式包含的,然后您可以使用所有标准c函数,所以如果您选择此解决方案,只需试用它们。

Greg说的是正确的,但您也可以使用
getch();
但是如果需要包含c-headers,getch()将只读取键盘上的一个按键笔划,因此如果单击“y”,它将自动识别它,而无需按enter键

keepShopping = getch();

我不确定它在哪个库中,但是像
这样的标准c库是以
#include
的形式包含的,然后你可以使用所有的标准c函数,所以如果你选择这个解决方案,就试试它们。

有没有像getline这样的方法可以得到字符呢?keepShopping是一个字符。不,只需要得到一行输入,然后看看行的第一个字符(
str[0]
如上所述)。答对了。感谢您的提示回答!有没有像getline这样的方法来获取字符?keepShopping是一个字符。不,只需获取一行输入并查看行的第一个字符(
str[0]
如上所述).Bingo.谢谢你的及时回答!老实说,我不太明白你刚才说的话,但看起来你指的是getchar(),或者可能我错了,但是是getch()如果您单击它,将读取enter。哦,很抱歉,我混淆了
getch
getchar
。看起来
getch
是curses的一部分,这是一个完全不同的主题。正确,getchar()与getch()不同,需要在输入结束时输入,因此在getch()时用于读取字符数组当时只读取一次击键。老实说,我不太明白你刚才说的话,但看起来你指的是getchar(),或者可能我错了,但是getch()如果您单击它,将读取enter。哦,很抱歉,我混淆了
getch
getchar
。看起来
getch
是curses的一部分,这是一个完全不同的主题。正确,getchar()与getch()不同,需要在输入结束时输入,因此在getch()时用于读取字符数组此时只读取一次击键。