Simple if statement

I am a real beginner with C++ and I am trying to make an if statement to work. My problem is that the variable age gets the value of 50 if at cin >> age a number greater that 30 in inserted. If I insert a number smaller that 30 the variable works normally.

So, only the if part works in my code. In other cases the output is the code inside of else if AND the variable age gets 50 as its value.

i hope my question is not too stupid :/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	cout << "Quanti anni hai?" << endl;
	cin >> age;
	
	if(age < 30)
		{
		cout << "\nMolto bene, " << name << ". " << strage1 << age << " blablabla." << endl << endl;
		_getch();
		}
	else if(age < 50)
		{
		cout << "\nMolto bene, " << name << ". " << strage2 << age << " blablabla." << endl << endl;
		_getch();
		}
	else
		{
		cout << "\nMolto bene, " << name << ". " << strage3 << age << " blablabla." << endl << endl;
		_getch();
		}
Last edited on
So the problem is that if you entre an age value higher than 30, you always get 50, as an ouput?. I run you program and it seems to work well.
Yes. If the value is greater than 30, it is always 50. the number is indifferent as long as it is higher than 30.
That code should work as intended. Is it possible that strage2 has the same value as strage3 making it appear like they are the same output?
Seems weird i ran that statement and it take the right values, if there's more in your code beside that maybe there's the problem or in the way you define your variables.

tl;dr: works fine for me, check values and that stuff.
It started to work when I replaced those _getch() functions with one _getch() after the whole statement.

I don't know if the problem can be related to my compiler? I installed MinGW and I am running my code on the command line.
hello ,

your question is not understandable !!!
what do you want from your program exactly ??

we must know that first , then we can help you.

please edit your question.
but i think this will solve your problem.




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
#include<iostream>
#include<cmath>

using namespace std;

int main(){

    
    cout << "Quanti anni hai?" << endl;
	cin >> age;
	
	if(age <= 30)
		{
		cout << "\nMolto bene, " << name << ". " << strage1 << age << " blablabla." << endl << endl;
		_getch();
		}
	else if(age < 50 && age > 30)
		{
		cout << "\nMolto bene, " << name << ". " << strage2 << age << " blablabla." << endl << endl;
		_getch();
		}
	else
		{
		cout << "\nMolto bene, " << name << ". " << strage3 << age << " blablabla." << endl << endl;
		_getch();
		}
    return(0);
}


and use ( "\n\n" ) instead of ( << endl << endl )
Last edited on
May I ask why "\n\n" should be used instead of << endl << endl?
May I ask why "\n\n" should be used instead of << endl << endl?

It's an aesthetic reason. It just looks shorter and cleaner, but they functionally do the same thing.
closed account (1CfG1hU5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
//#include<cmath>  // not necessary, commented
using namespace std;

int main()
{
int age;
   
cout << "put a number 30 to 50: ";
cin >> age;
	
  while (age <= 29 || age >= 51)
    {
      cout << endl << "try again, number 30 to 50: ";
      cin >> age;
    }   

    cout << endl <<"the age you entered is " << age;

return 0;
}


simpler is better. started with a simple if statement, you get a simple while loop instead.
Last edited on
closed account (1CfG1hU5)
1
2
3
4
5
cout << "text" << endl;

         or

cout << "text" << "\n";


would not exclude either. both are good. endl easier to type.

is there a way to define endl so that does down two lines without type that twice?

int i;
i = endl + endl; ???

a guess
Last edited on
Topic archived. No new replies allowed.