Im in dire help of needing to do the following:

1. Write a function which will take a grade as an argument and validate it for domain (in a valid range). Return true if valid, false if not.
2. Write a function which will take a grade as an argument and return the equivalent GPA value.
3. ALTERNATIVE CHALLENGE: Instead of writing two seperate functions above, write one function which will validate AND convert. This function should PROCESS ONLY. Leave the input/output for main().
4. In main(), prompt the user for a grade and convert it to GPA. Do this in a loop until the user wants to stop. Don't forget to validate.

GRADE EQUIVALENCY GUIDE

Grade Letter GPA Description

90 - 100 A+ 5.0 Outstanding

85 - 89 A 4.5 Outstanding

80 - 84 A- 4.0 Excellent

75 - 79 B+ 3.5 Very Good

70 - 74 B 3.0 Good

65 - 69 C+ 2.5 Pass

60 - 64 C 2.0 Pass

55 - 59 D 1.5 Conditional Pass

50 - 54 D- 1.0 Conditional Pass

Below 50 F 0.0 Fail

Notes:

* Grades are rounded to the nearest whole number before converting to GPA. For example, a grade of 74.6 is GPA 3.5 whereas 74.4 is 3.0.
* Grades must be between 0 and 100.

If anyone can help me, that would be greatly appreciated

Here are two protoypes to get you started.

bool gradeIsValid(char* grade);
int gradeToGPA(char* grade);

Here are two protoypes to get you started.

bool gradeIsValid(char* grade);
int gradeToGPA(char* grade);

so those would be my functions to call upon?

right now I have this:

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])

{
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;

}
    system("PAUSE");
    return EXIT_SUCCESS;
}

Edited 5 Years Ago by Shadbanks001: n/a

Comments
Copying and pasting someone elses work

That looks like you copy pasted it off someone elses post because of all the # sitting between the lines.

That looks like you copy pasted it off someone elses post because of all the # sitting between the lines.

I used theirs as a outline, but forgot to take those symbols off. thank you for noticing

hay for this you could use a simple switch statement it will be less crouded and simpler to use than a thousand if/else statments

i dont know how to write one, and also i still need to add in my function in my code. that wouldnt interfere with me doing so?

I don't think that you can use switch statements with a range of values without typing in each value.

I would just stick with if/else statements for this assignment.

Start off by writing a validGrade() function have that take in an int and return true or false (a bool) then from there you would write a convertToLetterGrade() function and have that take in the grade (which is now valid) and have that function return a string based on the range of values grade falls in.

Edit: since some grades have a + or - by them you cannot just use a char you will need to use a string or char array. But this is C++ so I would use a string.

Edited 5 Years Ago by sfuo: n/a

I don't think that you can use switch statements with a range of values without typing in each value.

I would just stick with if/else statements for this assignment.

Start off by writing a validGrade() function have that take in an int and return true or false (a bool) then from there you would write a convertToLetterGrade() function and have that take in the grade (which is now valid) and have that function return a string based on the range of values grade falls in.

Edit: since some grades have a + or - by them you cannot just use a char you will need to use a string or char array. But this is C++ so I would use a string.

So it would now look like this:

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])

{
int grade;

bool gradeIsValid(char* grade);
int gradeToGPA(char* 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;

}

Your right sorry I misread what he was asking for how ever to find a GPA it would be better and easier for a switch statement than an if/else

Those functions gradeIsValid and gradToGPA; you're supposed to write them and use them. All that other code in your main function should be in those functions.

I was getting confused by the question and I think you were too.

I am pretty sure you are taking in an integer and passing that into a function that is going to return true or false.
If that returns false then ask for another input and repeat.
If it is true then take that number and pass it into a function that returns a double (number with a decimal) based on the range of values that falls between.

I do not think this program requires you to input a letter grade and instead it is just the numerical grade (which makes it simpler and cleaner).

This is what you should have and then you need to write out what the two functions do.

#include <iostream>
using namespace std;

bool validGrade( int ); //prototypes are always outside of main()
double convertGrade( int );

int main()
{
	int grade = 0;
	cout << "Input the students grade (0-100): ";
	cin >> grade;
	if( validGrade(grade) ) //check if the grade is valid
		cout << convertGrade(grade) << endl; //output the GPA
	return 0;
}


bool validGrade( int in )
{
	if( /* check the input against the range needed */ )
		return true; //if it meets the condition above
	return false;
}

double convertGrade( int in )
{
	if( /* */ ) //if it is greater than the highest mark
		return /* GPA value */; //return value of highest mark
	else if( /* */ ) //if it is greater than the second highest mark
		return /* GPA value */; //return GPA of second highest mark
	/*
	keep going until lowest mark
	
	
	
	*/
	return 0.0; //return 0 if is below lowest mark
}

soo thats basically it, then i have to write the code for the functions, and im done?

yeah im having difficulty, in even writing the code for the functions...i'll paste what I have in 1 moment

ALright so below is my updated code, but i having so much errors...where have I gone wrong?

#include <iostream>
using namespace std;

bool validGrade( int ); //prototypes are always outside of main()
double convertGrade( int );

int main()
{
	int grade = 0;
	cout << "Input the students grade (0-100): ";
	cin >> grade;
	if( validGrade(100) ) //check if the grade is valid
		cout << convertGrade(100) << endl; //output the GPA
	return 0;
}

int grade;

bool validGrade( int in )
{

if (validGrade == 100){
               

cout << "You got a perfect grade!" << endl;
cout << "Letter grade: A" << endl;

}
		return true; //if it meets the condition above
	return false;
}

double convertGrade( int in )
{
	if(>=90 && <=100){
cout << "Letter grade: A" << endl << endl; ) //if it is greater than the highest mark
		return 5.0; //return value of highest mark
		cout <<"Outstanding!" << endl;
		cout << "Letter grade: A+" << endl;
	
    else if(>=85 && <=89) //if it is greater than the second highest mark
		return 4.5; //return GPA of second highest mark
		cout << "Outstanding!" << endl;
		cout << "Letter grade: A" << endl;
	
	else if (>=80 && <=84)
	     return 4.0;
         cout << "Excellent!" << endl;
         cout << "Letter grade: A-" << endl;
    else if (>=75 && <=79)
         return 3.5;
         cout << "Very Good!" << endl;
         cout << "Letter grade: B+" << endl;
    else if (>=70 && <=74)
         return 3.0;
         cout << "Good" << endl;
         cout << "Letter grade: B" << endl;
    else if (>=65 && <=69)
         return 2.5;
         cout << "Pass" << endl;
         cout << "Letter grade: C+" << endl;
    else if (>=60 && <=64)
         return 2.0;
         cout << "Pass" << endl;
         cout << "Letter grade: C" << endl;
    else if (>=55 && 59)
         return 1.5;
         cout << "Conditional Pass" << endl;
         cout << "Letter grade: D" << endl;
    else if (>=50 && 54)
         return 1.0;
         cout << "Conditional Pass" << endl;
         cout << "Letter grade: D-" << endl;
         
    else if (>=50)
         return 0.0;
         cout << "Fail" << endl;
         cout << "Letter grade: F" << end l;
	

}

ALright so below is my updated code, but i having so much errors...where have I gone wrong?

By saying there are errors and making us guess what they are...

Edited 5 Years Ago by WaltP: n/a

What do you think this does?

if(>=90 && <=100)

What exactly are you comparing to the values 90 and 100?

if (validGrade == 100)

This is the name of a function. Why are you comparing the name of a function to a numerical value?

Edited 5 Years Ago by Moschops: n/a

What do you think this does?

if(>=90 && <=100)

What exactly are you comparing to the values 90 and 100?

if (validGrade == 100)

This is the name of a function. Why are you comparing the name of a function to a numerical value?

isnt that what your supposed to do?
unless its supposed to be

i am very lost in how to write the function

#1 if you look at the code I posted I spent a bit of time formatting it so that it is pretty easy to read. You need to learn how to use tab/indents otherwise when someone reads your code it will be very difficult for them to figure out what is going on.

The way you have validGrade() right now it will always return true because the way I had it set up for you was like this.

bool validGrade( int in )
{
	if( in >= 0 && in <= 100 )
		return true;
	return false;
}

Notice that in is the variable used to store the variable that was passed into the function. You are using the name of the function for some reason and that is wrong. You check to see if the grade is 100 output some stuff and then right when the if ends it returns true without ever having a condition where it could return false. The code I posted above shows that it checks to see if the grade is between 0 and 100 -- if this is true then return true otherwise it wont run what is inside the if statement and it will just return false.

In your convertGrade() function you have kinda the right idea other than the fact that you are not testing against a variable so you would have to put if( in >=90 && in <= 100 ) to get the desired result. Anything after a return statement that actually gets executed does nothing because return kicks out of the function therefore making that code never run.

Another thing with is with if statements that I noticed with your code.

if( in >=90 && in <=100 ) //this is valid
	return 5.0;

is the same as

if(in >=90 && in <=100) //this is valid
{
	return 5.0;
}

however

if( in >=90 && in <=100 ) //valid but not the result we want
	cout << "This is an A+!" << endl; //this line runs only if the if statement is true
	return 5.0; //this line will run no matter what

is not the same as

if(in >=90 && in <=100) //this is valid
{
	cout << "This is an A+!" << endl; //both lines will only run if the if statement is true
	return 5.0;
	cout << "This line will never run because it is after the return" << endl; //this is a bonus line just to tell you
}

So in other words if you want to keep what you have in covertGrade() you need to use the variable name in the if statement, use {} brackets and put the cout statements above the return statement.

And from the instructions you posted it says there should be no outputs being done within the functions so I would not put in what letter grade you got since he/she is just asking for the GPA not the letter grade anyways.

double convertGrade( int in )
{
	if( in >= 90 )
		return 5.0;
	else if( in >= 85 )
		return 4.5;
	else if( in >= 80 )
		return 4.0;
	else if( in >= 75 )
		return 3.5;
	else if( in >= 70 )
		return 3.0;
	else if( in >= 65 )
		return 2.5;
	else if( in >= 60 )
		return 2.0;
	else if( in >= 55 )
		return 1.5;
	else if( in >= 50 )
		return 1.0;
	return 0;
}

K so I got no errors, but what the program is doing now. Is soon as i input a number, it just closes right after, without telling me what the GPA number is at all. Also any number i place over 100, its still not computing correctly.

I am not sure why this is happening to me, but ive done everyone has told me what to do on the post.

#include <iostream>
using namespace std;

bool validGrade( int ); //prototypes are always outside of main()
double convertGrade( int );

int main()
{
	int grade = 0;
	cout << "Input the students grade (0-100): ";
	cin >> grade;
	if( validGrade(100) ) //check if the grade is valid
		cout << convertGrade(100) << endl; //output the GPA
	return 0;
}

int grade;

bool validGrade( int in )
{
	if( in >= 90 && in <= 100 )
		return true;
	return false;
cout << "You got a perfect grade!" << endl;
cout << "Letter grade: A" << endl;

}

double convertGrade( int in )
{
	if( in >= 90 )
		return 5.0;
	else if( in >= 85 )
		return 4.5;
	else if( in >= 80 )
		return 4.0;
	else if( in >= 75 )
		return 3.5;
	else if( in >= 70 )
		return 3.0;
	else if( in >= 65 )
		return 2.5;
	else if( in >= 60 )
		return 2.0;
	else if( in >= 55 )
		return 1.5;
	else if( in >= 50 )
		return 1.0;
	return 0;
}

K so I got no errors, but what the program is doing now. Is soon as i input a number, it just closes right after, without telling me what the GPA number is at all. Also any number i place over 100, its still not computing correctly.

I am not sure why this is happening to me, but ive done everyone has told me what to do on the post.

#include <iostream>
using namespace std;

bool validGrade( int ); //prototypes are always outside of main()
double convertGrade( int );

int main()
{
	int grade = 0;
	cout << "Input the students grade (0-100): ";
	cin >> grade;
	if( validGrade(100) ) //check if the grade is valid
		cout << convertGrade(100) << endl; //output the GPA
	return 0;
}

int grade;

bool validGrade( int in )
{
	if( in >= 90 && in <= 100 )
		return true;
	return false;
cout << "You got a perfect grade!" << endl;
cout << "Letter grade: A" << endl;

}

double convertGrade( int in )
{
	if( in >= 90 )
		return 5.0;
	else if( in >= 85 )
		return 4.5;
	else if( in >= 80 )
		return 4.0;
	else if( in >= 75 )
		return 3.5;
	else if( in >= 70 )
		return 3.0;
	else if( in >= 65 )
		return 2.5;
	else if( in >= 60 )
		return 2.0;
	else if( in >= 55 )
		return 1.5;
	else if( in >= 50 )
		return 1.0;
	return 0;
}

Its because you have no way to make the program "wait" before it closes.

On windows you can use system("PAUSE"); or you can use cin.get(); for a pause at the end of your program

Its because you have no way to make the program "wait" before it closes.

On windows you can use system("PAUSE"); or you can use cin.get(); for a pause at the end of your program

so at the end of the program, i can just simply input:

system ("pause");
	return 0;

and it should be good?

no luck still the same issue...im about damn near ready to give up on this...this is getting really frustrating

post your entire program, in code tags lol

#include <iostream>
using namespace std;

bool validGrade( int ); //prototypes are always outside of main()
double convertGrade( int );

int main()
{
	int grade = 0;
	cout << "Input the students grade (0-100): ";
	cin >> grade;
	if( validGrade(100) ) //check if the grade is valid
		cout << convertGrade(100) << endl; //output the GPA
	return 0;
}

int grade;

bool validGrade( int in )
{
	if( in >= 90 && in <= 100 )
		return true;
	return false;
cout << "You got a perfect grade!" << endl;
cout << "Letter grade: A" << endl;


}

double convertGrade( int in )
{
	if( in >= 90 )
		return 5.0;
	else if( in >= 85 )
		return 4.5;
	else if( in >= 80 )
		return 4.0;
	else if( in >= 75 )
		return 3.5;
	else if( in >= 70 )
		return 3.0;
	else if( in >= 65 )
		return 2.5;
	else if( in >= 60 )
		return 2.0;
	else if( in >= 55 )
		return 1.5;
	else if( in >= 50 )
		return 1.0;

	system("PAUSE");
	return 0;
}

Put the pause between line 13 and 14. That will pause the program right at the very end so you get to see all the outputs.

This article has been dead for over six months. Start a new discussion instead.