logical operator problem

At the end of my code (ex. 2) I am trying to have the user input scores and then a -1 to end.
The output would then display how many A's B's ect.
My output is only working for the A's and the F's, BCD give me a to the e number, for example 3.45332e-344332.
Any ideas?

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
83
84
85
86
87
88
89
90
91
92
93
94
95
 #include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;

int main ()
{
    char ch;
    int ex;
    cout<<"Which excerise? ";
    cin>>ex;
    cout<<endl;
    if (ex == 1)
    {
    double beg, end;
    int num;
        
        cout<<"Enter beginning and ending numbers, seperated by space: "<<endl;
        cin>>beg;
        cin>>end;
        cout<<endl;
    
    for (num=beg; num<=end; ++num)
    {
        cout<<num<<","<<" ";
    }
    cout<<endl;
    cout<<endl;
    
    cout<<"Enter beginning and ending numbers, seperated by space: "<<endl;
    cin>>beg;
    cin>>end;
    cout<<endl;
    
    for (num=beg; num<=end; num++)
    {
        cout<<num<<","<<" ";
        num+=1;
    }
    cout<<endl;
    cout<<endl;
    
    cout<<"Enter the beginning and largest positive numbers, seperated by space: "<<endl;
    cin>>beg;
    cin>>end;
    cout<<endl;
    
    for (num=beg; num<=end; num)
    {
        cout<<num<<","<<" ";
        num*=-2;
    }
    cout<<endl;
    cout<<endl;
    }
    if (ex == 2)
    {
    int grade;
    double A, B, C, D, F;
    grade=0;
    
        cout<<"Enter one or more grades, or -1 to stop:"<<endl;
        
        do 
        {
        cin>>grade;    
        
            if (grade>=90) A++;
			if ((grade>=80) && (grade<=89)) B++;
			if ((grade>=70) && (grade<=79)) C++;
			if ((grade>=60) && (grade<=69)) D++;
			if ((grade>=0) && (grade<=59)) F++;
        }
        while (grade!=-1);
        
            
        {
        cout<<"The grades breakdown is:"<<endl;
        cout<<"A's: "<<A<<endl;
        cout<<"B's: "<<B<<endl;
        cout<<"C's: "<<C<<endl;
        cout<<"D's: "<<D<<endl;
        cout<<"F's: "<<F<<endl;
        }
    }
    if (ex == 3)
    {
        //put code here
    }

    
return 0;
}
That indentation makes your code hard to read, and hard to verify.

Looking at the reformatted code, it seemed ok. I ran it and it worked, i.e. it stops on -1. Why do you think there was a problem?
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
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;

int main ()
{
	char ch;

	int ex;
	cout<<"Which excerise? ";
	cin>>ex;
	cout<<endl;

	if (ex == 1)
	{
		double beg, end;
		int num;

		cout<<"Enter beginning and ending numbers, seperated by space: "<<endl;
		cin>>beg;
		cin>>end;
		cout<<endl;

		for (num=beg; num<=end; ++num)
		{
			cout<<num<<","<<" ";
		}
		cout<<endl;
		cout<<endl;

		cout<<"Enter beginning and ending numbers, seperated by space: "<<endl;
		cin>>beg;
		cin>>end;
		cout<<endl;

		for (num=beg; num<=end; num++)
		{
			cout<<num<<","<<" ";
			num+=1;
		}
		cout<<endl;
		cout<<endl;

		cout<<"Enter the beginning and largest positive numbers, seperated by space: "<<endl;
		cin>>beg;
		cin>>end;
		cout<<endl;

		for (num=beg; num<=end; num)
		{
			cout<<num<<","<<" ";
			num*=-2;
		}
		cout<<endl;
		cout<<endl;
	}
	if (ex == 2)
	{
		int grade;
		double A, B, C, D, F;
		grade=0;

		cout<<"Enter one or more grades, or -1 to stop:"<<endl;

		do 
		{
			cin>>grade;    

			if (grade>=90) A++;
			if ((grade>=80) && (grade<=89)) B++;
			if ((grade>=70) && (grade<=79)) C++;
			if ((grade>=60) && (grade<=69)) D++;
			if ((grade>=0) && (grade<=59)) F++;
		}
		while (grade!=-1);

		{
			cout<<"The grades breakdown is:"<<endl;
			cout<<"A's: "<<A<<endl;
			cout<<"B's: "<<B<<endl;
			cout<<"C's: "<<C<<endl;
			cout<<"D's: "<<D<<endl;
			cout<<"F's: "<<F<<endl;
		}
	}
	if (ex == 3)
	{
		//put code here
	}

	return 0;
}
Last edited on
Thank you for indenting, I need to work on that.
The problem is when you input grades.
For example:
you input
90
99
83
76
-1
The -1 ends the program (works great)
The output reads like this:
A's: 2
B's: 3.45e-56764
C's:3.41e-786
D's: 5.65e4325
F's: 0
When it should read like:
A's: 2
B's: 1
C's: 1
D's: 0
F's: 0
Your ABCDEF variables are uninitialized, so they contain some random value at the beginning.
Thank MiiNiPaa I just saw the results are only incorrect when nothing is entered, so that makes sense.
Do I just need to set ABCDF to 0 first?
Yes
Topic archived. No new replies allowed.