Hey everyone, I just joined this forum. I'm just starting to learn C++ and I've gotten pretty far I guess. This code was created just to test some of my already learned C++ knowledge. It worked fine until I added this bit into it.
1 2 3 4 5 6 7 8 9 10 11
if (result == "You fail, sucker");
{
if (first > 20);
{
cout << "Your number is too big" << endl;
}
elseif (first < 20);
{
cout << "Your number is too small" << endl;
}
}
After I put that in I received multiple errors in the output(I'm using Code::Blocks). This is the output.
1 2 3 4 5 6 7
/Users/MyName/Scripts/C++Starter/TheBeginning/main.cpp||In function 'int main()':|
/Users/MyName/Scripts/C++Starter/TheBeginning/main.cpp|29|error: expected `}' before 'else'|
/Users/MyName/Scripts/C++Starter/TheBeginning/main.cpp|29|error: expected `}' before 'else'|
/Users/MyName/Scripts/C++Starter/TheBeginning/main.cpp|29|error: expected unqualified-id before 'else'|
/Users/MyName/Scripts/C++Starter/TheBeginning/main.cpp|30|error: expected unqualified-id before '{' token|
/Users/MyName/Scripts/C++Starter/TheBeginning/main.cpp|33|error: expected declaration before '}' token|
||=== Build finished: 5 errors, 0 warnings ===|
#include <iostream>
usingnamespace std;
int main()
{
int first = 10;
int second = 20;
int ad;
string name;
string result;
result = first == second ? "The numbers are equal" : "The numbers are not equal";
cout << result << endl;
cout << "First number : " << first << " Second number : " << second << endl;
cout << "Your name is : ";
cin >> name;
cout << "Hello " << name << endl;
cout << "Please add a number to the first number to make the both numbers equal : ";
cin >> ad;
first += ad;
result = first == second ? "You win!" : "You fail, sucker";
cout << result << endl;
if (result == "You fail, sucker");
{
if (first > 20);
{
cout << "Your number is too big" << endl;
}
elseif (first < 20);
{
cout << "Your number is too small" << endl;
}
}
return 0;
}
an if statement is composed by the keyword "if", followed by the condition between parentheses, followed by a command or a block of commands, there is no semicolon. So lines 23, 25 and 29 should be