我在这里的问题是,我不知道如何插入一个规则,其中如果用户输入的字符串一个数字,它会cout
警告说这是不是有效的,同样有如果用户输入的上档次的字符串/字符。怎么样?我一直在尝试,但公式不起作用。如何检查输入是整数/字符串?
int x, cstotal = 100, extotal = 150;
double scorecs, exscore, labtotala, labtotalb, total;
string mystr = "";
cout << "Compute for: " << "\n" << "1. Laboratory Grade " << "\n" << "2. Lecture Grade" << "\n" << "3. Exit" << "\n";
cout << "Please enter a number: ";
cin >> x;
switch (x) {
case 1:
cout << "Compute for laboratory grade." << "\n";
cout << "Enter Student Name: ";
cin >> mystr;
cout << "Good day, " << mystr << " . Please provide the following grades: " << "\n";
cout << "CS Score: ";
cin >> scorecs;
cout << "Exam Score: ";
cin >> exscore;
labtotala = scorecs/cstotal * 0.6;
labtotalb = exscore/extotal * 0.4;
total = labtotala + labtotalb;
cout << "Your Laboratory Grade is " << total * 100 << "\n";
system("pause");
break;
case 2:
cout << "Compute for lecture grade." << "\n";
cout << "Enter Student Name: ";
cin >> mystr;
cout << "Good day, " << mystr << " . Please provide the following grades: " << "\n";
cout << "CS Score: ";
cin >> scorecs;
cout << "Exam Score: ";
cin >> exscore;
labtotala = scorecs/cstotal * 0.7;
labtotalb = exscore/extotal * 0.3;
total = labtotala + labtotalb;
cout << "Your Lecture Grade is " << total * 100 << "\n";
system("pause");
break;
哪一部分规则是给你的麻烦?如何实现检测,控制流,分支?如果只是检测一个字符串是否是数字的问题,则可以遍历每个字符并调用isdigit。 – RonaldBarzell
你需要更具体地了解你在找什么。尽可能少发布代码。 –