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.

Dani AI

Generated

The two big gotchas in your thread are (1) using single quotes for multi-character grade labels and (2) treating case labels as characters. As suggested, use strings, but use double quotes: "A", "B-", etc. A literal like 'B-' is a multi-character constant of type int; when it gets converted to a char/string you often end up with only the last character, which is why you saw just '-'. And as noted, print both results so your grader sees that the switch version works too. Also, case labels in a switch on int must be integers (10), not character constants ('10').

Here is a compact, consistent mapping that matches your rubric and returns strings (easy to test). If your assignment requires printing inside the functions, replace the returns with cout statements.

#include <string>
using std::string;

string grade_if(int s) {
    if (s < 0 || s > 10) return "Invalid";
    if (s >= 9) return "A";
    if (s == 8) return "B";
    if (s == 7) return "B-";
    if (s == 6) return "C+";
    if (s == 5) return "C";
    if (s == 4) return "D+";
    return "F";
}

string grade_switch(int s) {
    switch (s) {
        case 10: case 9:  return "A";
        case 8:           return "B";
        case 7:           return "B-";
        case 6:           return "C+";
        case 5:           return "C";
        case 4:           return "D+";
        case 3: case 2: case 1: case 0: return "F";
        default:          return "Invalid";
    }
}

Quick checklist:

  • Include <string> and use double quotes for string literals.
  • Use an integer switch with a default to catch out-of-range inputs.
  • In main, display both results (if-method and switch-method) side by side for the same input so it is obvious both work. Test with -1, 0, 4, 5, 6, 7, 8, 9, 10, and 11.

Recommended Answers

All 9 Replies

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.