1st post! New to C++ quite new...

0 goku san · February 18, 2015
Here's a problem I met at c++ . My problem is the "Ew, that's disgusting phrase never get to "cout" . it always links it to "it sounds delicious" what am I doing wrong?

Spoiler:

#include <cstdlib>
#include <iostream>

using namespace std;

int main () {

    int ans;
    //int mark;
    //int question;
    //for(;; ) { //infinite re-route

    cout<<"A Picnic"<<endl;
    cout<<endl;
    cout<<"You may only provide ONE answer for the following questions."<<endl;
    cout<<"One day, you are going to a picnic with your parents."<<endl;
    cout<<"What would be the only item you bring?"<<endl;
    cout<<"(1) Food   (2) Drink   (3) Laptop "<<endl;
    cin>>ans;
    cout<<endl;

    switch (ans)
    {
    case 1:
        cout<<"What kind of food will you bring?"<<endl;
        cout<<"(1) Hamburger   (2) Roti Canai   (3) Sandwiches  "<<endl;
        cin>>ans;

        if (ans==1) {
            cout<<"What kind of topping do you want?"<<endl;
            cout<<"(1) Cheese   (2) Pickles   (3) Vegetables"<<endl;
            cin>>ans;

             if (ans==1||3) {
            cout<<"That sounds delicious! "<<endl;
            }
            else if (ans==2) {
            cout<<"Ew, that's disgusting!"<<endl;
            }
            else {
            cout<<"Invalid Answer"<<endl;
            }
        }
        else if (ans==2) {
            cout<<"What kind of Roti will you prefer to eat?"<<endl;
            cout<<"(1) Roti Kosong   (2) Roti Bawang   (3) Roti Telur"<<endl;
            cin>>ans;
              if (ans==1||2||3) {
            cout<<"That sounds delicious! "<<endl;
            }

            else {
            cout<<"Invalid Answer"<<endl;
            }
        }
        else if (ans==3) {
            cout<<"What kind of Sandwiches will you prefer to eat?"<<endl;
            cout<<"(1) Egg   (2) Vegetables   (3) Ham"<<endl;
            cin>>ans;
              if (ans==1||2||3) {
            cout<<"That sounds delicious! "<<endl;
            }

            else {
            cout<<"Invalid Answer"<<endl;
            }

        }
        else {
            cout<<"Invalid Answer"<<endl;
        }
    break;

    case 2:
         cout<<"What kind of drink will you bring?"<<endl;
         cout<<"(1) 100 plus   (2) Milo   (3) Fruit Juice"<<endl;
         cin>>ans;

        if (ans==1) {
            cout<<"What kind of flavor will you want it to be??"<<endl;
            cout<<"(1) Original   (2) Lemon   (3) Orange "<<endl;
            cin>>ans;
              if (ans==1||3) {
            cout<<"That sounds delicious! "<<endl;
            }

            else if (ans==2) {

                cout<<"Ew, that's disgusting!"<<endl;
            }

            else {
            cout<<"Invalid Answer"<<endl;
            }
        }
        else if (ans==2) {
            cout<<"How will you like to eat with the Milo?"<<endl;
            cout<<"(1) Biscuits   (2) Bread   (3) Magee Mee"<<endl;
            cin>>ans;
               if (ans==1||2) {
            cout<<"That sounds delicious! "<<endl;
            }

            else if (ans==3) {

                cout<<"Ew, that doesn't seem to be a great combo..."<<endl;
            }

            else {
            cout<<"Invalid Answer"<<endl;
            }
        }
        else if (ans==3) {
            cout<<"What kind of juice will you want it to be?"<<endl;
            cout<<"(1) Orange   (2) Apple   (3) Tomato"<<endl;
            cin>>ans;
               if (ans==1||2) {
            cout<<"That sounds delicious! "<<endl;
            }

            else if (ans==3) {

                cout<<"Ew, that's disgusting!"<<endl;
            }

            else {
            cout<<"Invalid Answer"<<endl;
            }
        }
         else {
            cout<<"Invalid Answer"<<endl;
        }
    break;

    case 3:
        cout<<"What kind of games will you be playing?"<<endl;
        cout<<"(1) League of Legends   (2) Dota 2   (3) Facebook "<<endl;
        cin>>ans;
          if (ans==1) {
            cout<<"What kind of champions will you like to choose?"<<endl;
            cout<<"(1) Ashe   (2) Corki   (3) Annie"<<endl;
            cin>>ans;
               if (ans==1||2||3) {
            cout<<"Hope you have fun! "<<endl;
            }

            else {
            cout<<"Invalid Answer"<<endl;
            }
        }
        else if (ans==2) {
            cout<<"Which role will you prefer to play?"<<endl;
            cout<<"(1) Carry   (2) Mid   (3) Support"<<endl;
            cin>>ans;

               if (ans==1||2) {
            cout<<"Be prepared and bring your team to the victory! "<<endl;
            }

            else if (ans==3) {

                cout<<"That must be a challenging role!"<<endl;
            }

            else {
            cout<<"Invalid Answer"<<endl;
            }
        }
        else if (ans==3) {
            cout<<"Who will you do in Facebook?"<<endl;
            cout<<"(1) Chat with besties   (2) Post your status   (3) Post a 'selfie'"<<endl;
            cin>>ans;
               if (ans==1||2||3) {
            cout<<"Seems to be a great plan! "<<endl;
            }


            else {
            cout<<"Invalid Answer"<<endl;
            }
        }
         else {
            cout<<"Invalid Answer"<<endl;
        }
    break;

    default :
        cout<<"Invalid Answer"<<endl;
        cin>>ans;

    break;

    }


    //}


    system ("pause") ;


    return 0;
}



i think i misunderstood the "if" concept..

Post a Reply

Replies

Oldest  Newest  Rating
0 goku san · February 19, 2015
thanks! i totally understand it now! tqvm!
+2 c student · February 19, 2015
computers have a thing with numbers.  anything that is 0 is false and any non-zero number is true

if (0) { /* FALSE */}
if (!(0)) { /* TRUE */ }

so your code:

if (ans == 1 || 2) { /* ans == 1 or TRUE */ }

since true always runs, it will always pass the if statement.

here's more on if statements:
http://www.cprogramming.com/tutorial/lesson2.html
0 goku san · February 19, 2015
@Seems  Legit Thanks dude! Yeah I need to keep this stuff abit more neater. 

@Abdullah Nauman , well it's just a concept available in almost every forum, type 
[spoiler*] XXXX [/spoiler*] , XXX is what you wanna show, do remember to exclude the "*" symbol cause i want to make sure it doesnt appear as a spoiler.
example :
Spoiler:
XXXX
 
^ I type the exactly same stuff without the "*" thing.

@C student , do u mean i must express it using ans==1||ans==2 ... ? What does the computer read if i write ans==1||2 ?

thanks for the comments guys! i really appreciate it!
0 c student · February 19, 2015
Spoiler:

if (ans == 1 || ans == 2 || ans == ...) { /* your code here */}

+1 Abdullah Nauman · February 18, 2015
How did you make than show+hide, thing.
0 Seems Legit · February 18, 2015
This might not solve your problem, but a tip I'd like to give you is try to organize your code a bit more. Like for example with your if-else statement the code below is 'more easy to read'

if (ans==1||3) {
cout<<"That sounds delicious! "<<endl;
} else if (ans==2) {
cout<<"Ew, that's disgusting!"<<endl;
} else {
cout<<"Invalid Answer"<<endl;

It's just a tip :)
  • 1

C++

106,985 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator