# Thread: Help - Decision in a do loop

1. ## 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. 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)

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

4. 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; )
{