need help in grading program

hello i am really confuse with this. i v tried to do my possible best but not working.i am a newbie in programming just trying to do ma first program.
here is the question.

Write a program that allows the user to enter the grade scored in a programming class (0-100).
If the user scored a 100 then notify the user that they got a perfect score.

★ Modify the program so that if the user scored a 90-100 it informs the user that they scored an A

★★ Modify the program so that it will notify the user of their letter grade
0-59 F 60-69 D 70-79 C 80-89 B 90-100 A"

and here is what i did so far.

// t.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

void grade() {


cout << "you got it the perfect score: " << endl;

};



int main ()

{

int a,b,c,d,f;
int totalgrade = 100;
a = 90;
b = 80;
c = 70;
d = 60;
f = 59;

int result;
cout << "please enter your score to show your grade: ";
cin >> result;

if ((a >= 90) || (result == 99)) {
string score = "A" ;
cout << "your grade is " << score << endl ;

}

else if ((result <= b) || (result == 89)) {

string score2 = "B";
cout << "your grade is " << score2;
so my problem is that if i input 82 as the score, it still show me 'A' as the grade i dont know how to nest it anymore. help me out pls
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
        int score;
	
	cout << "Enter your score: ";
	cin >> score;

	if(score == 100) 
		cout << "You got a perfect score!";
	   else if(score >= 90 && score < 100)
		cout << "\nYou got an A";
	   else if(score >= 80 && score < 90)
		cout << "\nYou got a B";
	//...
	   else // to catch anything unexpected
		cout << "\nScore out of range";


Your instructions imply that you already had some code to begin with, but the above should be more than enough to get you started. Does any of that look familiar/make sense?
Last edited on
Edit on above. If you have else if statements, then you only need to have the lower number. It will not read any other else or else if statements if one is triggered, so you only need to put it from highest to lowest and use the bottom score to determine if they made the grade, but that's just optimization, otherwise the code by cantide5ga is perfectly correct.

My suggestion only works if you clear grades that are above 100 in the first if statement though, so it's not perfect.
thanks cantide5ga u really put me through out the whole error. and GRex2595 am so grateful. here is the code now.

// ade.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
int score;

cout << "Enter your score: ";
cin >> score;

if(score == 100)
cout << "You got a perfect score!";
else if (score >= 90 && score < 100)
cout << "You got an A\n";
else if (score >= 80 && score < 90)
cout << "You got a B\n";
else if (score >= 70 && score < 80)
cout << "You got a C\n";
else if (score >= 60 && score < 70)
cout << "You got a D\n";
else if (score >= 0 && score < 60)
cout << "You got a F\n";

else
cout << "Score out of range\n";



system ("pause");
return 0;
}
Last edited on
Glad I could help, just make sure to follow GRex's advice and edit my example as necessary; it's a good tip and my redundancy was unneeded.

I originally thought this would be perfect with using a switch, but remembered that in C, the switch operates on labels and does not support ranges. Other languages I use do allow this. The conversion to if statements left some unnecessary junk behind!
Thanks that really helped!
I still have one problem, after debugging, when the window opens i enter a number and press enter. Everything works but next to the result is a sentence saying "Press any key to continue" how do i remove it? or move it to a different line?
system("pause"); is a function that takes in a keypress to continue to the next line of code. You just need to check all your outputs and make sure they have a newline character '\n' or and endline in them to make sure that the "Press any key to continue . . ." is on a different line. Otherwise, you could just remove it.
Topic archived. No new replies allowed.