If/else/switch

0 Lé Pig · February 23, 2015
Hello everyone,

   So I began making this "adventure game" and I hit kind of a wall. As you can see early on I began to get lost in the if statements and they begin to be incoherent and I'm sure you'll see what I'm trying to do when I start jumbling the different choices right when I start creating them. For example, when I choose say reply # 2 it prints out reply  for two different options but not each time. It's almost like it's selective in which response it chooses to print double and I'm not too sure how to avoid that. It's kind of messy, I know, sorry about that I'll work on it but most importantly I was hoping I could continue without using so many "if" statements and maybe use a switch, but I'm having a hard time understanding how switches actually work. This is just a small little project I'm working on, I'm not a student or anything so it's not homework :). Anyway thanks for checking it out.


#include <iostream>

using namespace std;

int main()
{
    int reply;
    reply = 1;
    reply = 2;
    cout << "Hello world!\n" << endl;
    cout << "Are you having a good day today?\n\nType 1 for 'Yes' and 2 for 'No'." << endl;

    cin >> reply;
    if (reply == 1) cout << "That's good.";
    if (reply == 2){ cout << "That's bad. What do you want to do about it?\n";
    cout << "1. Sit about and complain to whoever still listens.\n";
    cout << "2. Build a campfire (Why?).\n";
    cout << "3. Go outside.";}
    cin >> reply;
    if (reply == 3) cout << "It's raining. You died. Press anything to exit the program";
    if (reply == 1) cout << "Ok, have fun with that. Press anything to exit the program.\n";
    if (reply == 2){ cout << "Well, that's a start. Now what?";
    cout << "\n1. Cook a fish.";
    cout << "\n2. Warm your hands.";
    cout << "\n3. Put out the fire and do something more productive.";}
    if (reply == 2){ cout << "\nYour hands are warmer, but they were never really cold. Good job!\n Now what?";
    cout << "\n1. Go back inside and throw a tennis ball against the wall (Dumb)";
    cout << "\n2. Do much needed school work.";
    cout << "\n3. Clean the house.";

    cout << "\n3. Put out the fire and do something more productive.";
    }
    cin >> reply;
    if (reply == 1){ cout << "\nYou ate the fish. It was good. Your mood has slightly improved\n";
    cout << "With your new mood you feel you can take anything on! But what?";
    cout << "\n 1. Get in a fight with a stranger (You have no experience fighting)";
    cout << "\n 2. Quit your job (which you hate)";
    cout << "\n 3. Tell your neighbor to stop playing their music so loud";}
        cin >> reply;{
            if (reply == 1){ cout << "You fought someone who also didn't know how to fight (Right on!) but you still\n lost.";}

            if (reply == 2){ cout << "You told your boss to go eat poop. Yeah! Now what?";
    cout << "\n 1. Go find more work (Highly recommended)";
    cout << "\n 2. Go rob a bank (Not so highly recommended";
    cout << "\n 3. Go home and do all the fun things your work has deprived you of.";
            }
    if (reply == 2){ cout << "\nYour hands are warmer, but they were never really cold. Good job!\n Now what?";
    cout << "\n1. Go back inside and throw a tennis ball against the wall (Dumb)";
    cout << "\n2. Do much needed school work.";
    cout << "\n3. Clean the house.";
    }
    if (reply == 3) cout << "\nOk, probably a good choice.\n";
    }
    return 0;


}

Post a Reply

Replies

Oldest  Newest  Rating
0 Cardinal Coog · February 23, 2015
Basically a switch works like this:

switch( <yourVariable> ){
case 1:
code to execute;
break;
case 2:
code to execute;
break;
case 3:
code to execute;
break;
default:
cout << "Error. Bad input << endl;
break;
}
The code to execute in each case should be indented. Usually, the code to be executed is a call to a pre-defined function.

Hope this helps.
0 Lé Pig · February 23, 2015
Very helpful. Thank you. So now would I want to have each case branch off for multiple answers for each case? So say case 1 leads to 3 more choices and so forth?

#include <iostream>

using namespace std;
int main()
{
   cout << "Do you want to play a game?\n";
   cout << "1. Yes \n2. No \n3. I'm not really sure\n";
   int choice;
   cin >> choice;

   switch (choice)
   {
   case 1:
        cout << "Ok, then";
        break;
   case 2:
        cout << "Alrighty then";
        break;
   case 3:
        cout << "Well...";
        break;

    default:
        cout << "That's not an answer...";
        return main();
   }
}
0 Dol Lod · February 23, 2015
Switch statements are covered by Bucky: https://www.thenewboston.com/videos.php?cat=16&video=17501

I don't want to give out incorrect information so I would leave the bulk of the explanation to the tutorial. However, you can use a variety of data type comparisons, not just ints. You can additionally use chars. Additionally, you can use fall throughs for switch statements. What I mean by fallthrough is grouping cases together by putting the cases next to each other without a break. Here is an example. 

char i='2'
switch(i)
{
     case '1':
     case '2':
     cout<"1 or 2"<endl;
     break;
    case '3':
    cout<<"3<<endl;
   break;
    default:
    cout<<"Default"<<endl;
    break;
}
0 Lé Pig · March 3, 2015
Thanks for the reply. I'm having a hard time following though. I watched the tutorial and I can see how switches are helpful in certain scenarios but can a switch really save time and make the program easier to read if you were to use switches instead of if statements or if else statements say in the program that I posted in the original post? Thanks again.
0 c student · March 3, 2015
how far are you into programming with c++?
0 Troy Jones · March 3, 2015
Yes, you can nest if/else statements and switch and case inside each other.
0 Lé Pig · March 3, 2015
I am not very far c student just beginning. Now I heard that switches will be beneficial when doing what I was doing in the program in the original post I was wondering if someone could give me some examples as to how it would save time and make it easier to read by stacking switch statements. Thanka
0 c student · March 3, 2015
instead of throwing everything into main, i'd suggest you use functions to make it cleaner.  i would also suggest some kind of directed graph or spanning tree data type for this however, as you have said, you aren't too familiar with much of the inner workings of c++.
0 Lé Pig · March 4, 2015
Thanks for the input. How can I create functions that make a little game like that easier to write? Sorry, I know I seem high maintenance but you'll have to explain it to me like I'm a kid :D
+1 c student · March 4, 2015
maybe something like this:
#include ...

int stageOne (void);
int stageTwo (void);
...
...
...

int main (void) {

   stageOne();
   stageTwo();
   ...
   ...
   ...

   return 0;

}

int stageOne (void) {
   /* your code here */
}

i'd also suggest you look into oop.
  • 1

C++

107,024 followers
About

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

Links
Moderators
Bucky Roberts Administrator