954,173 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

c++ Grade program

I need help with this program. here is what we are supposed to do.
The program should prompt the user to 'Please enter numeric grade: '.

The program will then pass the numeric grade to a function that will convert to a letter grade. This function should have the following prototype:

char get_letter_grade (int grade_num);

100-90 A

89-80 B

79-70 C

69-60 D

below 60 F

4. The character that is returned will then be sent to a function to determine if the letter grade is passing (grades higher than 59 are passing). This function should have the following prototype:

bool is_passing(char grade_letter);

5. Your program should then display the results. For example:

Please enter numeric grade: 83

The letter grade is: B.
The grade is passing.

Here is what i have so far.

#include
#include

using namespace std;

int main()
{
int grade_num;

cout<< "Please enter numeric grade:"<< endl;
cin>> grade_num;
return 0;
}

char get_letter_grade (int grade_num)

{
if (grade_num<=60);
cout<< "The letter grade is F"<< endl;
else if ( grade_num<=69);
cout<< "The letter grade is D"<< endl;
else if (grade_num<=79);
cout<< "The letter grade is C"<< endl;
else if (grade_num<=89);
cout<< "The letter grade is B"<< endl;
else (grade_num<=100);
cout<< "The letter grade is A"<< endl;
return (grade_num);
}

bool is_passing(char grade_letter)

{

sutty8303
Newbie Poster
4 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

Please use code tags
Remeber you are working with a specific range so you are going to need to use the && in your get letter grade function. Also that function needs some other adjustments. You also need to call it from main. Did you even go to class?

prog-bman
Junior Poster
109 posts since Nov 2004
Reputation Points: 14
Solved Threads: 4
 

the answer to this is in my old C++ book, it has the same questions as an example

i don't have to book with me, but i'll try to put it up tomorrow

Fasola
Junior Poster
188 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 

This solution won't work for you, but it should give you a push in the right direction:

#include <iostream>

using namespace std;

char letter_grade(int grade_num)
{
  char limit[][4] = {90,80,70,60,'A','B','C','D'};

  for (int i = 0; i < 4; i++) {
    if (grade_num >= limit[0][i])
      return limit[1][i];
  }

  return 'F';
}

bool is_passing(char grade_letter)
{
  return grade_letter < 'F'; // Portability is for wusses
}

int main()
{
  for (int i = 0; i < 101; i++) {
    cout<< i <<" = "<< letter_grade(i) <<": ";
    cout<< boolalpha << is_passing(letter_grade(i)) <<endl;
  }
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I'd like to point out that these solutions are no better or worse than anyone elses, but rather a demonstration of different ways to solve the problem and still get the same result.

char get_letter_grade (int grade_num) 
{
char Result;
 
switch (grade_num / 10)
	{
	case 10:
	case 9:
	 Result = 'A';
	 break;
 
	case 8:
	 Result = 'B';
	 break;
 
	case 7:
	 Result = 'C';
	 break;
 
	case 6:
	 Result = 'D';
	 break;
 
	default:
	 Result = 'F';
	}
 
return Result;
}

or Needs tweeking if grade is 100 or less then 60

char get_letter_grade (int grade_num)
 
{
return ((10 - (Grade / 10)) + 64);
}
Tight_Coder_Ex
Posting Whiz in Training
215 posts since Feb 2005
Reputation Points: 47
Solved Threads: 17
 

Thanks for all of the help. We get little to no help from the TA's in our classes and yes i do go to class, it's hard to learn when they do not teach what you need to know.

sutty8303
Newbie Poster
4 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

You realize you're paying to be taught, right? Don't just sit there and whine about the instructors not doing their part, make them do it.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
You realize you're paying to be taught, right? Don't just sit there and whine about the instructors not doing their part, make them do it.

ONE HELL OF A POINT...i'll have to remember that "intensely" if i get accepted to grad school

Fasola
Junior Poster
188 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 
Thanks for all of the help. We get little to no help from the TA's in our classes and yes i do go to class, it's hard to learn when they do not teach what you need to know.

int main(){

int grade;

grade = 0;

cout << "Enter grade: ";
cin >> grade;

{
if (grade >= 90)
cout << "A";
else if (grade >= 80)
cout << "B";
else if (grade >=70)
cout << "C";
else if (grade >= 60)
cout << "D";
else
cout << "F";
}
return 0;
}

i don't know if that will work, its been way to long since writing code

Fasola
Junior Poster
188 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 

>i don't know if that will work
So run it and see. If you aren't confident that your code is 100% correct, don't write directly to the thread. Write the code in an editor, compile it, run it, debug it, and make sure it's working like you expect before posting it.

To this day, I still test my code before posting it 9 times out of 10. I'm confident that it's flawless (though part of that is ego), but I still would rather double and triple check myself than have somebody else correct a stupid mistake.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
>i don't know if that will work So run it and see. If you aren't confident that your code is 100% correct, don't write directly to the thread. Write the code in an editor, compile it, run it, debug it, and make sure it's working like you expect before posting it.

i use public computers i don't have one of my own :cry:

trust me, if i could i'd be compiling running, and editing aaaaaaall day

*wonders if it would work*

Fasola
Junior Poster
188 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 

>use public computers i don't have one of my own
Blah blah blah, stop making excuses and start finding solutions. Here's one.

>*wonders if it would work*
No, it wouldn't.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
*wonders if it would work* No, it wouldn't.

I beleive you're referring to code. If so, what about it won't work. The only thing I could see in the last snippet was it's not really necessary to enclose the set of conditionals in a statement block.

Tight_Coder_Ex
Posting Whiz in Training
215 posts since Feb 2005
Reputation Points: 47
Solved Threads: 17
 
>use public computers i don't have one of my own Blah blah blah, stop making excuses and start finding solutions. Here's one.

:cheesy:

im currently working on a program to hack into your mind, or at least the part that stores programming knowledge

Fasola
Junior Poster
188 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 
>i don't know if that will work So run it and see. If you aren't confident that your code is 100% correct, don't write directly to the thread. Write the code in an editor, compile it, run it, debug it, and make sure it's working like you expect before posting it.
#include <iostream>

int main(){

int grade;

grade = 0;

std::cout << "Enter grade: ";
std::cin >> grade;

if (grade >= 90)
std::cout << "A";
else if (grade >= 80)
std::cout << "B";
else if (grade >=70)
std::cout << "C";
else if (grade >= 60)
std::cout << "D";
else
std::cout << "F";

return 0;
}


Narue,

I'm sorry i temporarily lost it back there :rolleyes:

thanks for the link, the code above has been compiled successfully, i don't know where i could run a test on it, but its error less.

*still trying to figure out your example*

Fasola
Junior Poster
188 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 

>If so, what about it won't work.
iostream? namespace?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
>If so, what about it won't work. iostream? namespace?


i left that out thinking it was a "given"

when i went to compile it i used and got an error back

*can't believe he's coding again thanks to this site and you*

but anyway, i took the ".h" off and compiled again, another error came up

it didn't recognize "cout" and "cin" saying they wasn't "standard"

i added "std::" (haven't used that in a while) and it compiled successfully

i didn't have to add "using namespace std;" like you did though, wonder why!?!?!?

Fasola
Junior Poster
188 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 

>i left that out thinking it was a "given"
You'd be surprised at how many people won't see it that way. If you post code that could be mistaken for a complete program, make sure it compiles and runs as is.

>i didn't have to add "using namespace std;" like you did though, wonder why!?!?!?
Because you qualified each standard name with std::. There are three ways to qualify names in a namespace. Opening up the entire namespace like I did is one way and handling names on a case by case basis like you did is another. The third way is with a using declaration:

#include <iostream>

// Only allow cout and endl as unqualified names
using std::cout;
using std::endl;

int main()
{
  cout<<"Hello, world!"<<endl;
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

>i didn't have to add "using namespace std;" like you did though, wonder why!?!?!? Because you qualified each standard name with std::. There are three ways to qualify names in a namespace. Opening up the entire namespace like I did is one way and handling names on a case by case basis like you did is another. The third way is with a using declaration:

#include <iostream>

// Only allow cout and endl as unqualified names
using std::cout;
using std::endl;

int main()
{
  cout<<"Hello, world!"<<endl;
}


that's right :!:

i forgot about all that stuff, you're absolutly right, you are definitely a God sent woman

thanx for bring that back to memory

Fasola
Junior Poster
188 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 

Here is what I've created. I have compiled it and it is legit. I'm not sure if this includes what you wanted but feel free to modify it.

#include <iostream>

using namespace std;


int main()
{
    int grade;

    cout << "Input your grade (0-100): ";
    cin >> grade;
    cout << endl;

    if (grade == 100){
       cout << "You got a perfect grade!" << endl;
       cout << "Letter grade: A" << endl;
        }
    else if (grade >=90 && grade <=100){
        cout << "Letter grade: A" << endl << endl;
        }
    else if (grade >=80 && grade <=89){
        cout << "Letter grade: B" << endl << endl;
        }
    else if (grade >=70 && grade <=79){
        cout << "Letter grade: C" << endl << endl;
        }
    else if (grade >=60 && grade <=69) {
        cout << "Letter grade: D" << endl << endl;
        }
    else if (grade < 60) {
        cout << "Letter grade: F" << endl << endl;
        }
    else {
        cout << "Invalid grade!" << endl;
    }

    system("pause");
    return 0;
}
vtothau
Newbie Poster
2 posts since May 2010
Reputation Points: 5
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You