CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Join Date
    Mar 2009
    Posts
    23

    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???

  2. #2
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    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

  3. #3
    Join Date
    Mar 2009
    Posts
    23

    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 );

  4. #4
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    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

  5. #5
    Join Date
    Aug 2008
    Location
    India
    Posts
    186

    Wink Re: do while question

    Quote Originally Posted by Vanilla_Rice View Post
    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....

  6. #6
    Join Date
    Mar 2009
    Posts
    23

    Re: do while question

    thanks loser 007

    can anyone point me in the direction of a good free C compiler?

  7. #7
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: do while question

    Quote Originally Posted by Vanilla_Rice View Post
    can anyone point me in the direction of a good free C compiler?
    Visual C++ Express Edition.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: do while question

    Quote Originally Posted by LOOSER_007 View Post
    Replace with this- while ( (number >= 2.5) && (number<= 6.5) );
    I'd omit the superfluous parentheses myself.

  9. #9
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    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

  10. #10
    Join Date
    Aug 2008
    Location
    India
    Posts
    186

    Wink Re: do while question

    Quote Originally Posted by krmed View Post
    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...

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: do while question

    Quote Originally Posted by LOOSER_007 View Post
    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.

  12. #12
    Join Date
    Mar 2009
    Posts
    23

    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?

  13. #13
    Join Date
    Aug 2008
    Location
    India
    Posts
    186

    Unhappy Re: do while question

    Quote Originally Posted by GCDEF View Post
    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.

  14. #14
    Join Date
    Aug 2008
    Location
    India
    Posts
    186

    Wink Re: do while question

    Quote Originally Posted by Vanilla_Rice View Post
    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....

  15. #15
    Join Date
    Mar 2009
    Posts
    23

    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured