|
-
April 1st, 2009, 01:02 AM
#16
Re: do while question
again, thanks for the info guys you've been a great help so far..
but to re-iterate my original question i want the program to display an error message and ask for another number if the inputted number is outside the range I ask for..
so I ask anyone who knows, how would I modify my program below to do that? thanks
Code:
#include <stdafx.h>
#include <stdio.h>
int main (void)
{
double number;
do
{
Printf("Please enter a number between 2.5 and 6.5");
fflush(stdin);
scanf("%lg", &number);
}while ( (number <= 2.5) || (number>= 6.5) );
fflush(stdin);
getchar();
return 0;
}
-
April 1st, 2009, 02:14 AM
#17
Re: do while question
Hi,
Well, you've a few choices on how to use a do{}while loop for that. You need to display a different message the 2nd and subsequent times through the loop.
You could create a boolean variable, bFirstTime and initialise it as true. Then use something along the lines of
Code:
do
{
if (bFirstTime)
{
// set bFirstTime to false
}
else
{
// print "Sorry, that number's out of range, please try again"
}
// prompt for the number and read it as you are at the moment.
}
Alan
-
April 1st, 2009, 02:18 AM
#18
Re: do while question
Are you not able to get the desired result with the code mention above.....
While loop will run until the entered number is not in your mentioned range...and will ask for other number...and goes on...
-
April 1st, 2009, 10:13 PM
#19
Re: do while question
 Originally Posted by LOOSER_007
Are you not able to get the desired result with the code mention above.....
While loop will run until the entered number is not in your mentioned range...and will ask for other number...and goes on...
Yes I can, although this is an assignment question and I have to follow its instructions.. hence why it needs to print an error message and prompt for another number if the number isnt in the desired range..
thanks for your input looser 07 and alanjhd08
-
April 2nd, 2009, 05:30 PM
#20
Re: do while question
STILL having problems I've emailed my lecturer who replied to me with this:
enter...
if ( condition )
action (the action is "display a clear error message and ask for another number")
It's NOT an if...else at this point, just an if.
Your "condition" is correct (I think).
so yeah, I'd prefer not to use a boolean variable at this point as we havent been taught them yet, the lecturer believes it can be done with an "If" statement although I have made a program containing one and it still doesnt work.. cant someone see any problem with what I have done below? thanks
Code:
#include <stdafx.h>
#include <stdio.h>
int main (void)
{
double number;
do
{
printf("Please enter a number between 2.5 and 6.5: ");
fflush(stdin);
scanf("%lg", &number);
if (number < 2.5) || (number > 6.5)
{
printf("Sorry that number is outside the specified range, please try again");
}
}while ( (number < 2.5) || (number > 6.5) );
fflush(stdin);
getchar();
return 0;
-
April 2nd, 2009, 05:51 PM
#21
Re: do while question
Not taught boolen....eh
You been wrote
if ( condition )
means condition can be true or false
in your case, your condition is that number must be in range 2.5 - 6.5
so condition is "if number smaller than 2.5 or greater than 6.5" then its some error
meaning condition in c++ is: (number < 2.5) || (number > 6.5)
so if you replace condition in if statement above, with (number < 2.5) || (number > 6.5)
you will have
if( (number < 2.5) || (number > 6.5) )
notice the plus parentheses...
-
April 2nd, 2009, 07:24 PM
#22
Re: do while question
 Originally Posted by streamer
if( (number < 2.5) || (number > 6.5) )
notice the plus parentheses...
if(number < 2.5 || number > 6.5)
Does the same thing without the superfluous parenthesis.
-
April 2nd, 2009, 11:26 PM
#23
Re: do while question
 Originally Posted by Vanilla_Rice
Code:
#include <stdafx.h>
#include <stdio.h>
int main (void)
{
double number;
do
{
printf("Please enter a number between 2.5 and 6.5: ");
fflush(stdin);
scanf("%lg", &number);
if (number < 2.5) || (number > 6.5) {
printf("Sorry that number is outside the specified range, please try again");
}
}while ( (number < 2.5) || (number > 6.5) );
fflush(stdin);
getchar();
return 0;
you did'nt closed the if with a bracket....i.e if (number < 2.5) || (number > 6.5) should be
if ((number < 2.5) || (number > 6.5))
I think so.....
-
April 3rd, 2009, 03:52 AM
#24
Re: do while question
 Originally Posted by GCDEF
if(number < 2.5 || number > 6.5)
Does the same thing without the superfluous parenthesis.
Yes I know, but to explain someone who doesn't know boolean type what is precedence of operators, and why that is the same....
Anyway I just wanted him to notice where his problem is, and to understand what is the problem. And my personal opinion is that nobody should write down that: "hey you need to add to code this 10 lines and it will work" style of posting because people who don't know really how to make a code will never learn anything, except that there are lot of users who are willing to help and do they homework.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|