so im trying to practice writing a few programs that might have to do with my upcoming test soon. in this program i wanted the user to enter a test score and it couts the letter grade. doesnt work properly it couts the test score instead of the letter grade. help?

#include <iostream>
using namespace std;
void inputScore(int* s);
bool isValidScore(int);
void processScore(int* c);

int main()
{
	int average;
	inputScore(&average);
	cout <<"Your Grade is:" << average << endl;
	system("pause");
	return 0;
}

void inputScore(int* s)
{
	bool result;
	do {
	cout <<"Enter Average :";
	cin >> *s;
	result = isValidScore(*s);
	} while(result == false);
}

bool isValidScore (int x)
{

if (x >= 0 && x <= 100)
return true;
else
{
	cout <<"Invalid Score" << endl;
	return false;
}
}

void processScore(int* c)
{
	if (*c >= 90 && *c <= 100)
	{
		cout <<"A" << endl;
	}
	else if (*c >=80 && *c <= 89)
	{
		cout <<"B" << endl;
	}
	else if (*c >=79 && *c <=70)
	{
		cout <<"C" << endl;
	}
	else if (*c >= 69 && *c <=60)
	{
		cout << "D" << endl;
	}
	else if (*c >= 59 && *c <=0) 
	{
		cout << "F" << endl;
	}
}

Perhaps I missed something, but I can't find where you are calling the processScore function. And this is where the number grade is converted to a letter grade. Please correct me if I am wrong.

You may want to change the

cout <<"Your Grade is:" << average << endl;

with the processScore function. average is an integer, so it would output an numerical value rather than the character you want.

i changed a few things but i guess thats not correct

"char average;"
"processScore(&average);"

is this part right "void processScore(int* c)" or does it need to be change to void processScore(char* c) instead?

am i even close on getting a successful build? -__-

You may want to change the function from void to char and have it return the char value of the grade. Pass the numeric value of the grade as the parameter. Try that. I'm not positive that it will work but I definitely think its worth a shot. Let me know how it goes.

soemthing like this

#include <iostream>

using namespace std;
void inputScore(int* s);
bool isValidScore(int);
char processScore(int* c);

int main()
{
	int average;
	processScore(&average);
	cout <<"Your Grade is:" << average << endl;
	system("pause");
	return 0;
}

void inputScore(int* s)
{
	bool result;
	do {
	cout <<"Enter Average :";
	cin >> *s;
	result = isValidScore(*s);
	} while(result == false);
}

bool isValidScore (int x)
{

if (x >= 0 && x <= 100)
return true;
else
{
	cout <<"Invalid Score" << endl;
	return false;
}
}

char processScore(int* c)
{
	if (*c >= 90 && *c <= 100)
	{
		cout <<"A" << endl;
	}
	else if (*c >=80 && *c <= 89)
	{
		cout <<"B" << endl;
	}
	else if (*c >=79 && *c <=70)
	{
		cout <<"C" << endl;
	}
	else if (*c >= 69 && *c <=60)
	{
		cout << "D" << endl;
	}
	else if (*c >= 59 && *c <=0) 
	{
		cout << "F" << endl;
	}
	return *c;
}

it builds but it doesnt run properly lol

Some issues with your code. I would pass by reference for iputScore function rather than passing in a pointer. Also, isValidScore and processScore don't need a pointer passed in as nothing is being altered only compared, so pass in the values only. For your processScore function, you need to assign a character to variable "c" from integer "average" rather than using cout. Or changing the char to void would be more easier to fix. Make sure you return nothing, not "c". If you decide to use the void processScore, then just call the function, because you don't need cout as it is already implemented in the function.

Most important: you're not getting any input! Call the inputScore function. I'll check back tomorrow if you need more help.

Comments
thanks for all the help

ok so i went back to my original code and changed a few things but still not working grr look! please help me out

#include <iostream>
using namespace std;
void inputScore(int& s);
bool isValidScore(int);
void processScore(char& c);

int main()
{
	int average;
	char letter;
	inputScore(average);
	processScore(letter);
	cout <<"Your Grade is:" << letter << endl;
	system("pause");
	return 0;
}

void inputScore(int& s)
{
	bool result;
	do {
	cout <<"Enter Average :";
	cin >> s;
	result = isValidScore(s);
	} while(result == false);
}

bool isValidScore (int x)
{

if (x >= 0 && x <= 100)
return true;
else
{
	cout <<"Invalid Score" << endl;
	return false;
}
}

void processScore(char& c)
{
	if (c >= 90 && c <= 100)
	{
		cout <<"A" << endl;
	}
	else if (c >=80 && c <= 89)
	{
		cout <<"B" << endl;
	}
	else if (c >=79 && c <=70)
	{
		cout <<"C" << endl;
	}
	else if (c >= 69 && c <=60)
	{
		cout << "D" << endl;
	}
	else if (c >= 59 && c <=0) 
	{
		cout << "F" << endl;
	}
}

You are getting "close, but no cigar".

Here is a hints :

Try changing the prototype of :

void processScore(char& c);

to

char processScore(int& numberGrade);

It accepts a number such as 82, and should return a value associate with the number.
You may use it like this :

int myGrade = 87;
//which maybe prints B or B+, depending on your choice
cout << processScore(myGrade) << endl;

or you can use it like this :

//always initialize
      int average = 0;
      char letterGrade = 0;
	processScore(&average);
      letterGrade = processScore(average);
	cout <<"Your Grade is:" << average << endl;
        cin.get(); //use this rather than system pause;
	return 0;

Edited 7 Years Ago by firstPerson: n/a

n response to ur previous code i modified ur code and its working try this. I m using old version of c++ so plz dont mind that i have changed some correct statements.

#include <iostream.h>
#include<conio.h>
void inputScore(int* s);
int isValidScore(int);
void processScore(int* c);

int main()

{ clrscr();
int average;
inputScore(&average);
cout<<"The score is:"<<average;
cout <<"\nYour Grade is:";
processScore(&average);
getch();
return 0;
}

void inputScore(int* s)
{ int result;
do {
cout <<"Enter Average :";
cin >> *s;
result = isValidScore(*s);
} while(result == 0);

}

int isValidScore (int x)

{ if (x >= 0 && x <= 100)
return 1;
else
{ cout <<"Invalid Score" << endl;
return 0;
}

}


void processScore(int* c)

{
int t=*c;
if (t >= 90 && t<=100)

{ cout <<"A" << endl;
}

else if (t >=80 && t <= 89)

{ cout <<"B" << endl;
}

else if (t >=70 && t <=79)

{ cout <<"C" << endl;
}

else if (t >= 60 && t <=69)

{ cout << "D" << endl;
}

else if (t >= 0 && t <=59)

{ cout << "F" << endl;
}

}

It also appears that you have incorrect ranges for your letter grades (which gurudevashu has fixed in his revised code above):

else if (c >=79 && c <=70)	
{cout <<"C" << endl;}	
else if (c >= 69 && c <=60)	
{cout << "D" << endl;}	
else if (c >= 59 && c <=0) 	
{cout << "F" << endl;}

The way you have that set right now is that if c is greater than or equal to 79 or less than or equal to 70 then the grade is C, same with D and F. I think you want the following:

else if(c<=79 && c>=70)
{
     cout << "C" << endl;
}
else if(c<=69 && c>=60)
{
     cout << "D" << endl;
}
else if(c<=59 && c>=0)
{
     cout << "F" << endl;
}

That way C falls between 70 - 79, D falls between 60 - 69, and F falls between 0 - 59.

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