Like Tree1Likes

How to do this?

This is a discussion on How to do this? within the C++ Programming forums, part of the General Programming Boards category; Sorry, taking a closer look at your output, you have a special case where for A, you print an exclamation ...

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Sorry, taking a closer look at your output, you have a special case where for A, you print an exclamation mark. As such, the way that I would solve this is to write a function, say grade_modifier, that takes the grade as an int and returns the modifier as a std::string.

    This way, you can write stuff like:
    Code:
    std::cout << "You scored an A" << grade_modifier(grade) << "!\n";
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #32
    Registered User ferfy's Avatar
    Join Date
    Nov 2011
    Location
    Michigan
    Posts
    22
    Or i could just remove the exclamation mark :P hehe

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Quote Originally Posted by ferfy
    Or i could just remove the exclamation mark
    Yeah, that would make things easier. In that case, I would write the program like this:
    Code:
    #include <iostream>
    
    int main()
    {
        std::cout << "Enter your grade %: ";
        int grade;
        std::cin >> grade;
    
        if (grade <= 60)
        {
            std::cout << "You scored an F";
        }
        else if (grade <= 70)
        {
            std::cout << "You scored a D";
        }
        else if (grade <= 80)
        {
            std::cout << "You scored a C";
        }
        else if (grade <= 90)
        {
            std::cout << "You scored a B";
        }
        else
        {
            std::cout << "You scored an A";
        }
    
        if (grade > 60)
        {
            int last_digit = grade % 10;
            if (last_digit > 0 && last_digit <= 3)
            {
                std::cout << '-';
            }
            else if (last_digit > 7 || last_digit == 0)
            {
                std::cout << '+';
            }
        }
        std::cout << std::endl;
    }
    Notice the check for grade > 60. Without that, you'll be incorrectly printing the modifier for F.
    ferfy likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #34
    Registered User ferfy's Avatar
    Join Date
    Nov 2011
    Location
    Michigan
    Posts
    22
    Quote Originally Posted by laserlight View Post
    Yeah, that would make things easier. In that case, I would write the program like this:
    Code:
    #include <iostream>
    
    int main()
    {
        std::cout << "Enter your grade %: ";
        int grade;
        std::cin >> grade;
    
        if (grade <= 60)
        {
            std::cout << "You scored an F";
        }
        else if (grade <= 70)
        {
            std::cout << "You scored a D";
        }
        else if (grade <= 80)
        {
            std::cout << "You scored a C";
        }
        else if (grade <= 90)
        {
            std::cout << "You scored a B";
        }
        else
        {
            std::cout << "You scored an A";
        }
    
        if (grade > 60)
        {
            int last_digit = grade % 10;
            if (last_digit > 0 && last_digit <= 3)
            {
                std::cout << '-';
            }
            else if (last_digit > 7 || last_digit == 0)
            {
                std::cout << '+';
            }
        }
        std::cout << std::endl;
    }
    Notice the check for grade > 60. Without that, you'll be incorrectly printing the modifier for F.


    yeah that's what i was gonna do.... but i've been up all night and it's 6am so i can't be bothered to revise it again tonight :/

    but thanks again for your help

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21