Thread: help with input check

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    9

    help with input check

    I need to check if the user is ,for example, typing a letter insted of a number or that he is typing too many pieces ,for example
    if i asked him to Enter an array of size 3 and he by mistake puts 4.
    how do I check this?



    this is part of the main function only:
    Code:
    void main() {
    	
    	int width[max_pieces], // the array of the pieces' heights
    		height[max_pieces], // the array of the pieces' widths
    		i, // index
    		numH,numW,numP,//heights widths and number of pieces.
    		puzzle[361];//the array of the puzzle.
    	printf("Enter puzzle height( < 20 ) width( < 20 ) number of pieces( <= 10 )\n");
    	scanf("%d%d%d",&numH,&numW,&numP);
    
    	while(numH>=20||numW>=20||numP>=10){
    		printf("Incorrect input ,try again\n");
    		printf("\nEnter puzzle height( < 20 ) width( < 20 ) number of pieces( <= 10 )\n");
    	scanf("%d%d%d",&numH,&numW,&numP);
    	}//while
    	
    	printf("Enter array of size %d of piece heights:\n",numP);
    	for (i=0; i<numP; i++)
    		scanf("%d",&height[i]);
    	
    	printf("Enter array of size %d of piece widths:\n",numP);
    	for (i=0; i<numP; i++)
    		scanf("%d",&width[i]);

  2. #2
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    All that falls under input validation, and it's a broad topic. You can test for whether the user enters a number or not by reading everything as a string and then checking the string for validity:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int is_number(char *s) {
      char *e;
    
      strtol(s, &e, 0);
    
      return *e == '\0';
    }
    
    int main() {
      char *n, s[128];
    
      if (fgets(s, sizeof s, stdin)) {
        n = strchr(s, '\n');
        if (n) *n = '\0';
        if (is_number(s))
          puts("It's a number");
        else
          puts("Not a number");
      }
    
      return 0;
    }
    Or you can use the return value of scanf to see if the conversion worked. If it didn't work then you can assume an invalid format:
    Code:
    #include <stdio.h>
    
    int main() {
      int i;
    
      if (scanf("%d", &i) == 1)
        puts("It's a number");
      else
        puts("Not a number");
    
      return 0;
    }
    But that's not as complete because it only tests up to the first invalid character, not the whole line like the first example. If the user is typing too many pieces, you can just ignore the extra ones or remove them when you want to ask for more input. That's an easy fix and it usually works great.
    Just because I don't care doesn't mean I don't understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 10:52 AM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  3. allegro issues
    By mramazing in forum C++ Programming
    Replies: 1
    Last Post: 01-07-2009, 11:56 PM
  4. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 06:41 PM
  5. check if user input matches a word
    By fakebloo in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2004, 07:12 PM