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

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    25

    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. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by eeengenious View Post
    while (max_Number < 2 || max_Number > 50,000)
    That doesn't work.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    What would be the proper way to write it?

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Get rid of the comma.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    Oh wow, I didnt know a comma could throw it off.

    Thanks.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    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. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    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. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by eeengenious View Post
    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. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    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. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #14
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    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. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Loops and printf... get it started, then post the code segment and we'll see what we can do...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  3. problem : input and calculation working together
    By itay390 in forum C Programming
    Replies: 13
    Last Post: 07-30-2005, 12:32 PM
  4. Quick C Question - checking buffer for input
    By sean in forum C Programming
    Replies: 3
    Last Post: 11-13-2004, 12:23 PM
  5. Checking input
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-26-2002, 03:06 AM