Help with code accepting decimal as input

closed account (zT4NhbRD)
For my assignment I'm being asked to write a program that allows a user to input as many grades as they'd like and then calculate only the GPA of only valid entries. I've yet to write the GPA calculation part of the code but what I'm having a hard time with is getting the program to accept decimal numbers as an input for grades. Every time I put in a decimal number, the program quits rather than allowing the user to answer if they'd like to put in another grade.

I'm super new to C++ so sorry for my horrible coding below. Thanks very much!

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
  #include <iostream>


using namespace std;

main ()

{
   int grade = 0, x=0;
   char answer;
   
   cout <<"Enter Grade " << x++ << " (From 0 to 100): ";
   cin >> grade; 
         const int    GRADEA=((grade>=90.00)&&(grade<=100.00)),
                      GRADEB=((grade>=80)&&(grade<90)),
                      GRADEC=((grade>=70)&&(grade<80)),
                      GRADED=((grade>=60)&&(grade<70)),
                      GRADEF=((grade<=60)&&(grade>0));
                   
         if (grade=GRADEA) {
             cout << "Your Letter Grade is A." << endl; }
         if (grade=GRADEB) {
             cout << "Your Letter Grade is B." << endl; }
         if (grade=GRADEC) {
             cout << "Your Letter Grade is C." << endl; }
         if (grade=GRADED) {
             cout << "Your Letter Grade is D." << endl; }
         if (grade=GRADEF) {
             cout << "Your Letter Grade is F." << endl; }
        
         
         cout << "Do you wish to input another grade? (Y/N) ";
         cin >> answer; 
   
  while (answer=='y' || answer=='Y') {
         cout <<"Enter Grade " << x++ << " (From 0 to 100): ";
         cin >> grade; 
         const int    GRADEA=((grade>=90)&&(grade<=100)),
                      GRADEB=((grade>=80)&&(grade<90)),
                      GRADEC=((grade>=70)&&(grade<80)),
                      GRADED=((grade>=60)&&(grade<70)),
                      GRADEF=((grade<=60)&&(grade>0));
                   
             if (grade=GRADEA) {
                cout << "Your Letter Grade is A." << endl; }
             if (grade=GRADEB) {
                cout << "Your Letter Grade is B." << endl; }
             if (grade=GRADEC) {
                cout << "Your Letter Grade is C." << endl; }
             if (grade=GRADED) {
                cout << "Your Letter Grade is D." << endl; }
             if (grade=GRADEF) {
                cout << "Your Letter Grade is F." << endl; }
         
         cout << "Do you wish to input another grade? (Y/N) " ;
         cin >> answer;   
                    
   }  
   
   if (answer=='n' || answer=='N') {
        cout << "thanks"; };                 
   
   system ("pause");
   return 0;
       
}
Oh my lord, lol. This won't even compile will it? You should be using nested if-else statements, and the const variables you are declaring actually aren't variables. Although you are the right track as far as the comparison operations, replace the variables inside the if-elses with what you are trying to declare them to be.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if ( conditional )
{
//code
{
else if ( conditional )
{
//code
}
else if ( conditional )
{
//code
}
else
{
//code
}
Last edited on
The data type int is only for whole numbers. You need to use float to accept a decimal.

So change
int grade = 0
to float grade=0;
Something like this may be more appropriate:

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
#include<iostream>

using namespace std;

int main()

{
	float mark;

	cout << "Enter mark for letter grade: ";
	cin >> mark;
	
	if(mark < 50)
	{
		cout << "F" << endl;
	}
	else if(mark >= 50 && mark < 60 )
	{
		cout << "P" << endl;
	}
	else if (mark >= 60 && mark < 70)
	{
		cout << "D" << endl;
	}
	else if (mark >= 70 && mark < 80)
	{
		cout << "C" << endl;
	}
	else if (mark >= 80 && mark < 90)
	{
		cout << "B" << endl;
	}
	else if (mark >= 90)
	{
		cout << "A" << endl;
	}
}
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
Something like this may be more appropriate:
#include<iostream>

using namespace std;

int main()

{
	float mark;

	cout << "Enter mark for letter grade: ";
	cin >> mark;
	
	if(mark < 50)
	{
		cout << "F" << endl;
	}
	else if(mark >= 50 && mark < 60 )
	{
		cout << "P" << endl;
	}
	else if (mark >= 60 && mark < 70)
	{
		cout << "D" << endl;
	}
	else if (mark >= 70 && mark < 80)
	{
		cout << "C" << endl;
	}
	else if (mark >= 80 && mark < 90)
	{
		cout << "B" << endl;
	}
	else //small change, if we reached this point, we can assume the grade is A, no need to test for it.
	{
		cout << "A" << endl;
	}
}


Minor change.
closed account (zT4NhbRD)
Thanks for the suggestions. I'll try them out. It does actually compile and run but I was having some issues with certain inputs like I said.

My instructor indicated that no hard coding values were to be used in the program and to use variables as much as possible -- especially for the grade values. It ran a lot better when I used values but alas, I'm not allowed to.

Thanks again, guys.
Instructor wrote:
My instructor indicated that no hard coding values were to be used in the program and to use variables as much as possible -- especially for the grade values.


That's good advice.

@Mr D

It's also a good idea to use the else part of an if else chain to catch errors. In this case if the input is > 100 Or < 0.

34
35
36
37
	else //small change, if we reached this point, we can assume the grade is A, no need to test for it.
	{
		cout << "A" << endl;
	}


Try this:

14
15
16
17
	if(mark < 50 && mark > 0)
	{
		cout << "F" << endl;
	}


34
35
36
37
38
39
40
41
42
	else if (mark >= 90 && mark <= 100) //use variables for the 90 and 100 and the others elsewhere
	{
		std::cout << "A\n";
	}

        else // mark is negative or > 100
        {
               std::cout << "Error, Input out of range.\n";
        }


Although an even better idea is to validate the input before doing any other code that uses it. Validating data is a very important concept in coding. Despite doing that, it is still a good idea to use the else as error check anyway.

@skyllianfive
Another thing: if you find yourself writing the same code over and over (as in your OP) then it can be done better.

Try to avoid having using namespace std; Google to see why that is so, and what to do instead.

Hope all is well :+)
Last edited on
Topic archived. No new replies allowed.