~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~ Fixed, thanks to moorecm! ~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~
Okay, so I was running a test program loosly based on a tutorial about operators/constants/variables, or something like that. The idea is that you ask the user to input their age, or some random number, and based on what they input, a message will show up. I originally had it so if you put anything less than 40, it would show "you're not that old.", and if they entered a number above or equal to 40, it would say "you're pretty old...".
Then I tried spicing it up a bit, intending to make it so that if it was somewhere between 20 and 40, or 40 and 100 then it would give different messages.
I attempted using the boolean operator && to check the two < and > values if they were true, so that anything between the two numbers would activate the cout statement to print the appropriate message for the numerical value entered.
I'm only a beginner in C++, so there may be a alternate way to do this, but if there is, I can't seem to find it anywhere.
By the way, I use a Microsoft Visual C++ compiler, hence the "stdafx.h" header.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int age = 0;
cout << "Please enter your age and then press enter." << endl; // asks for a number
cin >> age; // user inputs age
cin.ignore();
if (age < 20) {
cout << "You're still pretty young.\n" << endl;
}
if (age > 20 && < 40); {//This is where I have a problem. I get an error message telling me that there is a syntax error on lines 21 and 25, both referring to the < character.
cout << "You're not that old...\n" << endl;
}
if (age > 40 && < 100); { // < I'm not sure if that semicolon should be here or on the one above it, but I get an error message if I take them away...
cout << "You're over the hill....\n" << endl;
}
if (age >= 100) { //yet this one doesn't need a semicolon....could somebody please explain?
cout << "Dude....You're AINCENT!\n" << endl;
}
cin.clear();
cin.ignore(255, '\n');
cin.get();
return 0;
}
|
I only get error messages for lines 21 and 27....There's nothing wrong with the rest of the program, it seems.
It may not be the program itself, but the greater-than less-than thing...If there is another way to only activate the "if" statement when the value entered is between two numbers, please help me.
Thanks!
~Clefspeare