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

    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("&#37;lg", &number);
    
    }while ( (number <= 2.5) || (number>= 6.5) );
    
    fflush(stdin);
          getchar();
          return 0;
    }

  2. #17
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    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

  3. #18
    Join Date
    Aug 2008
    Location
    India
    Posts
    186

    Talking 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...

  4. #19
    Join Date
    Mar 2009
    Posts
    23

    Re: do while question

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

  5. #20
    Join Date
    Mar 2009
    Posts
    23

    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("&#37;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;

  6. #21
    Join Date
    Feb 2009
    Posts
    42

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

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

    Re: do while question

    Quote Originally Posted by streamer View Post

    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.

  8. #23
    Join Date
    Aug 2008
    Location
    India
    Posts
    186

    Red face Re: do while question

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

  9. #24
    Join Date
    Feb 2009
    Posts
    42

    Re: do while question

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

Page 2 of 2 FirstFirst 12

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