我是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;
}
使用'do while()'循環怎麼樣? –
'input'必須同時是'c'和'C'(怎麼樣?)不能滿足'input!='c'||輸入!='C''。爲什麼這麼難? – LogicStuff
@LogicStuff對於一些邏輯來說,邏輯幾乎是不合邏輯的。 :) –