Thread: How to do this?

  1. #1
    Registered User ferfy's Avatar
    Join Date
    Nov 2011
    Location
    Michigan
    Posts
    22

    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?

  2. #2
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Maybe:
    Code:
    if(intervall){
       if(subintervall){
          //print a + sign
       } else if (other subintervall){
          //- sign
       } else {
          //nottin
       }

  3. #3
    Registered User ferfy's Avatar
    Join Date
    Nov 2011
    Location
    Michigan
    Posts
    22
    Quote Originally Posted by überfuzz View Post
    Maybe:
    Code:
    if(intervall){
       if(subintervall){
          //print a + sign
       } else if (other subintervall){
          //- sign
       } else {
          //nottin
       }

    no idea what intereval and subinterval are

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    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.
    }
    }

  5. #5
    Registered User ferfy's Avatar
    Join Date
    Nov 2011
    Location
    Michigan
    Posts
    22
    Quote Originally Posted by überfuzz View Post
    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

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Something like this:
    Code:
               int lastDigit = grade % 10;
               char sign = '';
               if (lastDigit == 9 || lastDigit == 0)
                   sign = '+';
               else if (lastDigit <= 3)
                   sign = '-';
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User ferfy's Avatar
    Join Date
    Nov 2011
    Location
    Michigan
    Posts
    22
    Quote Originally Posted by oogabooga View Post
    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?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by ferfy View Post
    yeah that'd work, but how would u implement lastdigit?
    I don't understand what you're asking.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User ferfy's Avatar
    Join Date
    Nov 2011
    Location
    Michigan
    Posts
    22
    Quote Originally Posted by Elysia View Post
    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 View Post
    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?

  11. #11
    Registered User ferfy's Avatar
    Join Date
    Nov 2011
    Location
    Michigan
    Posts
    22
    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) }
    
    
    }

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by ferfy View Post
    lastdigit is a f'ing variable
    I still don't understand. What's a f'ing?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  14. #14
    Registered User ferfy's Avatar
    Join Date
    Nov 2011
    Location
    Michigan
    Posts
    22
    Quote Originally Posted by laserlight View Post
    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?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed