Oct 3, 2014 at 3:52pm Oct 3, 2014 at 3:52pm UTC
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 Oct 3, 2014 at 3:53pm Oct 3, 2014 at 3:53pm UTC
Oct 3, 2014 at 4:07pm Oct 3, 2014 at 4:07pm UTC
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.
Oct 3, 2014 at 4:13pm Oct 3, 2014 at 4:13pm UTC
Yes. If the value is greater than 30, it is always 50. the number is indifferent as long as it is higher than 30.
Oct 3, 2014 at 4:20pm Oct 3, 2014 at 4:20pm UTC
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?
Oct 3, 2014 at 4:22pm Oct 3, 2014 at 4:22pm UTC
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.
Oct 3, 2014 at 4:58pm Oct 3, 2014 at 4:58pm UTC
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.
Oct 3, 2014 at 10:29pm Oct 3, 2014 at 10:29pm UTC
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 Oct 3, 2014 at 10:35pm Oct 3, 2014 at 10:35pm UTC
Oct 12, 2014 at 8:13pm Oct 12, 2014 at 8:13pm UTC
May I ask why "\n\n" should be used instead of << endl << endl?
Oct 12, 2014 at 8:36pm Oct 12, 2014 at 8:36pm UTC
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 Oct 12, 2014 at 9:20pm Oct 12, 2014 at 9:20pm UTC
Oct 12, 2014 at 9:03pm Oct 12, 2014 at 9:03pm UTC
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 Oct 12, 2014 at 9:06pm Oct 12, 2014 at 9:06pm UTC