Having trouble with this assignment can anybody give my a hint as to where I should start?


Write a program that prints letter grades based on a score (between 0 and 10) entered from the keyboard.
The simplified scoring system will be:
9-10 A
8 B
7 B-
6 C+
5 C
4 D+
0-3 F

Write two functions: if_grade and switch-grade. Both will take the integer score as input and print the letter grade. The if_grade function will accomplish this with if-else, the switch with a switch statement.

string if_grade(int grade)
{
if (grade >= 9 && < 10)
return 'A';
else if (grade == 8)
return 'B';

[etc...]
}

string switch-grade(int grade)
{
switch(grade)
{
case '10':
return 'A';
case '9':
return 'A';
case '8':
return 'B';

[etc...]
}
}

that should be enogh help

So I got this far but I'm missing something simple I can't get the program to print B-, C+, C- and D+. If I put in B- It just print - (minus) like I have in my original question.

#include <iostream> 
using namespace std; 

char if_grade(int Num) 
{ 
char Grade; 
if ((Num < 0) || (Num > 10))
Grade = 'N';
else if (Num >= 9)
Grade = 'A';
else if (Num >= 8)
Grade = 'B';
else if (Num >= 7)
Grade = 'B';
else if (Num >= 6)
Grade = 'C';
else if (Num >= 5)
Grade = 'C'; 
else if (Num >= 4)
Grade = 'D';
else
Grade = 'F';
return(Grade);
}

char switch_grade(int Num) 
{ 
char grade;
switch(Num)
{
case 0: 
case 1: 
case 2: 
case 3: 
case 4: 
case 5: 
grade = 'F'; 
break; 
case 6: 
grade = 'D'; 
break; 
case 7: 
grade = 'C'; 
break; 
case 8: 
grade = 'B'; 
break; 
case 9: 
case 10: 
grade = 'A';
break; 
}
return(grade);
}

int main() 
{ 
int number; 
cout << " Please enter an integer from 0-10" << endl; 
cin >> number; 
char grades = if_grade(number); 
char switch_grades= switch_grade(number); 
cout << " The grade is: " << grades << endl; 
system("pause");
return 0;
}

change char to string, and add the + and - in the quotes

Hmm I changed everything to string and it's still print just - or +

#include <iostream> 
using namespace std; 
string if_grade(int Num) 
{ 
string Grade; 
if ((Num < 0) || (Num > 10))
Grade = 'N';
else if (Num >= 9)
Grade = 'A';
else if (Num >= 8)
Grade = 'B-';
else if (Num >= 7)
Grade = 'B';
else if (Num >= 6)
Grade = 'C+';
else if (Num >= 5)
Grade = 'C'; 
else if (Num >= 4)
Grade = 'D+';
else
Grade = 'F';
return(Grade);
}

string switch_grade(int Num) 
{ 
string grade;
switch(Num)
{
case 0: 
case 1: 
case 2: 
case 3: 
case 4: 
case 5: 
grade = 'F'; 
break; 
case 6: 
grade = 'D'; 
break; 
case 7: 
grade = 'C'; 
break; 
case 8: 
grade = 'B'; 
break; 
case 9: 
case 10: 
grade = 'A';
break; 
}
return(grade);
}
int main() 
{ 
int number; 
cout << " Please enter an integer from 0-10" << endl; 
cin >> number; 
string grades = if_grade(number); 
string switch_grades= switch_grade(number); 
cout << " The grade is: " << grades << endl; 
system("PAUSE");
return EXIT_SUCCESS;
return 0;	
}

if you work with strings I think you need to put the string into " " quotation marks and not '.

Bah he failed my code I'm not sure what is wrong but I hae been given another chance to amend it. I really donb't know what the problem is maybe someone can tell me?

These are his comments:
your program only prints the letter equivalent for the if method, but not for the switch method.

Assuming you put quotation marks around the strings the only thing I see missing is the output at the end.

cout << " The grade is: " << grades << endl;

This only gives you the string from the if-method.

cout << " The grade is (if-method): " << grades << "       The grade is (switch-method): " << switch_grades   << endl;

I'd be willing to help if I could read your code. You need to format it so all of us can see what you are doing -- including you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.