Thread: Chapter 5 practice program 2 difficulties

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    London, Ontario, Canada
    Posts
    13

    Chapter 5 practice program 2 difficulties


    I've been working through the e-book fine, until this program. I can make the menu work fine, as long as you choose an item. Anything off the list won't re-print the menu. It must be a syntax error, yes I'm old school syntax error, which I can't see.

    Any help would be appreciated,

    Dave H.

    Code:
    # include<iostream>
    using namespace std;
    int main()
    {
        int a = 0 ;
        {
            cout<<"\n\n\n";
            cout<<"\t Please choose 1 item.\n\n";
            cout<<"\t 1 : one\n\n";
            cout<<"\t 2 : two\n\n";
            cout<<"\t 3 : three\n";
            cout<<"\t -------------\n";
            cout<<"\t Choice : ";
            cin>>a;
        }  while (a ==1 && a == 2 && a ==3) ;
            {
                if (a == 1)
                {
                    cout<<"\a\n";
                    return 0;
                }
                else if (a == 2)
                {
                    cout<<"\a\a\n";
                    return 0;
                }
                else if (a == 3)
                {
                    cout<<"\a\a\a\n";
                    return 0;
                }
    
    
    
    
            }
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It looks like you're trying to use a "do-while" loop without the "do."

    Code:
    while (a ==1 && a == 2 && a ==3)
    How can 'a' be equal to one AND equal to two AND equal to three?

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    London, Ontario, Canada
    Posts
    13
    So I should change it to:

    Code:
    do
    {
    
    }While (a == 1 || a == 2 || a == 3)


    **************

    Now the code will re-print the menu, but will not react to any of the items?

    Code:
    
    
    Code:
    # include<iostream>
    using namespace std;
    int main()
    {
        int a = 0 ;
        do
        {
            cout<<"\n\n\n";
            cout<<"\t Please choose 1 item.\n\n";
            cout<<"\t 1 : one\n\n";
            cout<<"\t 2 : two\n\n";
            cout<<"\t 3 : three\n";
            cout<<"\t -------------\n";
            cout<<"\t Choice : ";
            cin>>a;
        }  while (a == 1 || a == 2 || a ==3) ;
                if (a == 1)
                {
                    cout<<"\a\n";
                    return 0;
                }
                else if (a == 2)
                {
                    cout<<"\a\a\n";
                    return 0;
                }
                else if (a == 3)
                {
                    cout<<"\a\a\a\n";
                    return 0;
                }
    }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can do a mental exercise. Imagine what happens if you choose a = 1...3. Then imagine what happens when you select anything else. Follow the code. Follow the loop.
    You can also make a flowchart to flow out the logic you want and then turn it into code. It's good for beginners.
    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.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Location
    London, Ontario, Canada
    Posts
    13
    Thanks for the insight. I should try to make a flowchart, never thought of it before.

    I'll re-post a reply if and when I conclude this error.

    All the best,

    Dave H

  6. #6
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    I presume you are doing this:

    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
    From one beginner to another, I would try looking at it this way:

    Code:
        do
        {
            cout<<"\n\n\n";
            cout<<"\t Please choose 1 item.\n\n";
            cout<<"\t 1 : one\n\n";
            cout<<"\t 2 : two\n\n";
            cout<<"\t 3 : three\n";
            cout<<"\t -------------\n";
            cout<<"\t Choice : ";
            cin>>a;
        }  while (a == 1 || a == 2 || a ==3) ;
    If 'a' equals 1 (i.e. TRUE), run loop, OR if 'a' equals 2 (i.e. TRUE), run loop, OR if 'a' equals 3 (i.e. TRUE), run loop. It's always going to be TRUE whether someone types 1,2 or 3 so the loop will always keep running.

    You need the opposite i.e. when something OTHER than 1,2 or 3 is pressed, reprint list. Think about the logic of representing something that is not 1, not 2 or not 3. 0 is outside of this, so is 4, so is 5, or 6, or 7... etc...

    Maybe this will help.
    Last edited by samwillc; 04-18-2013 at 12:34 PM.

  7. #7
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Have you tried changing it from this:

    >while(a == 1 || a == 2 || a ==3)

    to this?:

    >while ( (a == 1) || (a == 2) || (a == 3) )

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Comparison operator has a higher precedence than the or operator, so they are equivalent.
    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
    Registered User
    Join Date
    Mar 2013
    Location
    London, Ontario, Canada
    Posts
    13
    Thanks for all the help from all those who did. Last night I fixed the error.

    Code:
    while ( a ==1 || a == 2 || a == 3 )
    Was telling the compiler: " While a is equal to 1, or while a is equal to 2, or while a is equal to 3: reprint the menu. Anything else caused it to ring 3 bells and exit."

    Then I came up with:
    Code:
    while ( a !=1 && a != 2 && a != 3 )
    Was telling the compiler: " While a is not equal to 1, and while a is not equal to 2, and while a is not equal to 3: reprint the menu. " That tells the compiler anything other then these 3 numbers will cause the code to reprint the menu, since they will say what happens next. 1 bell, 2 bells, or 3 bells.

    I'm a 43 year old man trying to learn how to program in C++. Much thanks to Elysia who suggested to follow the code, and imagine what would happen.

    And to samwillc your code above won't work since you haven't declared what "a" will be used for.
    Last edited by DaveHubbard; 04-19-2013 at 09:48 AM.

  10. #10
    Registered User
    Join Date
    Mar 2013
    Location
    London, Ontario, Canada
    Posts
    13
    Quote Originally Posted by DaveHubbard View Post
    Thanks for all the help from all those who did. Last night I fixed the error.

    Code:
    while ( a ==1 || a == 2 || a == 3 )
    Was telling the compiler: " While a is equal to 1, or while a is equal to 2, or while a is equal to 3: reprint the menu. Anything else caused it to ring 3 bells and exit."

    Then I came up with:
    Code:
    while ( a !=1 && a != 2 && a != 3 )
    Was telling the compiler: " While a is not equal to 1, and while a is not equal to 2, and while a is not equal to 3: reprint the menu. " That tells the compiler anything other then these 3 numbers will cause the code to reprint the menu, since they will say what happens next. 1 bell, 2 bells, or 3 bells.

    I'm a 43 year old man trying to learn how to program in C++. Much thanks to Elysia who suggested to follow the code, and imagine what would happen.

    And to samwillc your code above won't work since you haven't declared what "a" will be used for.
    Trial and error is what keeps this world running smoothly. So keep trying to program through trial and error, the best way to learn.

  11. #11
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    Hi Dave,

    I didn't copy the whole program, only the loop, so you're spot on, it doesn't do much on its own!

    Try this:

    Code:
    # include<iostream>
    using namespace std;
    int main()
    {
        int a = 0 ;
        do
        {
            cout<<"\n\n\n";
            cout<<"\t Please choose 1 item.\n\n";
            cout<<"\t 1 : one\n\n";
            cout<<"\t 2 : two\n\n";
            cout<<"\t 3 : three\n";
            cout<<"\t -------------\n";
            cout<<"\t Choice : ";
            cin>>a;
        }  while (a !=1 && a != 2 && a != 3);
        
        if (a == 1)
        {
            cout << "You pressed: " << a;
        }
        else if (a == 2)
        {
            cout << "You pressed: " << a;
        }
        else if (a == 3)
        {
            cout << "You pressed: " << a;
        }
    }
    and substitute the while condition with these:

    Code:
    while (!(a == 1 || a == 2 || a == 3)); // Similar to what you did. If any are TRUE, the ! makes them FALSE
    or

    Code:
    while (!(a >= 1 && a <= 3)); // Anything greater or equal to 1 AND less than or equal to 3.
    or

    Code:
    while (!(a > 0 && a < 4)); // Anything greater than 0 AND less than 4.
    or (this makes most sense to me)

    Code:
    while (a < 1 || a > 3); // Anything less than 1 OR anything greater than 3.
    There are a number of ways to achieve the same thing. I'll leave it up to the experts here to say which would be considered best practice.

    I hope this gives you a few new ideas
    Last edited by samwillc; 04-19-2013 at 12:50 PM.

  12. #12
    Registered User
    Join Date
    Mar 2013
    Location
    London, Ontario, Canada
    Posts
    13
    Much thanks samwillc. You gave me 4 other ways to say the same thing, algebra. LOL

    I'm new to programming in C++, so any new ideas should help me in learning how to.

    Yes this should give me new ideas.

  13. #13
    Registered User
    Join Date
    May 2013
    Posts
    15
    Dave and group,
    I am new to C++ and going through the ebook. This was my answer to the problem. What do you think?
    tsdad

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main ()
    {
    
    string input;
    while ( input != "Mud pies" || input != "Pine cones" || input != "Jam cakes")
    {
        cout << "There are several options for tonight. Please choose from the following list" << endl;
        cout << '\t' << "Mud pies" << "\n";
        cout << '\t' << "Pine cones" << "\n";
        cout << '\t' << "Jam cakes" << "\n";
        getline ( cin, input, '\n' );
    
        if ( input == "Mud pies" || input == "Pine cones" || input == "Jam cakes")
        {
            cout << "Very good. I will get " << input << " for you." << "\n";
            break;
        }
    }
    
    }

  14. #14
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    Hi tsdad.

    Quote Originally Posted by tsdad View Post
    Dave and group,
    Code:
        if ( input == "Mud pies" || input == "Pine cones" || input == "Jam cakes")
        {
            cout << "Very good. I will get " << input << " for you." << "\n";
            break;
        }
    I don't believe you actually need an if statement in this situation, or a break. The loop itself handles the conditions because it will only exit once a correct option has been selected. Once a correct option has been selected, there's no need to test again afterwards.

    Why not try putting this line after the loop:

    Quote Originally Posted by tsdad View Post
    Code:
    cout << "Very good. I will get " << input << " for you." << "\n";
    You also want the code within the loop to run at least once. This sounds like a do/while loop to me instead of just a while loop. Not sure whether you've done them yet.

  15. #15
    Registered User
    Join Date
    May 2013
    Posts
    15
    First off, thank you for thinking about this.
    The first suggestion did not give me what I was looking for, there is no break for a correct answer if I lose the if statement with the break, until I changed the while conditions to ands instead of ors. Then it worked just fine.
    Also, the do while worked also with the same additional changes.
    Thanks for the help,
    Dan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Chapter 2 practice problems
    By Kyle Spalding in forum C++ Programming
    Replies: 14
    Last Post: 03-19-2013, 06:12 AM
  2. Replies: 2
    Last Post: 12-11-2012, 12:25 AM
  3. Best practice for super simple program
    By samwillc in forum C++ Programming
    Replies: 10
    Last Post: 06-02-2012, 04:29 PM
  4. Program difficulties
    By Birdhaus in forum C++ Programming
    Replies: 4
    Last Post: 11-22-2005, 01:18 PM
  5. help on this very small practice program
    By incognito in forum C++ Programming
    Replies: 4
    Last Post: 01-01-2002, 10:28 AM