C+中的循环+;检查错误输入,如果用户输入正确,则重新启动程序 我是C++新手。我已经给了作业,在作业中我必须计算分数并要求用户输入。如果他输入错误,我必须重新启动程序。如果用户输入了正确的输入,我必须处理数据,然后再次询问他是否要检查其他计算。到目前为止,我已经编写了此代码。我不知道如果用户输入错误,如何在程序中再次循环,如果成功,如何重新启动程序。请给我指导一下。谢谢 #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { //Declaring Variable char choice; char input; //Promptin User to Enter his/her Choice cout<<"Enter C or c for Computer Science: \n" ; cout<<"Enter S or s for Software Engineering: \n"; cout<<"Enter T or T for Telecom Engineering: \n"; cout<<"Select any option from the above Menu: "; cin>>input; if (input != 'c' || input != 'C'){ cout<<"Invalid input! Enter the correct input option again"; }else if (input != 's' || input != 'S' ){ cout<<"Invalid input! Enter the correct input option again"; }else if (input != 't' || input != 'T' ){ cout<<"Invalid input! Enter the correct input option again"; }else if (input == 'a' || input == 'A'){ } system("pause"); return 0; } #包括 #包括 使用名称空间std; int main(int argc,char*argv[]) { //声明变量 字符选择; 字符输入; //提示用户输入他/她的选择 cout
您可以使用一个简单的C+中的循环+;检查错误输入,如果用户输入正确,则重新启动程序 我是C++新手。我已经给了作业,在作业中我必须计算分数并要求用户输入。如果他输入错误,我必须重新启动程序。如果用户输入了正确的输入,我必须处理数据,然后再次询问他是否要检查其他计算。到目前为止,我已经编写了此代码。我不知道如果用户输入错误,如何在程序中再次循环,如果成功,如何重新启动程序。请给我指导一下。谢谢 #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { //Declaring Variable char choice; char input; //Promptin User to Enter his/her Choice cout<<"Enter C or c for Computer Science: \n" ; cout<<"Enter S or s for Software Engineering: \n"; cout<<"Enter T or T for Telecom Engineering: \n"; cout<<"Select any option from the above Menu: "; cin>>input; if (input != 'c' || input != 'C'){ cout<<"Invalid input! Enter the correct input option again"; }else if (input != 's' || input != 'S' ){ cout<<"Invalid input! Enter the correct input option again"; }else if (input != 't' || input != 'T' ){ cout<<"Invalid input! Enter the correct input option again"; }else if (input == 'a' || input == 'A'){ } system("pause"); return 0; } #包括 #包括 使用名称空间std; int main(int argc,char*argv[]) { //声明变量 字符选择; 字符输入; //提示用户输入他/她的选择 cout,c++,C++,您可以使用一个简单的do while循环: bool valid_input = false; do { // Code to read and validate the input... } while (valid_input == false); 如果输入有效,则将valid\u input设置为true,循环将结束 在一个不相关的注释中,如果您不使用大写或小写,请使用例如,因此您只需比较字母一次。例如std::tolower(输入) !=“c”只要答案是用开关语句定义的,这
do while
循环:
bool valid_input = false;
do
{
// Code to read and validate the input...
} while (valid_input == false);
如果输入有效,则将valid\u input
设置为true
,循环将结束
在一个不相关的注释中,如果您不使用大写或小写,请使用例如,因此您只需比较字母一次。例如
std::tolower(输入)
!=“c”
只要答案是用开关语句定义的,这里的代码就会提示用户输入答案。ans
是一个变量,用于保存与用户输入相对应的字符1或0(在开关情况下定义或不定义)。如果已定义,则ans将获取1,否则它将获取值0。当ans为1(在switch语句中定义)时,循环将重复执行
#包括
#包括
使用名称空间std;
int main(){
字符输入;
char ans;//正确答案,错误答案
做{
cout用户输入处理非常常见,通常可以使用类似的模式
基本上,您重新请求输入。您处理有效的选择并中断循环,当选择无效时显示错误,让循环再次请求输入
备注1:通过在这里不使用switch case,我可以立即中断循环。我立即中断循环是为了避免两次指定条件或使用标志,这也是我使用无结束条件的循环的原因
备注2:std::flush
用于在提示行上输入。它确保在等待输入之前显示提示
char inp = 0;
while (true) {
std::cout << "Give input (a, b): " << std::flush;
std::cin >> inp;
inp = std::tolower(inp);
if (inp == 'a') {
std::cout << 'a\n';
break;
}
if (inp == 'b') {
std::cout << 'b\n';
break;
}
std::cout << "invalid choice (" << inp << ")";
}
charinp=0;
while(true){
std::cout-inp;
inp=std::tolower(inp);
如果(inp='a'){
STD:谢谢大家支持。实际上这是我的第一个C++程序,抱歉我用过单词指导。实际上我已经成功编译了。请检查我的程序,我知道你不需要,但是我想知道我能不能在里面多加一些。
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
int main(int argc, char *argv[])
{
//Declaring Variable
char choice;
char input;
int addTest = 0, matricMarks = 0 , interMarks = 0 , result = 0;
start: //Label
system("cls"); // Clear the Screen
//Prompting User to Enter his/her Choice
cout<<"Please Select the Degree Programme in which you are interested in to take Addmission: ";
cout<<"\nEnter C or c for Computer Science: "<<endl ;
cout<<"Enter S or s for Software Engineering: "<<endl;
cout<<"Enter T or t for Telecom Engineering: \n"<<endl;
cout<<"\nSelect any option from the above Menu: ";
cin>>input;
//Switch Statement Started
switch(input){
//Case C Started
case 'c':
case 'C':
{
cout<<"Enter your Marks in Addmission Test: ";
cin>>addTest;
cout<<"Enter your Marks in Matric Degree: ";
cin>>matricMarks;
cout<<"Enter your Marks in Intermediate Degree: ";
cin>>interMarks;
result = (addTest * 0.20)+(matricMarks * 0.30)+(interMarks * 0.50);
if (result >= 70)
{
cout<<"\nCongratulations! You are eligible for the Computer Science degree program :)"<<endl;
}
else
{
cout<<"Sorry you Do not qualify for Computer Science Degree Programme: "<<endl;
system("pause");
}
break;
}//Case C Closeed
//Case s Started
case 's':
case 'S':
{
cout<<"Enter your Marks in Addmission Test: ";
cin>>addTest;
cout<<"Enter your Marks in Matric Degree: ";
cin>>matricMarks;
cout<<"Enter your Marks in Intermediate Degree: ";
cin>>interMarks;
result = (addTest * 0.20)+(matricMarks * 0.30)+(interMarks * 0.50);
if (result >= 85)
{
cout<<"\nCongratulations! You are eligible for the Software Enginnering degree program :)"<<endl;
}
else
{
cout<<"\nSorry! you Do not Qualify for Software Engineering Degree: "<<endl;
system("pause");
}
break;
}//Case S Closed
//Case t Started
case 't':
case 'T':
{
cout<<"Enter your Marks in Addmission Test: ";
cin>>addTest;
cout<<"Enter your Marks in Matric Degree: ";
cin>>matricMarks;
cout<<"Enter your Marks in Intermediate Degree: ";
cin>>interMarks;
result = (addTest * 0.20)+(matricMarks * 0.30)+(interMarks * 0.50);
if (result >= 80)
{
cout<<"\nCongratulations! You are eligible for the Telecom Engineering degree program :)"<<endl;
}
else
{
cout<<"Sorry you Do not Qualify for Telecom Enginnering Degree Programme: "<<endl;
system("pause");
}
break;
}//Case t Closed
//Default Case Started
default:
{
cout<<"\nInvalid input! Enter the correct option again: \n";
system("pause");
goto start; //Jump To Label Start
}//Deafult Case Closed
}// Switch Statement Close
//do while Loop Started
do{
cout<<"\nDo you want to check your eligibility in some other degree program y/n? : ";
cin>>choice;
if (choice=='Y'||choice=='y')
{
goto start; //jump to Label start:
}
else if (choice=='N'||choice=='n')
{
break;
}
}while(choice == 'y' || choice == 'Y');
//Do while Loop Closed
system("pause");
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[])
{
//声明变量
字符选择;
字符输入;
int addTest=0,矩阵标记=0,中间标记=0,结果=0;
开始://标签
系统(“cls”);//清除屏幕
//提示用户输入他/她的选择
使用do while()
loop?input
必须同时是c
和c
(如何?)而不是满足input!=“c”| input!=“c”
。为什么这么难?@LogicStuff几乎是不合逻辑的,逻辑对某些人来说有多难。:)堆栈溢出不是聊天室“指导”。这是一个问答网站。你的问题是什么?为什么会被否决?这个问题可能是基本的,没有问题形式,但这不是一个坏问题,它很有趣,因为几乎每个开始编程的人都会遇到这个问题。如果有什么问题,它可能会作为一个副本关闭。相关的“列表显示,但我建议使用它,因为它有未定义的行为(未初始化的变量)您可以使用std::tolower
或每个区块有多个案例:case'S':\ncase'S':ans=1;break;
很高兴看到一些反馈。遗憾的是,这还不够。但是,更常见的反馈方式是通过向上或向下投票、接受和评论问题/答案(向上投票也是鼓励人们继续帮助你的一种方式)。对于代码审查,我指示你。请注意,他们也有规则和约定。我确实注意到,尽管所有答案都建议你使用while循环,但你使用了goto。goto可能有用处,在你的练习中,它仍然很简单。然而(结束)使用goto's可能会很快出现问题,我希望你在完成作业的当晚不会遇到困难。它们几乎不再使用,因为3个goto's的组合可能已经很难遵循。在这种情况下,goto start可以由bool restart=true;do{…}代替while(restart)
loop和restart=false;
在choice==“n”
分支中(还请注意,重新启动问题的有效性检查是错误的:使用调试器和一些不同的输入进行检查)
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
char askInputChoice(const std::string& prompt, const std::vector<char>& valid)
{
char inp = 0;
while (true) {
std::cout << prompt << ": " << std::flush;
std::cin >> inp;
inp = std::tolower(inp);
if (std::find(valid.begin(), valid.end(), inp) != valid.end()) {
break;
}
std::cout << "invalid choice (" << inp << ")\n";
}
return inp;
}
int main()
{
char inp = askInputChoice("Give input (a, b)", std::vector<char>{'a','b'});
switch (inp) {
case 'a':
std::cout << "a\n";
break;
case 'b':
std::cout << "b\n";
break;
}
}
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
int main(int argc, char *argv[])
{
//Declaring Variable
char choice;
char input;
int addTest = 0, matricMarks = 0 , interMarks = 0 , result = 0;
start: //Label
system("cls"); // Clear the Screen
//Prompting User to Enter his/her Choice
cout<<"Please Select the Degree Programme in which you are interested in to take Addmission: ";
cout<<"\nEnter C or c for Computer Science: "<<endl ;
cout<<"Enter S or s for Software Engineering: "<<endl;
cout<<"Enter T or t for Telecom Engineering: \n"<<endl;
cout<<"\nSelect any option from the above Menu: ";
cin>>input;
//Switch Statement Started
switch(input){
//Case C Started
case 'c':
case 'C':
{
cout<<"Enter your Marks in Addmission Test: ";
cin>>addTest;
cout<<"Enter your Marks in Matric Degree: ";
cin>>matricMarks;
cout<<"Enter your Marks in Intermediate Degree: ";
cin>>interMarks;
result = (addTest * 0.20)+(matricMarks * 0.30)+(interMarks * 0.50);
if (result >= 70)
{
cout<<"\nCongratulations! You are eligible for the Computer Science degree program :)"<<endl;
}
else
{
cout<<"Sorry you Do not qualify for Computer Science Degree Programme: "<<endl;
system("pause");
}
break;
}//Case C Closeed
//Case s Started
case 's':
case 'S':
{
cout<<"Enter your Marks in Addmission Test: ";
cin>>addTest;
cout<<"Enter your Marks in Matric Degree: ";
cin>>matricMarks;
cout<<"Enter your Marks in Intermediate Degree: ";
cin>>interMarks;
result = (addTest * 0.20)+(matricMarks * 0.30)+(interMarks * 0.50);
if (result >= 85)
{
cout<<"\nCongratulations! You are eligible for the Software Enginnering degree program :)"<<endl;
}
else
{
cout<<"\nSorry! you Do not Qualify for Software Engineering Degree: "<<endl;
system("pause");
}
break;
}//Case S Closed
//Case t Started
case 't':
case 'T':
{
cout<<"Enter your Marks in Addmission Test: ";
cin>>addTest;
cout<<"Enter your Marks in Matric Degree: ";
cin>>matricMarks;
cout<<"Enter your Marks in Intermediate Degree: ";
cin>>interMarks;
result = (addTest * 0.20)+(matricMarks * 0.30)+(interMarks * 0.50);
if (result >= 80)
{
cout<<"\nCongratulations! You are eligible for the Telecom Engineering degree program :)"<<endl;
}
else
{
cout<<"Sorry you Do not Qualify for Telecom Enginnering Degree Programme: "<<endl;
system("pause");
}
break;
}//Case t Closed
//Default Case Started
default:
{
cout<<"\nInvalid input! Enter the correct option again: \n";
system("pause");
goto start; //Jump To Label Start
}//Deafult Case Closed
}// Switch Statement Close
//do while Loop Started
do{
cout<<"\nDo you want to check your eligibility in some other degree program y/n? : ";
cin>>choice;
if (choice=='Y'||choice=='y')
{
goto start; //jump to Label start:
}
else if (choice=='N'||choice=='n')
{
break;
}
}while(choice == 'y' || choice == 'Y');
//Do while Loop Closed
system("pause");
return 0;
}