1

Note: I am a beginner. I have used goto statement to execute if the user enters marks more than 200 but if the user has entered marks less than 100 then the rest of the code should run. How can I do that ?

Here is the piece of code

#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;
}
kometen
  • 6,536
  • 6
  • 41
  • 51
Fras Irfan
  • 37
  • 5
  • 6
    *I have used goto statement...* -- Use proper structured programming, not `goto`. For example: `bool inputGood = true; do { inputGood = true; input and set inputGood to false if necessary } while (!inputGood);` – PaulMcKenzie Feb 04 '22 at 13:49
  • `if (a > 100 && b > 100 && c > 100 && d > 100 && e > 100) {` you don't want to use && here. It will only be true if all of the variables are greater than 100. – drescherjm Feb 04 '22 at 13:49
  • 1
    Why not rather use an endless loop and a `break` statement? – πάντα ῥεῖ Feb 04 '22 at 13:49
  • Don't use one-letter variable names. Try to name your variables according to their use or the data they're supposed to contain. – Some programmer dude Feb 04 '22 at 13:51
  • Also you will have a major problem if the user enters some non-integer data for the marks. A floating pointer value (which have the dot `'.'`) or a string will break your program. – Some programmer dude Feb 04 '22 at 13:52
  • Oh, and what happens if the user enters a negative number? You need to handle that some way as well. – Some programmer dude Feb 04 '22 at 13:55
  • After you fix the if condition to not require all variables to be over 100, you can solve your problem by moving the goto right after the `cout << "You must enter all subject marks below 100" << endl;` – drescherjm Feb 04 '22 at 13:58
  • What is the problem in the if statement can anybody explain ? – Fras Irfan Feb 04 '22 at 14:03
  • 2
    Lets take a small part of it: `a > 100 && b > 100`. If `a > 100` is true but `b > 100` is false, then the whole expression is false. – Some programmer dude Feb 04 '22 at 14:07
  • But its running can you tell me which is more efficient way ? @Someprogrammerdude – Fras Irfan Feb 04 '22 at 14:09
  • 4
    _"Note: I am a beginner."_ Run away from any beginner material that suggests using `goto`. – Drew Dormann Feb 04 '22 at 14:14

2 Answers2

4

As already pointed out in the comments section, the line

if (a > 100 && b > 100 && c > 100 && d > 100 && e > 100) {

is wrong. The entire if condition will only be true if all of the entered marks are above 100, but you want the if condition to be true if at least one of the entered marks are above 100. You can accomplish this by using || (logical OR) instead of && (logical AND).

Another issue is that using goto should generally be avoided, if you can easily accomplish the same thing using a loop. See the following question for further information: What is wrong with using goto?

The other answer avoids goto by using a do...while loop. It does this by introducing an additional variable and by checking that variable multiple times per loop iteration. However, if you use an infinite loop with an explicit break statement instead, introducing an additional variable that must be checked multiple times is not necessary:

//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;
}
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
2

Just use do-while loop as for example

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 );
ChrisMM
  • 8,448
  • 13
  • 29
  • 48
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335