Input Checks

This is a discussion on Input Checks within the C Programming forums, part of the General Programming Boards category; Hi all, I need to write a program which you input up to 10 numbers, and it sorts them into ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    3

    Input Checks

    Hi all,

    I need to write a program which you input up to 10 numbers, and it sorts them into ascending, descending and random ordering.

    It also needs to alert the user when a float or a char is entered, as it can only take integers.

    The rest is pretty much sorted, but how would I go about altering the program such that the user is alerted and re-prompted to enter the value? I'm guessing the main function will have to be re-written.

    The program:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    /*Prototype declarations */
    
    void sort(int inputValues[], int promptCount);
    
    int main()
    {
    	int inputValues[10], promptCount, count1, count2;
    	inputValues[10] = 0;
    	printf("Please Enter Your Values (as Integers - up to 10 Values Maximum). If you require less than 10 values, please enter any negative Integer.\n");
    	
    	for(promptCount = 0; promptCount < 10; promptCount++)
    		{	
    			printf("\nEnter the #%d value:", (promptCount+1));
    			
    			scanf("%d", &inputValues[promptCount]);
    		}
    	
    	sort(inputValues, promptCount);
    	
    	printf("\nArranged in Ascending Order:");
       for (count1 = 0; count1 < 10; count1++)
    			printf(" %d,", inputValues[count1]);
    	
    	printf("\nArranged in Descending Order:");
       for (count2 = 9; count2 > -1; count2--)
    			printf(" %d,", inputValues[count2]);
    }
    
    void sort(int inputValues[], int promptCount)
    /* Bubble Sort. */
    {
    	int i, j, temp;
    	for(i = 0; i < (promptCount); i++)
    		{
    			for (j = 0; j < (promptCount - i - 1); j++)
    				{
    					if( inputValues[j] > inputValues[j + 1] )
    						{
    							temp = inputValues[j];
    							inputValues[j] = inputValues[j + 1];
    							inputValues[j + 1] = temp;
    						}
    				}
    		}
    }
    Last edited by Elements; 04-25-2006 at 03:59 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > scanf("%d", &inputValues[promptCount]);
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    Basically,
    - read a line using fgets
    - validate the line in some way
    - convert the line in some way

    There are many possible choices for what to do once you've read the line from the user.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    3
    Thank you very much for your extremely useful help.

    But I also need to ask the following:

    1. I have 10 or less figures to work with (dependant on how many figures the user wishes to enter), and I need to randomise the figures (used once and once only) so that each input is put in a different, random spot to when it was inputted to the program.

    Any ideas?

    2. In the following Code, I presume the first if statement is there to see if the input is blank (just hit enter), and state: "Error reading input". It doesn't seem to work - any idea why? Is there another #include I should have?


    Code:
    if ( fgets ( buffer, sizeof buffer, stdin ) != NULL ) 
      				{
        				buffer[strlen ( buffer ) - 1] = '\0';
       				if ( validate ( buffer ) == 0 ) 
       					{
          					input[promptCount] = atoi ( buffer );
        					}
        					else if( validate ( buffer ) == 2 )
    		    				break;
        					else
        					{
    
          					printf ("Error: Input is not an Integer. Please re-enter a valid integer.\n");
          					promptCount--;
          				}
      				}
      			else
      				{
        				printf ( "Error reading input\n" );
        				promptCount--;
        			}

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > I presume the first if statement is there to see if the input is blank (just hit enter),
    No, fgets() returns NULL at EOF, not on an empty line
    Use another inner if() statement to check for a string length of zero, after you've done removing the \n.

    > Any ideas?
    Maybe a loop which swaps pairs of elements of the array you've input?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    3
    Thanks again,

    I had thought about the swapping of particular elements of the array - but as far as I'm aware, the output has to be random in order, with the same values as the input.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    Well, you could index the array of numbers you got from the user at random, and print them out. There is excellent information on the website for using random numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input redirection
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 06-25-2009, 01:59 AM
  2. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  3. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 12:22 AM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. Help with Input Checking
    By Derek in forum C Programming
    Replies: 7
    Last Post: 06-17-2003, 03:07 AM

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