我是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;
}
答案 0 :(得分:0)
您可以使用简单的do while
循环执行此操作:
bool valid_input = false;
do
{
// Code to read and validate the input...
} while (valid_input == false);
如果输入有效,则将valid_input
设置为true
,循环将结束。
在不相关的说明中,如果您不是关于大写或小写的情况,请使用例如std::tolower
所以你只需要比较一次这封信。例如。 std::tolower(input)
!= 'c'
。
答案 1 :(得分:0)
只要使用switch语句定义了答案,这里的代码就会提示用户回答。 ans
是一个用于保存字符的变量,1或0对应于用户的输入(在交换机情况下定义与否)。如果已定义,那么ans得到1另外一个它得到值0. Do while循环重复而ans是1(在switch语句中定义)。
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
char input;
char ans; //correct answer, incorrect answer
do {
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;
switch (input){
case 'S':
ans = 1;
break;
case 's':
ans = 1;
break;
case 'C':
ans = 1;
break;
case 'c':
ans = 1;
break;
case 'T':
ans = 1;
break;
case 't':
ans = 1;
break;
default:
ans = 0;
}
} while (ans);
return 0;
}
答案 2 :(得分:0)
用户输入处理很常见,通常可以使用类似的模式。
基本上,你要求输入。您处理有效选择并突破循环,当选择无效时显示错误并让循环再次询问输入。
备注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 << ")";
}
此函数可以使无效选择处理更通用,但有效选择的处理仍必须在本地完成:
#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;
}
}
要重新启动程序,请将其置于while循环中,添加一个退出选项(&#39; q&#39;)并在给出该选项时中断。
答案 3 :(得分:0)
感谢所有人的支持。实际上它是我在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;
}