Consolidation help

Hi all,

I've only been doing this a few days.

I decided to try the program that asks you to input your score on a programming test and then returns your grade.

The thing i'm having difficulty with is remembering where the ; go when using else an if statements.

Would really appreciate some help. I've been juggling stuff around and don't really seem to be getting anywhere, just different errors each time.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    #include <iostream>

    using namespace std;

    int main()

    {


        int userScore;


        cout << "Enter your score <1-100>: \n";
        cin >> userScore;
        cout << endl;

        if ( userScore == 100 )

        cout << "Congratulations, perfect score! \n";



        else
       
        {

        if ( userScore > 89 && < 100 )

        cout << "Well done, you achieved a grade A. \n";

        }

        else

        {

        if ( userScore > 79 && < 90 )

            cout << "Well done, you achieved a grade B. \n";

        }

        else

        {

        if ( userScore > 69 && < 80 )

            cout << "Well done, you achieved a grade C. \n";

        }

        else

        {

        if ( userScore > 59 && < 70 )

            cout << "Try better next time, you achieved a grade D. \n";

        }

        else


        {

        if ( userScore > 0 && < 60 )

            cout << "Grade F, probably should have revised. \n";
        }
        {

        else

            cout << "Invalid score entered. \n";

        }

        cin.get();
        return 0;
    }
You don't use a semicolon with an if/else statement. You issue is with bracketing, your issue would be more clear if you used proper indentation to line up your ifs with the corresponding else.

1
2
3
4
5
6
7
8
9
10
        if ( userScore == 100 )
                cout << "Congratulations, perfect score! \n";
        else
        {

                if ( userScore > 89 && < 100 )
                        cout << "Well done, you achieved a grade A. \n";
        }

        else


As you can see, the second else doesn't actually belong to any if statement. The if on line 6 doesn't have an else statement.
Thanks for the input. I've finally resolved the issue. It was asking for a primary expression before < so I was inputting if ( userScore > 89 && < 100 ) instead of if ( userScore > 89 && userScore < 100 )

I understand where I went wrong, but would someone mind explaining why I need all the braces at the end? Many thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>

using namespace std;

int main()

{


    int userScore;


    cout << "Enter your score <1-100>: \n";
    cin >> userScore;
    cout << endl;

    if ( userScore == 100 )

    {
       cout << "Congratulations, perfect score! \n";
    }

    else {

         if ( userScore > 89 && userScore < 100 )

         {
             cout << "Well done, you achieved a grade A. \n";
         }

         else {

                if ( userScore > 79 && userScore < 90 )
                {
                    cout << "Well done, you achieved a grade B. \n";
                }

                    else {
                        
                        if ( userScore > 69 && userScore < 80 )
                        {
                        cout << "Well done, you achieved a grade C. \n";
                        }
        
                                else {

                                if ( userScore > 59 && userScore < 70 )
                                {
                                  cout << "Try better next time, you achieved a grade D. \n";
                                }

                                        else {

                                        if ( userScore > 0 && userScore < 60 )
                                        {
                                            cout << "Grade F, probably should have revised. \n";
                                        }

                                                    else {
                                                        cout << "Invalid score entered. \n";
                                                         }

                                                    }
                                            }
                                    }
                            }
                    }

    cin.get();
    return 0;
}
I think you have a bunch of brackets because you have unnecessary else statements.

This is How I would do it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
using namespace std;

int main()

{
	int userScore;

	cout << "Enter your score <1-100>: \n";
	cin >> userScore;
	cout << endl;
	cin.ignore(99,'\n');

	if ( userScore < 1 || userScore > 100 )
	{
		cout << endl << "Date out of range: Program terminating." << endl;
		cout << "Press [ENTER] to finish...";
		cin.ignore(99,'\n');
		return 1;
	}


    if ( userScore == 100 )
	{
		cout << "Congratulations, perfect score! \n";
	}

	if ( userScore > 89 && userScore < 100 )
	{
		cout << "Well done, you achieved a grade A. \n";
	}

	if ( userScore > 79 && userScore < 90 )
	{
		cout << "Well done, you achieved a grade B. \n";
	}


    if ( userScore > 69 && userScore < 80 )
	{
		cout << "Well done, you achieved a grade C. \n";
	}

	if ( userScore > 59 && userScore < 70 )
	{
		cout << "Try better next time, you achieved a grade D. \n";
	}
	
	if ( userScore > 0 && userScore < 60 )
	{
		cout << "Grade F, probably should have revised. \n";
	}
                                          
	cin.ignore(99,'\n');
	return 1;
}


edit: damn copy paste formatting >.>
Last edited on
Oh I see, so I'd only need to use an else statement for any other possible outcome? (which wouldn't be used in this program).

Also, what does the cin.ignore (99) do?

Thanks very much.
If-Else statements is kinda like in algebra;

if it's black, it's an 8 ball. Else (it's something else)...

1
2
3
4
5
6
7
8
   if (black)
   {
      cout << "it's an 8 ball" << endl;
   }
   else
   (
   cout << "It's not an eight ball" << endl;
   )


I know it's a stupid example, but that's kinda the gist of it, I think.

You could do what you're trying to do with IF-Else Statements, but why complicate life?

And "cin.ignore (99)" let's the person hit ANY key for 99 times before the console closes. (the 99 is modifiable bytheway; I use 99 kind of like standard)
My teacher told me to use it everytime you use "cin" and at the end of course.
Otherwise, you might have problems with your console closing down. Ever since I use it I don't have that problem anymore.

:3
yes, I asked my instructor that. I feel like I waste his time cause he has a PhD and I'm here like "oh, what's this for?" all the time. :3
Last edited on
Add this in case the person inputs a letter. It basically says "error, closing down". You should probably add it after your number range limits.

1
2
3
4
5
6
7
8
9
10
11
	// Non Numeric Input
	if ( !cin )
	{
		cin.clear();
		cin.ignore(99,'\n');

		cout << endl << "Non-numeric input: Program terminating." << endl;
		cout << "Press [ENTER] to finish...";
		cin.ignore(99,'\n');
		return 1;
	}
Appreciate the input but I'm not going to use that until I know what it all means.

One last question for now, I want the program to repeat over and over until the user closes it down. I've tried a while (true) but it just spams Enter your score.

I've been messing around with trying to call a function (mainly because that's only as far as I've got in video tutorials). Whenever I enter a number, it just comes back with the data out of range. I'm sure you more experienced people have a much much easier way :P

Many thanks for the awesome help thus far. (P.S. I know userScore is declared twice, i'm just getting allsorts of errors without it.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
using namespace std;

void Func1 ()

{
    int userScore;
    cout << "Enter your score <1-100>: \n";
	cin >> userScore;
	cout << endl;
}


int main ()

{
	int userScore;
	int repeat;
        Func1();
	if ( userScore < 1 || userScore > 100 )
	{

		cout << endl << "Date out of range: Program terminating." << endl;
		cout << "Press [ENTER] to finish...";
		cin.ignore(99,'\n');
		return 1;
	}


    if ( userScore == 100 )
	{
		cout << "Congratulations, perfect score! \n";
	}

	if ( userScore > 89 && userScore < 100 )
	{
		cout << "Well done, you achieved a grade A. \n";
	}

	if ( userScore > 79 && userScore < 90 )
	{
		cout << "Well done, you achieved a grade B. \n";
	}


    if ( userScore > 69 && userScore < 80 )
	{
		cout << "Well done, you achieved a grade C. \n";
	}

	if ( userScore > 59 && userScore < 70 )
	{
		cout << "Try better next time, you achieved a grade D. \n";
	}

	if ( userScore > 0 && userScore < 60 )
	{
		cout << "Grade F, probably should have revised. \n";
	}
    cout << endl << endl;
    cout << "Would you like to run the program again (1 for Yes / 2 for No.): \n";
    cin >> repeat;

        if (repeat == 1)

        {
            Func1();
        }

    else {
            cout << "\n\n Thankyou! Goodbye.";

            }

	cin.get();
	return 1;
}
Last edited on
userscore needs to be declared twiced because its a local variable (inside a function) in both cases. If you placed int userscore under the using namespace you would only have to declare it once.
Thanks Nova.

Any ideas on the loop?
try something like this to exit the loop (typing in exit will end the loop)

1
2
3
4
5
6
7
8
9
while (selection !=exit){
cin>>selection;

//if and else statements



}
Hmm, I will try that tomorrow but doesn't seem to make much sense to me.
I'll work on it now and I'll let you know if it works.

ok so you can either do this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int repeat=1;
while (repeat==1){
        Func1();
	
	if ( userScore < 1 || userScore > 100 )
	{


        }
        
 cout << endl << endl;
    cout << "Would you like to run the program again (1 for Yes / 2 for No.): \n";
	cin >> repeat;
       }


or

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int repeat;
while (repeat!=2){
        Func1();
	
	if ( userScore < 1 || userScore > 100 )
	{


        }
        
 cout << endl << endl;
    cout << "Would you like to run the program again (1 for Yes / 2 for No.): \n";
	cin >> repeat;
       }


In the first case every number but 1 exits the program, in the second case only 2 exits the program. If you want it so only 1 and 2 affect your loop you have to create a different loop for that
Last edited on
Topic archived. No new replies allowed.