#include <iostream>
#include <string>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
usingnamespace std;
std::string x;
int main() {
cout << "What is your full name?\n";
cin >> x;
if(x=="Fred")
{
cout << "Computer says - You suck.\n";
}
elseif(x == "Joe")
{
cout << " Computer says - You are the greatest!\n";
}
return 0;
}
The code is fine, assuming you typed in Fred or Joe.
Notice the comment at the top.
run this program using the console pauser or add your own getch, system("pause") or input loop
You should run it through cmd (assuming Windows since you wouldn't have this problem on Linux in the first place).
Best solution is to have your IDE pause the console after execution so that you don't have to. All major IDEs like Code Blocks, Visual Studio, Eclipse, should have this option.
The problem is that your output happens so fast and program closes as soon as its calculations are done, so you have no time to see the output.
NVM I got it I for some reason had a space in the names I was using so I guess the compiler cant handle that? How do I make it so lets say I type instead
Thanks alot, and yea I saw the rant with duoas but I dont plan on doing anything publicly with my programs, I just like writing em for my own use. But W/e thank you for the help.
The reason why "Fred Ford" doesn't work is because you are using cin >> to get your name. cin "sees" the space in "Fred Ford" and retrieves only "Fred". Does "Fred" == "Fred Ford"? Nope.
If you want to properly retrieve "Fred Ford" with C++ strings you can use: