|
-
March 31st, 2009, 12:21 AM
#1
do while question
I have to use the "do while" statement for the following question in my programming class:
Prompt for a number between 2.5 and 6.5 (loop until a number is correctly inputted)
If the number is outside the specified range, display an error message and ask for another number.
Help needed thankyou. I dont know how to write this???
-
March 31st, 2009, 12:56 AM
#2
Re: do while question
Hi,
You don't know how to write this?
If someone will do it for you you won't learn anything.
Please write what you have so far and where your problem is!
And refer to the Before you post section and the FAQ's first!
With regards
Programartist
-
March 31st, 2009, 01:33 AM
#3
Re: do while question
ok sorry here is what I have done already, I am not sure if what I have put in the while bracket is correct, also I am unsure how to go about displaying the error message and asking for another number.. I have searched through my text book but its written in a way that I cant understand hence why im asking here.. thanks in advance
Code:
double number;
do
{
Printf("Please enter a number between 2.5 and 6.5");
fflush(stdin);
scanf("%lg", &number);
}while ( number >= 2.5 & <= 6.5 );
-
March 31st, 2009, 02:00 AM
#4
Re: do while question
*hmh*
Did you give this code any chance to compile?
There are a few errors in the code you posted.
Some will cause compiler errors.
Please try to compile this code, read the error messages the compiler will give you and try to understand what they mean.
If you can change your code so that it can be compiled without errors then please try to execute it. If the program won't do what you're expecting then use the debugger and execute it step by step.
(Of course I could tell you the errors (or at least some of them). But you should learn something, shoudn't you?)
With regards
Programartist
-
March 31st, 2009, 03:00 AM
#5
Re: do while question
 Originally Posted by Vanilla_Rice
while ( number >= 2.5 & <= 6.5 );
[/code]
&- Bitwise AND operation
&&- Logical AND operation.
Replace with this- while ( (number >= 2.5) && (number<= 6.5) );
Refer some programming books to improve your basics....
-
March 31st, 2009, 04:33 AM
#6
Re: do while question
thanks loser 007
can anyone point me in the direction of a good free C compiler?
-
March 31st, 2009, 05:20 AM
#7
Re: do while question
 Originally Posted by Vanilla_Rice
can anyone point me in the direction of a good free C compiler?
Visual C++ Express Edition.
-
March 31st, 2009, 06:55 AM
#8
Re: do while question
 Originally Posted by LOOSER_007
Replace with this- while ( (number >= 2.5) && (number<= 6.5) );
I'd omit the superfluous parentheses myself.
-
March 31st, 2009, 07:19 AM
#9
Re: do while question
Since you want to stay in the loop until a number is within the range, you need to change
Code:
while ( (number >= 2.5) && (number<= 6.5) );
to this
Code:
while ( (number <= 2.5) || (number>= 6.5) );
Hope that helps.
Be sure to rate those who help!
-------------------------------------------------------------
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
March 31st, 2009, 07:36 AM
#10
Re: do while question
 Originally Posted by krmed
Since you want to stay in the loop until a number is within the range, you need to change
Code:
while ( (number >= 2.5) && (number<= 6.5) );
to this
Code:
while ( (number <= 2.5) || (number>= 6.5) );
Hope that helps.
I don't Think so.. e.g. What if number = 1.0
so While((1.0<2.5 is = TRUE || (Anything)) = TRUE) so the loop still goes on...
-
March 31st, 2009, 08:04 AM
#11
Re: do while question
 Originally Posted by LOOSER_007
I don't Think so.. e.g. What if number = 1.0
so While((1.0<2.5 is = TRUE || (Anything)) = TRUE) so the loop still goes on...
He wants to prompt until a number is in that range. If the number is 3 for example, both conditions are false and the loop ends as required.
-
March 31st, 2009, 09:54 PM
#12
Re: do while question
lets say I use
while ( (number >= 2.5) && (number<= 6.5) );
I think I wrote it wrong, should'nt it be:
while ( (number < 2.5) && (number > 6.5) );
looking at the less than and greater than statements these appear to be the wrong way around to me, because if i used the first one surely if would continue to ask me to enter a number between 2.5 and 6.5 as that's how I'd interpret it.
your thoughts anyone?
-
March 31st, 2009, 10:45 PM
#13
Re: do while question
 Originally Posted by GCDEF
He wants to prompt until a number is in that range. If the number is 3 for example, both conditions are false and the loop ends as required.
Oh... Its my mistake i didn't read the question well...I was interpreting it the other way..... What krmed has written is absolutely right...
while ( (number <= 2.5) || (number>= 6.5) );
Best Regards
Last edited by LOOSER_007; March 31st, 2009 at 10:51 PM.
-
March 31st, 2009, 10:50 PM
#14
Re: do while question
 Originally Posted by Vanilla_Rice
lets say I use
while ( (number >= 2.5) && (number<= 6.5) );
I think I wrote it wrong, should'nt it be:
while ( (number < 2.5) && (number > 6.5) );
looking at the less than and greater than statements these appear to be the wrong way around to me, because if i used the first one surely if would continue to ask me to enter a number between 2.5 and 6.5 as that's how I'd interpret it.
your thoughts anyone?
while ( (number < 2.5) && (number > 6.5) ) this condition always result in FALSE as the number cannot be <2.5 and >6.5 at the same time.... What krmed has mentioned i.e use LOGICAL OR || for your while loop condition....
-
March 31st, 2009, 10:50 PM
#15
Re: do while question
ahh thats good to know, to be honest i didnt read krmed's post correctly eigther..
Last edited by Vanilla_Rice; March 31st, 2009 at 10:53 PM.
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
|