为无效字符创建循环
creating a loop for invalid characters
我要做的是为无效字符创建一个循环,例如:如果允许的字符是"A"、"A"、"C"、"C",并且用户输入"f"它会说它无效,并要求他们输入一个新的字符,直到他们输入了一个有效的字符。
我也不确定它会在以下代码中进入什么位置:
#include<iostream>
using namespace std;
int main (){
int units;
double priceA,priceB,priceC;
char package, goAgain;
priceA=15.95;
priceB=23.95;
priceC=45.95;
do {
cout << "Enter the Package chosen. Enter 'A' or 'a' for package A.n";
cout << "Enter 'b' or 'B' for package B.n";
cout << "Enter 'c' or 'C' for package C.n";
cin >> package;
cout << "Enter the number of message units";
cin >> units;
while(units>672){
cout <<endl;
cout <<"Invalid number,please enter a value from 0 to 672."<<endl;
cout <<"Please Enter a new Number for units.";
cin >>units;
}
switch (package){
case 'A':
case 'a':
if(units>10){
priceA=15.95+(units-10)*.20;
priceB=23.95+(units-20)*.10;
cout <<"The price of Package A is" << " $"<<priceA<< endl;
}
else if(units<=10){
priceA=15.95;
cout << "The price of Package A is" << " $"<<priceA<<endl;
}
if (priceA>priceB){
cout <<"By switching to Package B you would save" << " $" << priceA- priceB<<endl;
}
if (priceA>priceC){
cout << "By switching to Package C you would save" << " $"<<priceA- priceC<<endl;
}
break;
case 'b':
case 'B':
if(units>10){
priceA=15.95+(units-10)*.20;
priceB=23.95+(units-20)*.10;
cout <<"The price of Package B is" << " $"<<priceB<< endl;
}
else if(units<=10){
priceB=23.95;
cout << "The price of Package B is" << " $"<<priceB<<endl;
}
if (priceB>priceA){
cout <<"By switching to Package A you would save" << " $" << priceB- priceA<<endl;
}
if (priceB>priceC){
cout << "By switching to Package C you would save" << " $"<<priceB-priceC<<endl;
}
break;
case 'c':
case 'C':
if(units>10){
priceA=15.95+(units-10)*.20;
priceB=23.95+(units-20)*.10;
cout <<"The price of Package C is" << " $"<<priceC<< endl;
}
else if(units<=10){
priceC=45.95;
cout << "The price of Package C is" << " $"<<priceC<<endl;
}
if (priceC>priceA){
cout <<"By switching to Package A you would save" << " $" << priceC-priceA<<endl;
}
if (priceC>priceB){
cout << "By switching to Package B you would save" << " $"<<priceC-priceB<<endl;
}
break;
}
cout<<"do you want to go again? y/n" << endl;
cin >>goAgain;
}
while(goAgain=='y');
}
开关可以有一个default
分支,它处理其他case
分支中未处理的所有内容。您可以将代码放在那里,然后将整个switch
放在do
-while
循环中,如下所示:
bool valid;
do {
valid = true;
switch (package) {
case 'A':
⋮
default:
valid = false;
cout << "Invalid bla bla " << package << ", enter it again" << endl;
cin >> package;
break;
}
} while (!valid);
您可以尝试以下操作:
bool valid = false;
while (!valid)
{
const std::string valid_packages = "AaCc";
char package;
cin >> package;
if (valid_packages.find(package) != std::string::npos)
{
valid = true;
}
else
{
cout << "n"
<< "Invalid package name, try again.n";
}
}
有效的包裹字母被放入一个字符串中。用户的输入将根据有效字母串进行检查。如果找到用户的输入,标志valid
将设置为true,从而结束循环。
相关文章:
- 正在使用for循环创建QScatterSerie
- 使用循环创建类对象
- 在C++中使用循环创建多个对象
- 我想在 C++ 中使用 for 循环创建类对象
- 循环创建对象以进行C++
- 如何使用由for循环创建的向量向量构建链接列表
- C++:使用 for 循环创建多个数据结构
- 使用循环创建条形图
- 如何为创建循环创建新样式
- 为项目和循环创建简单的控制菜单会比预期打印提示的次数更多
- C vs Java:无尽的循环创建对象仅崩溃C
- 数组和 For 循环:创建从大到小的数字列表
- 使用嵌套 while 循环创建模式
- 如何从 for 循环创建计算数据列表
- 如何在 C++ 中使用 for 循环创建金字塔
- 通过循环创建弹出菜单 w.r.t 到参数时出现问题
- 从嵌套循环创建数学表达式
- 使用for循环创建结构名称
- 使用while循环创建链接列表
- 使用opengl中的for循环创建立方体