# Thread: Checking for Invalid Input...Not Working?

1. ## Checking for Invalid Input...Not Working?

Valid input for this program is between 2 and 50,000.
However, whenever I try a number less than 2 or greater than 50,000, it still goes through the functions.

What's wrong? Appreciate the help.

Code:
```int max_Number;
printf("This program wills calculate all primes up to the number you specify.\n");
printf("\nEnter a number between 2 and 50,000: ");
scanf("%d" , &max_Number);

while (max_Number < 2 || max_Number > 50,000)
{
printf("Invalid Input. Enter a Number Between 2 and 50,000: ");
scanf("%d" , &max_Number);
if (max_Number > 2 && max_Number < 50,000) {
break;
}

}

Sieve (max_Number);
Palindrome (count);
CPSP (count);

getchar();
}```

2. Originally Posted by eeengenious
while (max_Number < 2 || max_Number > 50,000)
That doesn't work.

3. What would be the proper way to write it?

4. Get rid of the comma.

5. Oh wow, I didnt know a comma could throw it off.

Thanks.

6. It's the coma in 50,000 that's killing you...

Something like this will probably work better...
Code:
```int max_Number = 0;  // ALWAYS initialize.

do
{ printf("Enter a Number Between 2 and 50,000: ");
scanf("%d" , &max_Number);
if (max_Number > 2 && max_Number < 50000) {     // no comma 50000  not 50,000
break;
else
printf("/nError... "); }
while (max_Number < 2 || max_Number > 50000)   // no commas!```

7. Thanks

Also trying to get it such that the process repeats whenever the user inputs 'Y' or 'y' and quits when 'N' or 'n'

Not sure if I'm going about it the right way, thinking I'm making it too complex:

Code:
```int main() {

int max_Number;
printf("This program wills calculate all primes up to the number you specify.\n");
printf("\nEnter a number between 2 and 50,000: ");
scanf("%d" , &max_Number);

while (max_Number < 2 || max_Number > 50000)
{
printf("\nInvalid Input. Enter a Number Between 2 and 50,000: ");
scanf("%d" , &max_Number);
if (max_Number > 2 && max_Number < 50000) {
break;
}

}

Sieve (max_Number);
Palindrome (count);
CPSP (count);

printf("\nIf you would like to run the program with another input, please enter 'Y.' Otherwise, enter 'N' to Quit");
while (getchar() != 'N' || 'n') {
if (getchar() == 'Y' || getchar() != 'y') {
printf("\nEnter a number between 2 and 50,000: ");
scanf("%d" , &max_Number);

while (max_Number < 2 || max_Number > 50000)
{
printf("\nInvalid Input. Enter a Number Between 2 and 50,000: ");
scanf("%d" , &max_Number);
if (max_Number > 2 && max_Number < 50000) {
break;
}

}

Sieve (max_Number);
Palindrome (count);
CPSP (count);

}

}

}```

8. You need to make the whole thing into a do -- while() loop...

Think about the flow of events... how often can they run it now?

To exit the loop assume that anything except Y or y means no and let it exit. It is safer for the program to bail then for it to get stuck in a loop because the usuer is too stupid to read and follow the prompts.

9. Okay, I have it in a do-while loop like this.

Where would the character stuff go?

Code:
```int main() {

int max_Number = 0;
printf("This program wills calculate all primes up to the number you specify.\n");

do

{
printf("\nEnter a Number Between 2 and 50,000: ");
scanf("%d" , &max_Number);
if (max_Number > 2 && max_Number < 50000) {     // no comma 50000  not 50,000
break;
}
else
printf("\nInvalid Input. Please enter a number between 2 and 50,000\n"); }
while (max_Number < 2 || max_Number > 50000);   // no commas!

Sieve(max_Number);
Palindrome (count);
CPSP (count);

getchar();

}```

10. Wrong logic for C here:

Code:
`if (getchar() == 'Y' || getchar() != 'y')`
You don't want TWO getchar()'s. You want just one getchar(), and two checks from what it returns:

Code:
```c = getchar()
if(c == 'Y' || c == 'y') {
//do something here
}```
This is also a bug:
Code:
`while (getchar() != 'N' || 'n') {`
The " || 'n' " part of it is lacking a the char to compare with. Only the 'N' is comparing OK. This bug is related to the first one, and has the same cure: Use one getchar() and two FULL comparisons.

If you want to mix char and number input, you need to clean the input stream of the keyboard (which will have the left over newline). Either add a getchar() after the scanf()'s, or your program may loop endlessly when it next expects a number, and the user gives it a letter, instead.

11. Originally Posted by eeengenious
Okay, I have it in a do-while loop like this.

Where would the character stuff go?
Try it something like this. The code is "top of my head" and untested so you probably should not scoop and poop it... type it up by hand and test as you go.

Code:
```int main()
{ int max_Number;
char u_Repeat;

do
{ max_Number = 0;
u_Repeat = 0;

printf("This program wills calculate all primes up to the number you specify.\n");
do
{
printf("\nEnter a Number Between 2 and 50,000: ");
scanf("%d" , &max_Number);
if (max_Number > 2 && max_Number < 50000)
break;
else
printf("\nInvalid Input. Please enter a number between 2 and 50,000\n"); }
while (max_Number < 2 || max_Number > 50000);

Sieve(max_Number);
Palindrome (count);
CPSP (count);

printf ("Again? (Y/N) : ");
u_Repeat = getchar();
}
while (u_Repeat = 'Y' || u_Repeat = 'y')

return 0;
}```

12. Here's what I have:

Getting the error "Expression must be a modifiable lvalue for the 'y' down below:

Code:
```int main() {

int max_Number;
char Repeat;

do

{

max_Number= 0;
Repeat = 0;
printf("This program wills calculate all primes up to the number you specify.\n");

do {
printf("\nEnter a Number Between 2 and 50,000: ");
scanf("%d" , &max_Number);
if (max_Number > 2 && max_Number < 50000) {
break;
}
else
printf("\nInvalid Input. Please enter a number between 2 and 50,000\n"); }
while (max_Number < 2 || max_Number > 50000);

Sieve(max_Number);
Palindrome (count);
CPSP (count);

printf("\nIf you would like to run the program with another input, please enter 'Y.' Otherwise, enter 'N' to Quit");
Repeat = getchar();

}

while (Repeat = 'y' || Repeat = 'Y');

return 0;
}```

13. LOL... my classic mistake!

It should be...
Code:
`while (Repeat == 'y' || Repeat == 'Y');`
single = | & etc are for math operations
doubles are for conditional testing.

I KNOW this... But you would be amazed how many times I do it to myself when coding....

14. Haha, sweet, seems to be working.
Thanks for all your help man.

Last thing, I have to return the list of prime numbers in a specific format:

Display the final set of prime numbers in a 7 column, table format. The prime numbers must be in descending order with the highest prime in row 1, column 1. The numbers must read, in descending order from left to right and top to bottom.
The output table must have exactly 7 columns unless there are fewer than 7 values to display, in which case only a single row is necessary.

Each number must fit into a field of size 10, right justified.

How would I play around with the output formats to get it like this?

15. Loops and printf... get it started, then post the code segment and we'll see what we can do...