Okay, I need some help with "if" statements.
I'm trying to get across to the program...
if theMonth == 4
AND
theDate %26gt;= 1 %26amp;%26amp; theDate %26lt;=19
THEN
(say a statement)
I've tried it as...
cout %26lt;%26lt; "So, what day where you born?" %26lt;%26lt; endl;
int theDate;
cin %26gt;%26gt; theDate;
if((theMonth==4)%26amp;%26amp;(theDate %26gt;=1 %26amp;%26amp; theDate %26lt;= 19))
cout %26lt;%26lt; "I didn't know you were an Aries!" %26lt;%26lt; endl;
But it isn't working! All that happens is when it's executed, it shows..
"So, what day where you born?"
And you input a number between 1 and 19, it asks you to hit another key to close the program.
For the record, earlier in the coding, I did...
int theMonth
cout %26lt;%26lt; "What month were you born?";
cin %26gt;%26gt; theMonth
(switch/case stuff)
cout %26lt;%26lt; "Ah, so you were born in (month)".
I follow the instructions correctly. Input four, get it to say April. Then later I input a number like 15 for the date, and it doesn't work.
Help?
C++ help for someone using the program for the first time today?
EDIT: Just seen your full program - birthMonth is a char and therefore fails the test
if(birthMonth == 4) .
Change the condition to if(birthMonth == '4')
This is a working version of your program:
#include %26lt;conio.h%26gt;
#include %26lt;iostream.h%26gt;
void main()
{
int theMonth, theDate;
cout %26lt;%26lt; "So, what month where you born?" %26lt;%26lt; endl;
cin %26gt;%26gt; theMonth;
cout %26lt;%26lt; "So, what day where you born?" %26lt;%26lt; endl;
cin %26gt;%26gt; theDate;
if((theMonth==4)%26amp;%26amp;(theDate %26gt;=1) %26amp;%26amp; (theDate %26lt;= 19)) {
cout %26lt;%26lt; "I didn't know you were an Aries!" %26lt;%26lt; endl;
}
getch();
}
It's always best to put brackets around if and else statements by habit like this:
if(month == 4)
{
// code
}
else
{
// code
}
Reply:On your If statement be sure you have { open brackets after the criteria and } at the end. I did not see that unless your using ( and ). Which is not the correct symbol for the if statement to be closed in the code.
Reply:not enough information. Doesn't look like C++ in the first place. You'll need to post the actual code (copy/paste) and then you need to say what compiler you are using and what operating system and so on.
The IF statement must be followed by a complete boolean expression in parentheses. In one of your code snippets you have two expressions following the IF... I'm surprised it compiles that way.
If you have a choice, switch to C# right away. It is a much better language, and it will catch programming errors like the one I mention. It won't compile that and you'll know right away what you did wrong. You are missing brackets and so on... and C++ is doing exactly what you told it to do... basically nothing. You don't have a code block after the IF....
marguerite
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment