need help with if else loop

closed account (i8bjz8AR)
Always have trouble with if else statements. I can't get "Good choice!" to output when i enter LeBron. What am i doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

using namespace std;

int main()
{
    cout << "I am just going to run through some input and output statements with loops, go over some\n";
    cout << "functions, look at arrays, vectors, c-strings, and last but not least,\n";
    cout << "and definitley everybodys favorite, pointers!\n\n";

    cout << "Alright, now time for some input!";

    cout << "Who is your favorite basketball player?\n";
    cout << "Type LeBron or Kobe, exactly as you see it appear, then press enter\n";

    string input, LeBron, Kobe; //used to hold the name of favorite player
    cin >> input;  //gets the input from the user

    if(input == LeBron)
    {           //if and else loop compares input and
        cout << "Good choice!";  //prints correct output depending on
    }
                                //the logic assigned
    else
     {
        cout << "Kobe is NOT a VERY good basketball player atm";

     }

    return 0;
}
closed account (E0p9LyTq)
You are comparing your input string against a NULL string (nothing in the string).

You also need to include the <string> header file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>

int main()
{
   std::cout << "I am just going to run through some input and output statements with loops, go over some\n";
   std::cout << "functions, look at arrays, vectors, c-strings, and last but not least,\n";
   std::cout << "and definitley everybodys favorite, pointers!\n\n";

   std::cout << "Alright, now time for some input!";

   std::cout << "Who is your favorite basketball player?\n";
   std::cout << "Type LeBron or Kobe, exactly as you see it appear, then press enter\n";

   std::string input;
   std::string LeBron = "LeBron";
   std::string Kobe = "Kobe"; //used to hold the name of favorite player
   std::cin >> input;  //gets the input from the user

   if (input == LeBron)
   {
      //if and else loop compares input and
      std::cout << "\nGood choice!\n";  //prints correct output depending on
   }
   //the logic assigned
   else
   {
      std::cout << "\nKobe is NOT a VERY good basketball player atm\n";
   }

   return 0;
}


Or you could just compare your input against a string literal:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>

int main()
{
   std::cout << "I am just going to run through some input and output statements with loops, go over some\n";
   std::cout << "functions, look at arrays, vectors, c-strings, and last but not least,\n";
   std::cout << "and definitley everybodys favorite, pointers!\n\n";

   std::cout << "Alright, now time for some input!";

   std::cout << "Who is your favorite basketball player?\n";
   std::cout << "Type LeBron or Kobe, exactly as you see it appear, then press enter\n";

   std::string input;
   std::cin >> input;  //gets the input from the user

   if (input == "LeBron")
   {
      //if and else loop compares input and
      std::cout << "\nGood choice!\n";  //prints correct output depending on
   }
   //the logic assigned
   else
   {
      std::cout << "\nKobe is NOT a VERY good basketball player atm\n";
   }
}

I am by no means great at c++, but I got your program to work by simply getting rid of the strings LeBron and Kobe, including string at the top, and putting LeBron and Kobe in quotes in the loop. I believe the issue was that you were declaring LeBron and Kobe as separate strings rather than something that could be set in the input string.
#include <iostream>
#include <string>

using namespace std;

int main()
{
    cout << "I am just going to run through some input and output statements with loops, go over some\n";
    cout << "functions, look at arrays, vectors, c-strings, and last but not least,\n";
    cout << "and definitley everybodys favorite, pointers!\n\n";

    cout << "Alright, now time for some input!";

    cout << "Who is your favorite basketball player?\n";
    cout << "Type LeBron or Kobe, exactly as you see it appear, then press enter\n";

    string input; //used to hold the name of favorite player
    cin >> input;  //gets the input from the user

    if(input == "LeBron")
    {           //if and else loop compares input and
        cout << "Good choice!\n";  //prints correct output depending on
		system("PAUSE"); /*System pause so you can read the output rather than it rapidly closing out */
    }
                                //the logic assigned
    else if (input == "Kobe")
     {
        cout << "Kobe is NOT a VERY good basketball player atm";

     }

    return 0;
}
Last edited on
closed account (i8bjz8AR)
Thanks so much! I see what i did! Also, I noticed you implemented the std thing into the code where there are cout and cin statements. Is this something i should just get into a habit doing now as i move into computer science 2?
Honestly I would say that implementing namespace std is a preference, if you're comfortable with typing std:: before stuff then you can do that. I just think it makes it simpler for reading through and typing but if you prefer it that way then that's fine too.
closed account (E0p9LyTq)
but I got your program to work by simply getting rid of the strings LeBron and Kobe, including string at the top, and putting LeBron and Kobe in quotes in the loop.


You did something similar to what I did with my second version, the one that used a string literal for input comparison.

If you have only TWO choices you do not need to include another comparison check on your else statement.

If you have to use system("pause"); then placing in in your if block is not a good idea. What if "the user" chooses "Kobe?" *PLOOF!* Console window disappears immediately after displaying the output.

Place the system("pause"); statement AFTER your if/else blocks and before your return 0; statement.

Using system("pause"); is a very, very, VERY BAD idea.
I just did that to demonstrate that specific if block, sorry for any confusion. Why do you say using system("pause"); is a bad idea?
closed account (E0p9LyTq)
Thanks so much! I see what i did! Also, I noticed you implemented the std thing into the code where there are cout and cin statements. Is this something i should just get into a habit doing now as i move into computer science 2?


Abso-FRAGGING-lutely!

http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

For a price of a few extra keystrokes, trying to debug a multiple file/multiple 3rd party library project becomes manageable within the lifetime of most sentient lifeforms.
closed account (E0p9LyTq)
Why do you say using system("pause"); is a bad idea?


It is not a universal command across all operating systems.

http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong
It is not a universal command across all operating systems.

Oh, I was totally oblivious to that fact, thanks I'll try to keep that in mind too.
If you have only TWO choices you do not need to include another comparison check on your else statement.

I know I honestly forgot that was in there, I had put it in because the first time I ran his program to try to see what was wrong it gave me the else output and I put that there before I realized what the actual problem was.
closed account (E0p9LyTq)
Having the if, else/if blocks gives you three choices.

if "LeBron," do this.

Or if "Kobe," do something else.

If none of the above, do nothing.
Topic archived. No new replies allowed.