Getting a value between two numbers with boolean operators?

~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~ 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
Last edited on
Your second and third if()'s are closed by semicolons.
The comparison needs to be between the variable and the integral age each time. For example:
if( age > 20 && age < 40 ) { /*...*/ }.
You will not want the semicolon. It would make the true condition result in executing an "empty" statement. The following block would then always run.

Also, what happens if you enter 20 or 40?
Last edited on
Okay, so I removed the semicolons, (Even though I've already tried that -- Did anyone read the comments in the code?), and all I got was 2 extra error messages along with the first two.

(21) : error C2059: syntax error : '<'
(21) : error C2143: syntax error : missing ';' before '{'
(25) : error C2059: syntax error : '<'
(25) : error C2143: syntax error : missing ';' before '{'


I don't get it......Could I be missing a header file or something?

And I realize what would happen if I entered 20 or 40, but I was focusing on just trying to get the program to work......

~Clefspeare
closed account (zTUXoG1T)
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;
}


try this ... its working for me.
read moorecm's post again, it tells you why those error messages in your last post are occuring
OOOHHHH......Thanks, BettyBoopTS and moorecm, I missed that when I first read your post.

Now it works.

And moorecm, I see what you mean about the semicolon making the next block run. That was one of my bugs earlier on. I would type in 3, and 2 or 3 different messages would show.

Once again, thanks!
~Clefspeare
It's all about paying attention to detail and reading very, very carefully. Glad to hear it's working!
Topic archived. No new replies allowed.