-2

I'm a newbie with c++, and I tried googling a solution but every one I came across was so different from the issue I was facing so I couldn't figure it out. The problem I'm having is my "if" statements are completely ignored when I run the .exe from powershell.

https://pastebin.com/aE6MiQig

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string q;
    string w;
    string Bob;
    string Emily;
    {
        cout << "Who is this? ";
        cin >> q;
        if (q == Bob)
        {
            cout << "Hey there bro. ";
        }
        if (q == Emily)
        {
            cout << "Hi friend :) ";
        }
        else
        {
            cout << "Oh hey " << q << ", how are you? ";
        }
        cin >> w;
        cout << "Hey, that's " << w;
    }
    return 0;
}

When I input my name as "Bob" I should be seeing the message from the if statement "Hey there bro." but I am instead seeing the else statement, "Oh hey Bob, how are you?". Same goes when I input Emily. Only seeing the else statement.

I'm not getting any errors (running this in visual studio) so where am I messing this up?

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
Zii
  • 1
  • 5
  • 7
    Where do you set `Bob` or `Emily` to anything? – NathanOliver Aug 08 '18 at 18:57
  • 2
    Might want to use a debugger next time. – Rakete1111 Aug 08 '18 at 18:58
  • 1
    Change `string Bob;` to `string Bob{"Bob"};` and see what happens. – drescherjm Aug 08 '18 at 19:01
  • 1
    Did you by any chance mean to compare q to a literal string "Bob" and "Emily"? Your code example suggests that could be the case, in which case you should write them as string literals (e.g. "Bob"). The way you have them, they are empty string variables that have not yet been assigned any value. – Marijan Aug 08 '18 at 19:03
  • @NathanOliver "if (q == Emily)" line? Doesn't that make it so when the user inputs "Emily" it will show the "Hi friend" message instead? – Zii Aug 08 '18 at 19:13
  • @Marijan you mean I need to put them in "double quotes" to make them literal? – Zii Aug 08 '18 at 19:13
  • @drescherjm I'll give that a shot! Thanks – Zii Aug 08 '18 at 19:13
  • @Rakete1111 I started C++ yesterday, what is a debugger? Doesn't Visual studio give you warnings and errors if you make a mistake? – Zii Aug 08 '18 at 19:13
  • @Zii [See this Q](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). Debuggers are great to find bugs, because they allow to go through your program step by step and inspect variables in real-time :) Yes they do, but there are many different tools to help programmers not make mistakes. – Rakete1111 Aug 08 '18 at 19:14
  • 3
    Re, "Doesn't Visual studio give you warnings and errors if you make a mistake?" Mistakes can be made on many different levels. A tool like Visual Studio can find and highlight the simplest mistakes before you ever try to run the program. A _[debugger](https://en.wikipedia.org/wiki/Debugger)_ can help you to find deeper mistakes that only become apparent when you actually do run the program, and it doesn't do what you expected it to do. – Solomon Slow Aug 08 '18 at 19:40
  • P.S.: Visual Studio has a debugger built-in, but I can't tell you how to use it. – Solomon Slow Aug 08 '18 at 19:42
  • ***Doesn't Visual studio give you warnings and errors if you make a mistake?*** Visual Studio would have had no idea you made a mistake for this case. Your program is syntactically correct. Although it does not do what you expect. – drescherjm Aug 08 '18 at 19:56
  • I *think* you start debugging pressing F5. Not 100% sure, only rarely using MSVC... At least, it is one of the Fn keys. – Aconcagua Aug 08 '18 at 19:57
  • Typical (potential) errors IDE in general warn about are comparing signed and unsigned integrals, bad format specifiers for printf (GCC does, MSVC: not sure), possible operator precedence issues (A & B == C - recommending to set parentheses), possible unintended assignment (`if(x = y)` - `if((x = y))` prevents the warning, with the additional, normally obsolete parentheses you tell the compiler assignment and comparison of result were intentional), ... – Aconcagua Aug 08 '18 at 20:01
  • 1
    ***I think you start debugging pressing F5*** Make sure you set a breakpoint at the beginning of your code if you do that otherwise your code will likely execute past the point where you want to debug. – drescherjm Aug 08 '18 at 20:01
  • @drescherjm Good point - got that natural to me during the years that I forget to hint beginners to... – Aconcagua Aug 08 '18 at 20:05

3 Answers3

4

Why not just compare directly to strings?

#include <iostream>
#include <string>

int main()
{
  std::string q;
  std::string w;
  std::cout << "Who is this? ";
  std::cin >> q;
  if (q == "Bob") // note the quotation marks around Bob
  {
      std::cout << "Hey there bro. " << std::endl;
  }
  else if (q == "Emily") // note the quotation marks around Emily
  {
      std::cout << "Hi friend :) " << std::endl;
  }
  else
  {
      std::cout << "Oh hey " << q << ", how are you? ";
      std::cin >> w;
      std::cout << "Hey, that's " << w << std::endl;
  }
  return 0;
}

Also, you should avoid using namespace std because it leads to namespace pollution. Instead, you can just put std:: in front of string, cin, cout, and endl as I've done above, or include a using statement for each of those specifically, like this:

using std::string;
using std::cin;
using std::cout;
using std::endl;
Alex Johnson
  • 958
  • 8
  • 23
1

std::string comes with its own operator==, comparing the string's contents.

string Bob;
string Emily;

Well, now you have created two strings, both using the default constructor, and so both are empty strings (i. e. they both would compare equal to "").

You need to assign them a value:

string Bob("Emily");
string Emily("Bob");

I deliberately assigned them inverse! Try this piece of code and you'll discover yourself that it is the content that is relevant for comparison, not the variable's name...

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
  • 1
    "_I desperately assigned them inverse!_" Did you, by any chance, mean, that you "_Purposefully assigned them in the opposite manner_"? I had to re-read the sentence multiple times, to grasp what you had in mind. – Algirdas Preidžius Aug 08 '18 at 19:09
  • @AlgirdasPreidžius Haha, discovered that myself already - bad luck that someone spotted as well before I could correct it... Sorry, English is not my mother tongue, so, although it's (hopefully???) not *that* bad, sometimes... – Aconcagua Aug 08 '18 at 19:12
  • Your English is fine. – drescherjm Aug 08 '18 at 19:15
  • @Aconcagua Your English is fine. It's just that one bit, that was confusing to me, and I decided, that I might as well share it. Note: English isn't a native language to a majority of speakers (for example, I am not a native speaker either). – Algirdas Preidžius Aug 08 '18 at 19:17
  • Thanks, guys... @AlgirdasPreidžius Imagined so already. However could not find out *which* language uses z with trema. Would be curious now... – Aconcagua Aug 08 '18 at 19:53
1

You obtain the value for string q from the user and compare it to string Emily or string Bob , neither of which have any values assigned.

The problem I'm having is my "if" statements are completely ignored when I run the .exe from powershell.

Your if statements are not being ignored instead you're comparing string q to "".

You can give string Bob and string Emily initial values:

#include <iostream>
#include <string>

int main()
{
  std::string Bob = "Bob";
  std::string Emily = "Emily";
  std::string q;
}

This way you have something to compare the values you're getting from cin to:

#include <iostream>
#include <string>

int main()
{
  std::string Bob = "Bob";
  std::string Emily = "Emily";
  std::string q;

  std::cout<<"What is your name? ";
  std::cin>>q; //Obtain value from cin

  if(q == Emily){}//If q is Emily, then do something
}

You can read more about default variable values here and here.

GKE
  • 960
  • 8
  • 21