如果用户输入的标记小于 100,如何跳过 goto 语句
How can I skip the goto statement if the user enters marks less than 100
注:我是初学者
如果用户输入的标记超过 200,我使用 goto 语句执行,但如果用户输入的标记小于 100,则其余代码应该 运行。
我该怎么做?
这是一段代码
#include <iostream>
using namespace std;
int main()
{
int a, b, c, d, e;
float sum, perc;
int total = 500;
INPUT:
cout << "Enter marks for English" << endl;
cin >> a;
cout << "Enter marks for Urdu" << endl;
cin >> b;
cout << "Enter marks for Maths" << endl;
cin >> c;
cout << "Enter marks for Computer" << endl;
cin >> d;
cout << "Enter marks for Islamiat" << endl;
cin >> e;
if (a > 100 && b > 100 && c > 100 && d > 100 && e > 100) {
cout << "You must enter all subject marks below 100" << endl;
}
goto INPUT;
sum = a + b + c + d + e;
perc = (sum / total) * 100;
if (a <= 100 && b <= 100 && c <= 100 && d <= 100 && e <= 100) {
cout << "Percentage is = " << perc << "%" << endl;
}
else {
cout << "You must enter marks below 100" << endl;
return 0;
}
if (perc >= 50) {
cout << "Congratulations you are Passed" << endl;
}
else {
cout << "You are fail" << endl;
}
return 0;
}
例如使用do-while循环
bool success = false;
do
{
cout << "Enter marks for English" << endl;
cin >> a;
cout << "Enter marks for Urdu" << endl;
cin >> b;
cout << "Enter marks for Maths" << endl;
cin >> c;
cout << "Enter marks for Computer" << endl;
cin >> d;
cout << "Enter marks for Islamiat" << endl;
cin >> e;
success = not ( a > 100 || b > 100 || c > 100 || d > 100 || e > 100 );
if ( not success )
{
cout << "You must enter all subject marks below 100" << endl;
}
} while ( not success );
正如评论部分已经指出的那样,行
if (a > 100 && b > 100 && c > 100 && d > 100 && e > 100) {
错了。整个 if
条件只有在 所有 输入的标记都在 100 以上时才为真,但是如果 至少有一个 输入的分数高于 100。您可以使用 ||
(逻辑或)代替 &&
(逻辑与)来完成此操作。
另一个问题是通常应避免使用 goto
,如果您可以使用循环轻松完成同样的事情。有关详细信息,请参阅以下问题:What is wrong with using goto?
另一个答案通过使用 do
...while
循环避免了 goto
。它通过引入一个附加变量并在每次循环迭代中多次检查该变量来实现。但是,如果您改为使用带有显式 break
语句的无限循环,则无需引入必须多次检查的附加变量:
//loop forever until input is ok
while ( true )
{
cout << "Enter marks for English: " << endl;
cin >> a;
cout << "Enter marks for Urdu: " << endl;
cin >> b;
cout << "Enter marks for Maths: " << endl;
cin >> c;
cout << "Enter marks for Computer: " << endl;
cin >> d;
cout << "Enter marks for Islamiat: " << endl;
cin >> e;
//check whether input is ok
if ( a <= 100 && b <= 100 && c <= 100 && d <= 100 && e <= 100 )
//input is ok, so we can break out of the infinite loop
break;
//input is not ok, so we must print an error message and repeat the loop
cout << "You must enter all subject marks at or below 100\n" << endl;
}
注:我是初学者 如果用户输入的标记超过 200,我使用 goto 语句执行,但如果用户输入的标记小于 100,则其余代码应该 运行。 我该怎么做?
这是一段代码
#include <iostream>
using namespace std;
int main()
{
int a, b, c, d, e;
float sum, perc;
int total = 500;
INPUT:
cout << "Enter marks for English" << endl;
cin >> a;
cout << "Enter marks for Urdu" << endl;
cin >> b;
cout << "Enter marks for Maths" << endl;
cin >> c;
cout << "Enter marks for Computer" << endl;
cin >> d;
cout << "Enter marks for Islamiat" << endl;
cin >> e;
if (a > 100 && b > 100 && c > 100 && d > 100 && e > 100) {
cout << "You must enter all subject marks below 100" << endl;
}
goto INPUT;
sum = a + b + c + d + e;
perc = (sum / total) * 100;
if (a <= 100 && b <= 100 && c <= 100 && d <= 100 && e <= 100) {
cout << "Percentage is = " << perc << "%" << endl;
}
else {
cout << "You must enter marks below 100" << endl;
return 0;
}
if (perc >= 50) {
cout << "Congratulations you are Passed" << endl;
}
else {
cout << "You are fail" << endl;
}
return 0;
}
例如使用do-while循环
bool success = false;
do
{
cout << "Enter marks for English" << endl;
cin >> a;
cout << "Enter marks for Urdu" << endl;
cin >> b;
cout << "Enter marks for Maths" << endl;
cin >> c;
cout << "Enter marks for Computer" << endl;
cin >> d;
cout << "Enter marks for Islamiat" << endl;
cin >> e;
success = not ( a > 100 || b > 100 || c > 100 || d > 100 || e > 100 );
if ( not success )
{
cout << "You must enter all subject marks below 100" << endl;
}
} while ( not success );
正如评论部分已经指出的那样,行
if (a > 100 && b > 100 && c > 100 && d > 100 && e > 100) {
错了。整个 if
条件只有在 所有 输入的标记都在 100 以上时才为真,但是如果 至少有一个 输入的分数高于 100。您可以使用 ||
(逻辑或)代替 &&
(逻辑与)来完成此操作。
另一个问题是通常应避免使用 goto
,如果您可以使用循环轻松完成同样的事情。有关详细信息,请参阅以下问题:What is wrong with using goto?
另一个答案通过使用 do
...while
循环避免了 goto
。它通过引入一个附加变量并在每次循环迭代中多次检查该变量来实现。但是,如果您改为使用带有显式 break
语句的无限循环,则无需引入必须多次检查的附加变量:
//loop forever until input is ok
while ( true )
{
cout << "Enter marks for English: " << endl;
cin >> a;
cout << "Enter marks for Urdu: " << endl;
cin >> b;
cout << "Enter marks for Maths: " << endl;
cin >> c;
cout << "Enter marks for Computer: " << endl;
cin >> d;
cout << "Enter marks for Islamiat: " << endl;
cin >> e;
//check whether input is ok
if ( a <= 100 && b <= 100 && c <= 100 && d <= 100 && e <= 100 )
//input is ok, so we can break out of the infinite loop
break;
//input is not ok, so we must print an error message and repeat the loop
cout << "You must enter all subject marks at or below 100\n" << endl;
}