Thread: Code not working properly.

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    7

    Code not working properly.

    Hello, I am having issues with this program I created, the goal of this program is for the user to input numbers and it will display the sum of the numbers. If a user enters in any letters then it will give the message "Please enter only numbers.". The problem I have is if you enter in numbers it still says "Please enter only numbers.". Any help is truly appreciated.

    Code:
    #include<stdio.h>
    #include<ctype.h>
    
    int main()
    {
       int num1, num2, sum;
    
       printf("Please enter numbers for me to add.\n");
    
       scanf("%d", &num1);
       scanf("%d", &num2);
       sum = (num1 + num2);
    
       if (isdigit(num1)==1 && (num2)==1 )
        printf("%d + %d = %d\n", num1, num2, sum);
       else
        printf("Please enter only numbers.\n");
       return 0;    
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    isdigit - is used for testing string contents. You do not have strings

    If user enters something different from number - scanf will fail - so you need to test the return value.

    Or easier - use fgets to read user input as string

    use strtol to convert the string to number

    and examine the end pointer - to be sure all the input was consumed by the converting function...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    7
    Hello Vart, thank you for you input, I'm going to read about fgets, strtol and isdigit so I can get a better understanding of it and then modify this code.

  4. #4
    Registered User zedd's Avatar
    Join Date
    Jun 2015
    Posts
    14
    Another way is to check the values returned by the two scanf functions.

    scanf returns the number of item successfully read, and in this case if they can read two integer numbers, both scanf may return the value 1.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    This thread is talking about similar problem Help with isdigit?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    7
    Hello, thank you for your time and suggestions. I picked up C Programming Language 2nd edition and have been reading it and following along with their exercises. I haven't gotten up to the isdigit in the book yet but with some of their exercise I was able to fix the issue.
    Code:
    #include<stdio.h>
    
    main()
    {
       int num1, num2;
    
       printf("Please enter numbers for me to add.\n");
    
       scanf("%d", &num1);
       scanf("%d", &num2);
    
       if (num1 == 0 || num2 == 0)
        printf("Enter numbers only.\n");
       else   
        printf ("%d + %d = %d\n", num1, num2, (num1 + num2));
       return 0;
    }
    In the future I will try to update this program so the addition will be in another function. So if the user types in a letter it will print out for the user to enter numbers only then restart. Once again thank you all :-)
    Last edited by iiwii; 06-14-2015 at 04:03 PM.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Your num1 and num2 are not initialized, why do yo think they will be equal to 0 if scanf failed? - check the return value of scanf instead
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by vart View Post
    isdigit - is used for testing string contents. You do not have strings
    "isdigit()" is used for testing characters, which do not necessarily have to be part of a string.

  9. #9
    Registered User
    Join Date
    Jun 2015
    Posts
    7
    Quote Originally Posted by vart View Post
    Your num1 and num2 are not initialized, why do yo think they will be equal to 0 if scanf failed? - check the return value of scanf instead
    I'm not sure why I put that, I was reading something either online or from the book. The code works when I compile and run it in linux (Fedora21) but does not work when I compile and run in Windows.
    I though the way I did it was checking the return values from scanf. Can you explain how to?
    Code:
    if (scanf("%d %d", &num1, &num2)==0)
                printf("Please enter only numbers\n");
    else
                 printf("%d + %d = %d\n", num1, num2, (num1 + num2));

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    How does it "not work" when compiled in Windows?

    Code:
    if (scanf("%d %d", &num1, &num2)==0)
    If you want to make sure both numbers have been successfully read, you should do the comparison like so:

    Code:
    if (scanf("%d %d", &num1, &num2) != 2)  // if 2 numbers have not been read, error

  11. #11
    Registered User
    Join Date
    Jun 2015
    Posts
    7
    What's up Matticus, thank you for your response,
    does the 2 mean error inputting? for example if someone didnt type anything or enter in a space or tab?? I used 0 because when I entered in letters before it would show up in the printf, but I want users only to type in numbers. Sorry if my questions are frustrating, I'm still in noobie mode and having a hard time with some concepts .

  12. #12
    Registered User
    Join Date
    Jun 2015
    Posts
    7
    Before after I compiled the code in Windows if a user entered any letter it would add that, but with this updated code it work in linux and windows.
    Code:
    #include<stdio.h>
    
    main()
    {
       int num1, num2;
    
       printf("Please enter numbers for me to add.\n");
    
       if (scanf("%d %d", &num1, &num2) == 0)
        printf("Enter numbers only.\n");
       else
        printf ("%d + %d = %d\n", num1, num2, (num1 + num2));
       return 0;
    }

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should get into the habit of doing searches for how functions work.

    scanf(3): input format conversion - Linux man page

    "scanf()" returns the number of values successfully read. In your case, you're scanning for two numbers. If it successfully reads two numbers, it will return 2. If it does not, you know the input was not entered correctly.

    By checking the return value against zero, there's still a chance that only one number was read (and "scanf()" would return 1) which will still execute the addition, even though it shouldn't.

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by iiwii View Post
    Before after I compiled the code in Windows if a user entered any letter it would add that, but with this updated code it work in linux and windows.
    Code:
    #include<stdio.h>
    
    main()
    {
       int num1, num2;
    
       printf("Please enter numbers for me to add.\n");
    
       if (scanf("%d %d", &num1, &num2) == 0)
        printf("Enter numbers only.\n");
       else
        printf ("%d + %d = %d\n", num1, num2, (num1 + num2));
       return 0;
    }
    To prove the point I made, try running your program and entering one number and one letter.

    Code:
    /*
    Please enter numbers for me to add.
    1 a
    1 + 57 = 58
    */
    The 1 was valid, the 'a' was not, so "scanf()" returned 1 and your code went on to do the addition. (You can also print the return value of "scanf()", which might be a good experiment for you to try).

    If you change the condition as I've previously stated, you get:

    Code:
    /*
    Please enter numbers for me to add.
    1 a
    Enter numbers only.
    */
    This reduces the possibility of operating on bad input.

  15. #15
    Registered User
    Join Date
    Jun 2015
    Posts
    7
    I misunderstood when "return value" was mentioned, I though it was the value of the variable num1 or num2. After you mentioned that the input "1" and "a" returns a value of 1 I looked online for more information, I found this site https://www.hackerearth.com/notes/printf-scanf-in-c/ does that mean if I use three %d in scanf, there should be a return value of 3?
    What if it was the other way around and I wanted users to input only letters, if I use scanf with %c and a user enters a digit will it return a 0 because I used a %c instead of %d?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inequality code doesn't seem to be working properly
    By Sinca in forum C++ Programming
    Replies: 1
    Last Post: 03-12-2013, 11:48 PM
  2. Numbering of code not working properly
    By std10093 in forum General Discussions
    Replies: 14
    Last Post: 09-28-2012, 09:41 AM
  3. Replies: 1
    Last Post: 07-17-2012, 12:56 AM
  4. Reducing rational numbers - code not working properly
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 07:42 AM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM