Like Tree3Likes
  • 1 Post By laserlight
  • 2 Post By rTeapot

What am I doing wrong?? (simple)

This is a discussion on What am I doing wrong?? (simple) within the C++ Programming forums, part of the General Programming Boards category; Ok so I am quite sure everyone will find this stupidly simple but I am trying to learn C++ and ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    19

    Angry What am I doing wrong?? (simple)

    Ok so I am quite sure everyone will find this stupidly simple but I am trying to learn C++ and am still in the MOST basic steps, just made it to Loop section in Jumping Into C++ E-book. Anyways here is my question that I need to answer (I am stuck on a practice problem):

    Write a menu program that lets the user select from a list of options, and if the input is not one of the options, reprint the list...

    *pertinent info: it works without the OR operator so essentially if "1" is the only option...

    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    int main()
    {
        string num;
        cout << "Please choose from the options: 1,2,3: ";
        cin >> num;
        while ( (num != ("1")) || (num != ("2")) || (num != ("3")))
        {
            cout << "Wrong option--try again (1,2,3): ";
            cin >> num;
        }
    }
    Thank you in advance to any help received!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    Consider this expression:
    Code:
    (num != ("1")) || (num != ("2")) || (num != ("3"))
    What does it evaluate to if num == "0"? What about num == "1"? What about num == "2"?
    antred 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

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    19
    Quote Originally Posted by laserlight View Post
    Consider this expression:
    Code:
    (num != ("1")) || (num != ("2")) || (num != ("3"))
    What does it evaluate to if num == "0"? What about num == "1"? What about num == "2"?
    0 would result in all being true thus the loop would run again.
    1 would result in (false, true, true) run again.
    2 would result in (true, false, true) run again.

    Darn it ok it always evaluates to true no matter what, I see why that would be a problem, no matter what it will always run :/. I fail. I fixed it this way... Thank you very much for your help!!!

    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    int main()
    {
        string num;
        cout << "Please choose from the options: 1,2,3: ";
        cin >> num;
        while (1)
        {
            if (num == "1" || num == "2" || num == "3")
            {
                break;
            }
            else
            {
                cout << num <<" was not an option please try again.";
                cin >> num;
            }
        }
        cout << "Thank you!";
    }
    antred and laserlight like this.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    While loops run on inverted logic. That is, it loops while the answer is wrong.
    The easiest way to create a complex condition is IMO to think about when a loop must end, then invert that logic.
    Similarly, when reading a condition, invert it first and then you know when it will end. Give it a try.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    19
    Thanks Elysia!

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,012
    Quote Originally Posted by rTeapot View Post
    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    int main()
    {
        string num;
        cout << "Please choose from the options: 1,2,3: ";
        cin >> num;
        while (1)
        {
            if (num == "1" || num == "2" || num == "3")
            {
                break;
            }
            else
            {
                cout << num <<" was not an option please try again.";
                cin >> num;
            }
        }
        cout << "Thank you!";
    }
    This is more simply written as:
    Code:
    while ( !(num == "1" || num == "2" || num == "3") )
        {
            cout << num <<" was not an option please try again.";
            cin >> num;
        }
    To simplyfy it further, you can apply DeMorgan’s Rules for distributing the negation:
    Code:
    while ( num != "1" && num != "2" && num != "3" )
        {
            cout << num <<" was not an option please try again.";
            cin >> num;
        }
    Which literally reads "while num is not equal to 1, and num is not equal to 2, and num is not equal to 3".
    Last edited by King Mir; 06-17-2012 at 05:11 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    5,884
    Personally, I'd reduce it even further
    Code:
    while (num.size() == 1 && num[0] >= '1' && num[0] <= '3')
    True, this takes slightly more typing, but it offers other advantages such readability (arguably more clear that it's testing for input in a range) and being more easily extended to more than 3 cases.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Registered User
    Join Date
    Jun 2012
    Posts
    19
    Thank you everyone for your quick replies and help! To grumpy and King Mir, thank you for showing me how you would shorten it, I feel like writing shorter code comes with more experience because currently I find it really hard to initially write that way... I have to write it the long way it seems and then try to think of ways to shorten it... Like the one below it gets the job done but its much longer than it should be, at least I have a feeling it can be shortened a lot haha, I will keep working at it...

    1. Take the "menu program" you wrote earlier and break it out into a series of calls to functions for
    each of the menu items. Add the calculator and "100 bottles of beer" as two different functions
    that can be called.
    2. Make your calculator program perform computations in a separate function for each type of
    computation.

    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    double add (double x, double y)
    {
        return x + y;
    }
    double mult (double x, double y)
    {
        return x * y;
    }
    double sub (double x, double y)
    {
        return x - y;
    }
    double div (double x, double y)
    {
        return x / y;
    }
    
    
    string selection;
    
    
    int main()
    {
        while (selection != "Calculator" && selection != "99")
        {
            cout << "Please pick a selection from the following menu by typing it out and hitting enter:" <<endl;
            cout << "Calculator" <<endl;
            cout << "99" <<endl;
            cin >> selection;
        }
    
    
        if (selection == "Calculator")
        {
            string op;
            double x;
            double y;
    
    
            cout << "Please enter an arithmetic operator (+,-,*,/): ";
            cin >> op;
    
    
            cout << "Please enter a number: ";
            cin >> x;
    
    
            cout << "Please enter another number: ";
            cin >> y;
    
    
            if ( op == "+")
            {
                cout << "The sum is: " << add(x,y);
            }
            if ( op == "-")
            {
                cout << "The difference is: " << sub(x,y);
            }
            if ( op == "*")
            {
                cout << "The product is: " << mult(x,y);
            }
            if ( op == "/")
            {
                cout << "The dividend is: " << div(x,y);
            }
        }
    
    
        if (selection == "99")
        {
            for ( int beer = 99; beer > 0; beer--)
            {
            cout << beer << " Bottles of Beer on the wall, " << beer << " Bottles of Beer, take one down and pass it around, " << beer - 1 << " Bottles of Beer on the wall." <<endl;
            }
        }
    }
    Last edited by rTeapot; 06-18-2012 at 03:36 PM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    Personally, I don't think the add, mult, sub and div functions help. You might as well use the operators directly in main. These functions could help if you map the strings (e.g., "+") to the function pointers and then do a map lookup to perform the operation, but that might be a little more advanced than what you intend right now.

    Instead, I suggest moving the entire calculation section to another function. Also, since op cannot be "+" and "-" at the same time, it makes sense to use an if/else chain instead of just a series of if statements.
    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

  10. #10
    Registered User
    Join Date
    Jun 2012
    Posts
    19
    Thanks for the input laserlight, I agree that the way I did it was a bit bulky but I am a little new to this so that's what I came up with, still working at getting my writing minimalistic and streamlined...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with my simple C code?
    By godmoney in forum C Programming
    Replies: 20
    Last Post: 01-03-2011, 09:19 AM
  2. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  3. what is wrong with this simple program?
    By Dave Jay in forum C Programming
    Replies: 3
    Last Post: 03-07-2005, 08:47 PM
  4. what is wrong with this simple prg??
    By ryan_qut in forum C Programming
    Replies: 7
    Last Post: 11-15-2004, 09:21 AM
  5. Simple Code Gone Wrong
    By mrlucky0 in forum C Programming
    Replies: 11
    Last Post: 06-20-2003, 07:47 AM

Tags for this Thread


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