Help - Decision in a do loop

This is a discussion on Help - Decision in a do loop within the C Programming forums, part of the General Programming Boards category; Help! I need to make 2 decisions in my code and cannot figure out how to write the code that ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    1

    Unhappy Help - Decision in a do loop

    Help! I need to make 2 decisions in my code and cannot figure out how to write the code that will return the user to the scanf point once an error message has appeared.

    do
    {
    printf("Enter the number of deposits (0-50): ");
    scanf ("%i", &number_of_deposits);

    if (number_of_deposits <= 0)
    printf("ERROR! - Please enter a non-negative number!\n");
    else (number_of_deposits >=50 );
    printf ("***Too many deposits.\n");

    } while (number_of_deposits <= 0);

    I can get the first error statement to return to the "Enter the number of deposits", but not if data is entered to make the second error appear. Any ideas?

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    27
    one sugestion I would have is that for simple program like that is to not us scanf. scanf can be tricky sometimes and if you just starting off you better off trying something else. if you want something real easy try using this getline function below to read in a line.

    Code:
    int getline(char line[], int max)  // line is the variable to read into, max is max number of char to read
    
    int nch = 0;
    int c;
    max = max - 1;
    
    while((c = getchar()) != EOF)
    {
         if(c == '\n')
              break;
         if(nch < max)
         {
              line[nch] = c;
              nch = nch + 1;
         }
    }
    
    if(c == EOF && nch == 0)
         return EOF;
    
    
    line[nch] = '\0';
    return nch;
    }
    then if you need it to be an int just use atoi

    also I think you want that else to be an else if(number_of_deposits >= 50)

    and for your while at the bottom won't take when the number entered is greater than 0 so you should change it to

    while(number_of_deposits >= 50 || number_of_deposits <= 0)
    Last edited by samps005; 12-05-2002 at 11:28 AM.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    Code:
    do{
    printf("Enter the number of deposits (1-50): ");
    scanf ("%i", &number_of_deposits);
      if(number_of_deposits > 50 ){
        printf ("***Too many deposits.\n");
      }
      else
      if (number_of_deposits <= 0){
        printf("ERROR! - Please enter a non-negative number!\n"); 
      }
    } while (number_of_deposits <= 0 || number_of_deposits > 50);
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    And one more for luck:
    Code:
    #include <stdio.h>
    
    #define NUM_MIN 1
    #define NUM_MAX 49
    
    int main(void)
    {
        int mynum;
        int Invalid;
        
        for (Invalid = 1; Invalid == 1; )
        {
            printf ("Enter your number: ");
            if (scanf("%d", &mynum) == 1 && mynum >= NUM_MIN && mynum <= NUM_MAX)
                Invalid = 0;
            while (getchar() != '\n');
        }
        
        printf ("You entered %d\n", mynum);
        
        return 0;
    }
    But I'd still prefer to use fgets() and strtol().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21