C++ 如何停止while循环并转到其他语句?
我想制作一个程序,跟踪CD的销售数量。我想知道哪本书最畅销。CD被分为古典音乐、乡村音乐、舞蹈、流行音乐、摇滚音乐和R&B音乐 我制作了一个菜单驱动程序,可以显示音乐类型,但是 这是我的问题:我不知道用户如何才能继续输入出售的音乐类型的数量,直到他想停止为止。在他停下来之后,他如何报告每种类型的CD销量?基本上,用户将继续计算CD的销售数量,直到他想停止销售为止C++ 如何停止while循环并转到其他语句?,c++,while-loop,C++,While Loop,我想制作一个程序,跟踪CD的销售数量。我想知道哪本书最畅销。CD被分为古典音乐、乡村音乐、舞蹈、流行音乐、摇滚音乐和R&B音乐 我制作了一个菜单驱动程序,可以显示音乐类型,但是 这是我的问题:我不知道用户如何才能继续输入出售的音乐类型的数量,直到他想停止为止。在他停下来之后,他如何报告每种类型的CD销量?基本上,用户将继续计算CD的销售数量,直到他想停止销售为止 #include<iostream> using namespace std; int main () { in
#include<iostream>
using namespace std;
int main ()
{
int counter = 1;
int result, Alternative = 0, Classical = 0, Country = 0, Dance = 0, Pop = 0, Rock = 0, RnB = 0;
cout << "Choose the music genre sold (Only choose the numbers): \n1. Alternative\n";
cout << "2. Classical\n3. Country\n4. Dance\n5. Pop\n6. Rock\n7. R&B\n\t\t\t\t\t\t\t";
while (counter <= 100)
{
cin>>result;
if(result == 1)
{
Alternative = Alternative + 1;
}
else if(result == 2)
{
Classical = Classical + 1;
counter++;
}
else if(result == 3)
{
Country = Country + 1;
counter++;
}
else if(result == 4)
{
Dance = Dance + 1;
counter++;
}
else if(result == 5)
{
Pop = Pop + 1;
counter++;
}
else if (result == 6)
{
Rock = Rock + 1;
counter++;
}
else if (result == 7)
{
RnB = RnB + 1;
counter++;
}
cout<<"\nAlternative:"<<Alternative;
cout<<"\tClassical:"<<Classical;
cout<<"\tCountry: "<<Country<<"\n";
cout<<"Dance: "<<Dance;
cout<<"\tPop: "<<Pop;
cout<<"\tRock: "<<Rock;
cout<<"\tR&B: "<<RnB<<"\t\t\t";
}
}
#包括
使用名称空间std;
int main()
{
int计数器=1;
int-result,Alternative=0,classic=0,Country=0,Dance=0,Pop=0,Rock=0,RnB=0;
cout因此将退出选项添加到您的cout:
cout << "2. Classical\n3. Country\n4. Dance\n5. Pop\n6. Rock\n7. R&B\n8. quit
同样,在第一个if中,您应该增加选择,即在选择的情况下。最好是在仅读取一次值后增加选择,而不是在每个条件下重复
而在while循环中打印流派的cout应该是while循环的末尾,即最后一个大括号之前,即main的末尾。允许用户告诉您他已完成输入:
//...
cout << "...7. R&B\n999.End input \t\t\t\t\t\t\t";
do {
// all your statements
} while (result!= 999);
while(计数器编辑:
感谢@timrau和@sfjac对中断
、开关
和变量+
的建议
不必在迭代次数固定(100)后停止while
循环[1],您可以要求用户在完成时输入特定字符或数字
考虑以下代码,例如:
// assume you have already declared and initialized all the required variables, as in your example.
// for now we will say that this is -1, so that it doesn't count as a valid option, and so it passes our while's condition.
int result = -1;
while (result) // condition evaluates to FALSE if user enters zero.
{
// print out the menu.
cout << "The menu goes here... " << endl
<< "Press 0 (zero) when you are done.";
// get the input.
cin >> result;
// use switch to check against constant values. Its cleaner.
switch (result)
{
case 1: Alternative++; // For incrementing variables, use the ++ operator.
break;
case 2: Classical++;
break;
// and so on, for all your choices.
// And then,
default: cout << "Oops! That wasn\'t a valid choice. Try again..." << endl;
}
}
// now the user is done, and has entered 0.
cout << "\nAlternative: " << Alternative
<< "\tClassical: " << Classical
<< "\tCountry: " << Country << "\n"
<< "Dance: " << Dance
<< "\tPop: " << Pop
<< "\tRock: " << Rock
<< "\tR&B: " << RnB << "\t\t\t";
// and yes, you can fold your cout like that, so you don't have to type "cout" every time.
//假设您已经声明并初始化了所有必需的变量,如示例中所示。
//现在我们将说这是-1,因此它不算作有效选项,因此它通过了while的条件。
int结果=-1;
while(result)//如果用户输入零,则条件的计算结果为FALSE。
{
//把菜单打印出来。
cout和switch
和operator++
,…谢谢你的帮助!:)但是当我试着运行你的代码时,它给了我一个错误。它说“[错误]在'while'之前预期不合格的id”?尽管我仍在根据你的建议修改我自己的代码。再次感谢。你忘了把它全部放在main()
中吗?
int genre[8]={}; // create ainitalised array
//... as above
do {
cin>>result;
if (result>=0 && result<8) {
genre[result]++;
counter++;
}
else if (result!=999)
cout <<"Wrong value entered!\n";
} while (result!= 999);
while (counter <= 100 || result == -1)
{...}
bool stop = false;
while (counter <= 100 || stop)
{
switch(result)
{
case 1: ...;
case 2: ...;
.
.
.
default:
stop = true;
}
}
// assume you have already declared and initialized all the required variables, as in your example.
// for now we will say that this is -1, so that it doesn't count as a valid option, and so it passes our while's condition.
int result = -1;
while (result) // condition evaluates to FALSE if user enters zero.
{
// print out the menu.
cout << "The menu goes here... " << endl
<< "Press 0 (zero) when you are done.";
// get the input.
cin >> result;
// use switch to check against constant values. Its cleaner.
switch (result)
{
case 1: Alternative++; // For incrementing variables, use the ++ operator.
break;
case 2: Classical++;
break;
// and so on, for all your choices.
// And then,
default: cout << "Oops! That wasn\'t a valid choice. Try again..." << endl;
}
}
// now the user is done, and has entered 0.
cout << "\nAlternative: " << Alternative
<< "\tClassical: " << Classical
<< "\tCountry: " << Country << "\n"
<< "Dance: " << Dance
<< "\tPop: " << Pop
<< "\tRock: " << Rock
<< "\tR&B: " << RnB << "\t\t\t";
// and yes, you can fold your cout like that, so you don't have to type "cout" every time.