hello. I'm trying to figure out how you could use strings, and show equality with user input. This code compiles, and runs, but doesn't do what I'm trying to get it to do. What I would like to have happen is obvious, but what happens is it ends at please enter your saviours name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string HisName;
string Jesus;
cout << "please enter your saviours name: ";
cin >> HisName;
if (HisName == Jesus)
{
cout << "Amen!";
}
return 0;
}
I'm a little confused as to how to do an if statement to check if the imput matches to display the message.
string Jesus;
That's initalising a variable string named Jesus and it's contents aren't anything you'd want. You need to give it something to store first. string Jesus = "Jesus";
Now Jesus variable contains Jesus.
Also it's better to use getline(cin, myString); as opposed to cin >> myString;
I'm not sure how new you are to C++, but I just thought I'd add in something that I often use to save memory, which in a much larger program can make the difference between a slow or fast running program.
Instead of doing as bluezor said, which he is absolutely correct, it may be easier for you to think of it this way. Not to knock bluezor at all, just throwing this out there as well.
Delete your line that reads: string Jesus;
In your if statement, change it to:
1 2 3 4
if (HisName == "Jesus")
{
cout << "Amen!";
}
By putting quotation marks around Jesus, it will understand it is looking to see if the string the user-entered is equal to Jesus.
Sorry if I confused you at all, pretty basic concept so I hope I could've possibly helped as well.