Thread: Error if reading char or float

  1. #1
    Registered User
    Join Date
    Jun 2007

    Error if reading char or float

    i made a simple program to take in two integers and evaluate their sum, product, etc... The problem is, Im wanting to display an error message if the user enters a character or a decimal number, but I have been unsuccessful achieving this. Please help!

    here is my program:

    #include <stdio.h>
    int main(void)
    		int num1=0, num2=0;
    		int sum, diff1, diff2, product, quotient, average;
    		printf("Please enter the first integer:\n");
    		printf("Please enter the second integer:\n");
    		sum = num1 + num2;
    		diff1 = num1 - num2;
    		diff2 = num2 - num1;
    		product = num1*num2;
    		quotient = num1/num2;
    		average = (num1 + num2)/2;
    		if(num1 == num2)
    				printf("\nCONGRATULATIONS!  The two integers you've entered are equal!\n\n");
    		printf("The sum of the integers is equal to:%d\n", sum);
    		printf("The difference of integer 1 minus integer 2 is equal to:%d\n",diff1);
    		printf("The difference of integer 2 minus integer 1 is equal to:%d\n",diff2);
    		printf("The product of the integers is equal to:%d\n",product);
    		printf("The quotient of the integers is equal to:%d\n",quotient);
    		printf("The average of the integers is equal to:%d\n", average);

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007

    Welcome to a well-known question.

    Quick summary of options:

    1) Check the return value of scanf().
    2) Use fgets()/sscanf() to get a number.
    3) Use fgets()/strtol() to get a number.
    4) Write your own number-getting function.

  3. #3
    Math wizard
    Join Date
    Dec 2006
    One way is to use a string instead and parse it checking for only the numerical characters. That is, something like this:

    if (((string[ArrayIndex] >= '0') && (string[ArrayIndex] <= '9')) || (string[ArrayIndex] == 0)) // 0 to 9 characters or the null character
    	// it's valid and the next character should be checked, except if the null character is encountered
    	// it's not valid and an error should be displayed without continuing any further
    This is just a rough concept and probably a poor/inefficient method. With the string being valid, use atoi to convert the string into a number.

    Edit: a better method was posted while I typed the above
    Last edited by ulillillia; 06-07-2007 at 10:14 AM. Reason: better method posted before my message
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  4. #4
    Registered User
    Join Date
    Jun 2007
    thx guys
    i modified my program to read the scanf()
    and it now works!

    thanks for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  2. Reflective Factory Design
    By Shamino in forum Game Programming
    Replies: 4
    Last Post: 12-16-2005, 06:50 PM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 05:13 PM
  5. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM