#include <iostream>
#include <string>
#include <sstream>
usingnamespace std;
int main ()
{
string mystr;
int a;
cout << "What's your name? ";
getline (cin,mystr);
cout << "Hello " << mystr << ".\n";
cout << "How old are you?";
getline (cin, mystr);
stringstream(mystr) >> a
if (a > 16)
{
cout << "You are older than me." << endl << "I am 16 years old.";
}
elseif (a < 16)
{
cout << "You are younger than me." << endl << "I am 16 years old.";
}
else
{
cout << "I am 16 years old too.";
}
return 0;
}
ok I know that cin is easier than getline but anyway it doesnt run and it´s not beacuse it seays getline instead of cin. It says else without a previous if AND THERE IS A PREVIOUS IF!
#include <iostream>
#include <string>
#include <sstream>
usingnamespace std;
int main()
{
string mystr;
int a;
cout << "What's your name? ";
getline(cin, mystr);
cout << "Hello " << mystr << ".\n";
cout << "How old are you?";
getline(cin, mystr);
stringstream(mystr) >> a; // <<-- You also missed a ';' here that I kindly added :)
if (a > 16)
{
cout << "You are older than me." << endl << "I am 16 years old.";
}
if (a < 16)
{
cout << "You are younger than me." << endl << "I am 16 years old.";
}
else
{
cout << "I am 16 years old too.";
}
system("pause");
return 0;
}
system("pause") // because I run my code in the console and it's habitual lol