-
IF statements
Hey all.
I'm new to programming, and im currently at lesson 2 about If statements. I used the example given in the thread, and tried to create one very similar, but it's not made correctly.
Here is the code, hope someone can tell me whats wrong:
Code:
#include iostream
using namespace std;
int main()
{
int age;
cout<<"How old are you? ";
cin>> age;
cin.ignore();
if (age < 20); {
cout<<"You are young.\n";
}
else if (age = 20) {
cout<<"You are pretty.\n";
}
else (age > 20) {
cout<<"You are old!\n";
}
}
-
no semicolon after a 'IF' statement allowed:
'else' is not a conditional statement.. it is a block of code that is executed if all other 'if's or 'else if's' have been attempted:
Code:
else (age > 20)
//should just be
else
{ //stuff here }
-
> else if (age = 20)
Also, use == for comparison,
= is assignment.
-
Hmm.. It's still not working, the program says there is something wrong with this line:
Code:
cout<<"Please input your age: ";
I tried to fix by adding \n, but it's still not working.
Here's the hole code again:
Code:
#include iostream
using namespace std;
int main()
{
int age;
cout<<"Please input your age: ";
cin>> age;
cin.ignore();
else (age < 20) {
cout<<"You are young.\n";
}
else if (age == 20) {
cout<<"You are pretty.\n";
}
else (age > 20) {
cout<<"You are old!\n";
}
}
-
Your first else should be an if.
-
Still doesnt work, look at my post again. The program (dev-C++) makes an error at this line, and i really can't see whats wrong. I tried adding \n in the end, but that doesnt help.
Code:
cout<<"Please input your age: ";
-
> The program (dev-C++) makes an error at this line
Well posting the actual error message would help.
Also, a single line in isolation doesn't say much. It's also pretty likely that the actual error is on a preceding line and this is just the first line that the compiler notices that there is something wrong.
For such a short program, reposting the whole thing again is the best course.
-
Could be that you use #include iostream instead of #include <iostream>, but the compiler should complain about that.
-
Can't imagine 3 people missed this one ;)
See anything that's missing?
-
The error line is:
'cout' undeclared (first use this function).
This is the hole code:
Code:
#include iostream
using namespace std;
int main()
{
int age;
cout<<"Please input your age: ";
cin>> age;
cin.ignore();
if (age < 20) {
cout<<"You are young.\n";
}
else (age == 20) {
cout<<"You are pretty.\n";
}
else (age > 20) {
cout<<"You are old!\n";
}
}
-
I had to use:
<iostream>
instead of
iostream
thx for all the replies.
-
> Here is the code, hope someone can tell me whats wrong:
Your compiler will tell you what's wrong.
Code:
$ g++ foo.cpp
foo.cpp:1:10: error: #include expects "FILENAME" or <FILENAME>
foo.cpp: In function ‘int main()’:
foo.cpp:10: error: ‘cout’ was not declared in this scope
foo.cpp:11: error: ‘cin’ was not declared in this scope
foo.cpp:16: error: expected primary-expression before ‘else’
foo.cpp:16: error: expected `;' before ‘else’
foo.cpp:19: error: expected primary-expression before ‘else’
foo.cpp:19: error: expected `;' before ‘else’
Start at the top, and work your way through the error messages. Just fixing the #include gets rid of 3 errors.