Thread: How to do this?

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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";
    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

  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
    28,413
    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.
    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

  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

Popular pages Recent additions subscribe to a feed