# How to do this?

Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last
• 03-22-2012
ferfy
How to do this?
Code:

```#include <iostream>     int main()     {             int grade;             std::cout << "Enter your grade %: ";           std::cin >> grade;             if (grade <= 60) {                   std::cout << "You scored an F\n"; }             if (grade > 60 && grade <= 70) {                   std::cout << "You scored a D\n"; }             if (grade > 70 && grade <= 80) {                   std::cout << "You scored a C\n"; }           if (grade > 80 && grade <= 90) {                   std::cout << "You scored a B\n"; }           if (grade > 90 && grade <= 100) {                   std::cout << "You scored an A!\n"; }     }```
Modify the previous program to print out a + or - after the letter grade based on the last digit of the score.
1-3: -
4-7: blank
9-0: +

for instance:
I mean like say it's the grade is 33, it'd be an F-
say the grade is 35, it'd be an F
say the grade is 39, it'd be an F+

say the grade is 73, it'd be an C-
say the grade is 75, it'd be a C
say the grade is 70, it'd be a C+

do i need to use a character array to do this? with strcat? or? and is there another way of doing it or?
• 03-22-2012
überfuzz
Maybe:
Code:

```if(intervall){   if(subintervall){       //print a + sign   } else if (other subintervall){       //- sign   } else {       //nottin   }```
• 03-22-2012
ferfy
Quote:

Originally Posted by überfuzz
Maybe:
Code:

```if(intervall){   if(subintervall){       //print a + sign   } else if (other subintervall){       //- sign   } else {       //nottin   }```

no idea what intereval and subinterval are
• 03-22-2012
überfuzz
Code:

```if (grade > 60 && grade <= 70) {                   std::cout << "You scored a D\n"; if(grade > 60 && grade <= 63){   std::cout "-"; } else if(grade > 67 && grade <= 70){   std::cout "+"; } else {   // nottin. } }```
• 03-22-2012
ferfy
Quote:

Originally Posted by überfuzz
Code:

```if (grade > 60 && grade <= 70) {                   std::cout << "You scored a D\n"; if(grade > 60 && grade <= 63){   std::cout "-"; } else if(grade > 67 && grade <= 70){   std::cout "+"; } else {   // nottin. } }```

i would have to do that for every last digit...... ........ that, isn't there a way to read the last digit, and place a - or + accordingly
• 03-22-2012
oogabooga
Something like this:
Code:

```          int lastDigit = grade % 10;           char sign = '';           if (lastDigit == 9 || lastDigit == 0)               sign = '+';           else if (lastDigit <= 3)               sign = '-';```
• 03-22-2012
ferfy
Quote:

Originally Posted by oogabooga
Something like this:
Code:

```          int lastDigit = grade % 10;           char sign = '';           if (lastDigit == 9 || lastDigit == 0)               sign = '+';           else if (lastDigit <= 3)               sign = '-';```

yeah that'd work, but how would u implement lastdigit?
• 03-22-2012
Elysia
People, stop handing out solutions.
ferfy: Have you worked out the logic on how to do this?

I am also going to comment that the above code is poorly written since it is an unwritten rule that the closing brace should be on a separate line (unless the first brace is on the same line).
• 03-22-2012
oogabooga
Quote:

Originally Posted by ferfy
yeah that'd work, but how would u implement lastdigit?

I don't understand what you're asking.
• 03-22-2012
ferfy
Quote:

Originally Posted by Elysia
People, stop handing out solutions.
ferfy: Have you worked out the logic on how to do this?

I am also going to comment that the above code is poorly written since it is an unwritten rule that the closing brace should be on a separate line (unless the first brace is on the same line).

if i worked out the logic on how to do it, i wouldn't be asking silly :P

well i know i need to read the last digit of the variable "grade", and then add + or - to the output, depending, but idk how to do that..... i was thinking a character array or a multidimensional array with a bunch of if statements, and using strcat to add the + or -, but i'm not even sure if what i'm saying is possible in order to do what i wanna do.... :(

Quote:

Originally Posted by oogabooga
I don't understand what you're asking.

how are u reading the last digit of "grade" using lastdigit... lastdigit is a f'ing variable, so how is that doing anything at all? in what i want it to do?
• 03-22-2012
ferfy
This is what i got so far... not sure why it's not compiling.... or if it's right :/

Code:

```#include <iostream> #include <string> int main() {         std::string grade;         std::string minus;         std::string plus;         minus = "-";         plus = "+";         std::cout << "Enter your grade %: ";         std::cin >> grade;         if (grade <= 60) {                 std::cout << "You scored an F\n"; }         if (grade > 61 && grade <= 70) {                 std::cout << "You scored a D\n"; }                         if (grade.substr(1, 1) <= 3) {                                 std::strcat (grade, minus) }                         if (grade.substr(1, 1) = 8 || 9 || 0 {                                 std::strcat (grade, plus) }         if (grade > 71 && grade <= 80) {                 std::cout << "You scored a C\n"; }                         if (grade.substr(1, 1) <= 3) {                                 std::strcat (grade, minus) }                         if (grade.substr(1, 1) = 8 || 9 || 0 {                                 std::strcat (grade, plus) }         if (grade > 81 && grade <= 90) {                 std::cout << "You scored a B\n"; }                         if (grade.substr(1, 1) <= 3) {                                 std::strcat (grade, minus) }                         if (grade.substr(1, 1) = 8 || 9 || 0 {                                 std::strcat (grade, plus) }         if (grade > 91 && grade <= 100) {                 std::cout << "You scored an A!\n"; }                         if (grade.substr(1, 1) <= 3 {                                 std::strcat (grade, minus) }                         if (grade.substr(1, 1) = 8 || 9 || 0 {                                 std::cat (grade, plus) } }```
• 03-22-2012
laserlight
There are a number of syntax errors in your code. Your compiler should be reporting errors. What are the errors? THese error messages include the line number where the error was detected. Make use of this line number.

Also, you need to indent your code properly. For example, this snippet:
Code:

```        if (grade > 61 && grade <= 70) {                 std::cout << "You scored a D\n"; }                         if (grade.substr(1, 1) <= 3) {                                 std::strcat (grade, minus) }                         if (grade.substr(1, 1) = 8 || 9 || 0 {                                 std::strcat (grade, plus) }```
would be better written as:
Code:

```    if (grade > 61 && grade <= 70) {         std::cout << "You scored a D\n";     }     if (grade.substr(1, 1) <= 3) {         std::strcat (grade, minus)     }     if (grade.substr(1, 1) = 8 || 9 || 0 {         std::strcat (grade, plus)     }```
Note that your use of strcat here is a mistake, and that your attempt to compare with 8, 9 and 0 is not done right.
• 03-22-2012
oogabooga
Quote:

Originally Posted by ferfy
lastdigit is a f'ing variable

I still don't understand. What's a f'ing?
• 03-22-2012
ferfy
Quote:

Originally Posted by laserlight
There are a number of syntax errors in your code. Your compiler should be reporting errors. What are the errors? THese error messages include the line number where the error was detected. Make use of this line number.

Also, you need to indent your code properly. For example, this snippet:
Code:

```        if (grade > 61 && grade <= 70) {                 std::cout << "You scored a D\n"; }                         if (grade.substr(1, 1) <= 3) {                                 std::strcat (grade, minus) }                         if (grade.substr(1, 1) = 8 || 9 || 0 {                                 std::strcat (grade, plus) }```
would be better written as:
Code:

```    if (grade > 61 && grade <= 70) {         std::cout << "You scored a D\n";     }     if (grade.substr(1, 1) <= 3) {         std::strcat (grade, minus)     }     if (grade.substr(1, 1) = 8 || 9 || 0 {         std::strcat (grade, plus)     }```
Note that your use of strcat here is a mistake, and that your attempt to compare with 8, 9 and 0 is not done right.

that's a matter of opinion and style, i dont like ur indenting style, i like mine -_-

also, i guess it's because strings cant be processed as numbers... -_-
although i tried a character array cuz they can process numbers, but i guess that doesn't work either b/c they need to be initialized first not read or something.... unless i did something wrong and this would work with a character array? anyone mind clarifying?
• 03-22-2012
laserlight
Quote:

Originally Posted by ferfy
that's a matter of opinion and style, i dont like ur indenting style, i like mine

If you want to get into a style argument with me (and mind you, I'm open to the use of various reasonable styles, as long as there is consistency: in fact, how I styled your code is not my preferred style), kindly state what benefit does your style provide. In particular, explain:
• Why did you consistently forget the terminating semi-colon? Could it be that your closing brace is not well placed?
• Did you intend to do the string concatenation so many times? Could it be that your indentation is misleading?

Quote:

Originally Posted by ferfy
also, i guess it's because strings cant be processed as numbers...

Not in this way, yes. But why guess? I already told you to check your compiler's error messages.

Quote:

Originally Posted by ferfy
although i tried a character array cuz they can process numbers

No, that is not the case. A character array, used as a string, can (and cannot) "process numbers" similiar to how a std::string can (and cannot) "process numbers".

Use std::string if you need a string type. The approach I recommend here is to convert the string into an integer, or simply read as an integer.
Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last