Code not working and I'm unsure why

So I'm trying to type out the code below that insults my brother in law when he types in his name after being prompted, says i love you to my girlfriend, and calls every other person amazing. for some reason every time I input "Jesus Rosa" or "Carly Reyes" it says "You're Amazing" rather than the insult for Jesus or ILY for Carly. Anyone know why?

Also don't be offended by my message to Jesus, we're coding together and he thinks it's hilarious what this code does.

*******************************************************


// This program is designed to insult Jesus Rosa

#include <iostream>
#include <string>
#include <stdbool.h>
#include <iomanip>

using namespace std;

int main()
{
string name;

cout << "What is your first and last name?" << endl;
cin >> name;

if (name == "Jesus Rosa") {
cout << "You're a spanish piece of shit. \n";
}
else if (name == "Carly Reyes") {
cout << "I love you. \n";
}
else {
cout << "You're amazing. \n";
}


return 0;
}
cin>> will stop at the first blank space. If you want a multi-part name then use
getline( cin, name );

Please use code tags.
@lastchance

why wouldn't i format it as

cin.getline(name);
luckylukebrooks wrote:
why wouldn't i format it as
cin.getline(name);


Because that would be reading a c-string into a pre-sized buffer (and you haven't declared maximum characters in your call), not a std::string, which is far more flexible and doesn't need pre-sizing.
why wouldn't i format it as

cin.getline(name);


Cause that's how the language is defined!

For std::getline() see http://www.cplusplus.com/reference/string/string/getline/

for cin.getline() see http://www.cplusplus.com/reference/istream/istream/getline/

The two aren't the same.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string name;

	cout << "What is your first and last name?\n";
	getline(cin, name);

	if (name == "Jesus Rosa")
		cout << "You're a spanish piece of shit. \n";
	else if (name == "Carly Reyes")
		cout << "I love you. \n";
	else
		cout << "You're amazing. \n";
}

Last edited on
why wouldn't i format it as
cin.getline(name);

That calls member function getline of object cin.

The cin is of type istream and istream has member getline.
However, it does require two or three arguments, not one.
Furthermore, it does not accept std::string as any of its arguments.
See: https://en.cppreference.com/w/cpp/io/basic_istream/getline
Topic archived. No new replies allowed.