Thread: reading 2 ints and a char

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    11

    reading 2 ints and a char

    Hello
    I want to read 2 integers and 1 character. I have a function named input called from main.
    The code I have is below. But when I check the values read I get different values, i.e

    Enter value 1: 2
    Enter char value 2: M
    Enter value 3: 4
    read values: 2293580 K 2293572

    Code:
    #include <stdio.h>
    #include <string.h>  //not sure needed
    #include <stdlib.h>  // not sure needed
    
    void input(int* val1, char* val2, int* val3);
    
    void main()
    {
        int val1;       // value 1
        char val2;    // value 2
        int val3;          // value 3
    
        input(&val1, &val2, &val3);
    
        printf ("read values: %d %c %d", &val1, &val2, &val3);
    
    }
    
    void input(int* val1, char* val2, int* val3)
    {
        //Input from the user
        printf ("Enter value 1: ");
        scanf ("%d", val1);
        printf ("Enter char value 2: ");
        scanf ("%s", val2);
        printf ("Enter value 3: ");
        scanf ("%d", val3);
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    $ gcc -Wall bar.c
    bar.c:7: warning: return type of ‘main’ is not ‘int’
    bar.c: In function ‘main’:
    bar.c:15: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
    bar.c:15: warning: format ‘%c’ expects type ‘int’, but argument 3 has type ‘char *’
    bar.c:15: warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘int *’

    And you're using %s to read into a single char.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    I'm not returning any values in main. How should the values be sent back from the input function?

    Thanks

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > How should the values be sent back from the input function?
    No, that bit of your code is actually OK.
    It's the use of & in your printf which is screwing up.

    And using %s in a scanf where you should have used " %c" instead.
    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.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    I see what you mean. I removed the & signs from the printf statement on line 15. Now the values for the 2nd and 3rd values show correct but not the first one. Output for 1st value shows as 0 for some reason.

    Thank you

  6. #6
    Registered User DevoAjit's Avatar
    Join Date
    Jun 2011
    Location
    Ludhiana, Punjab, India, India
    Posts
    32
    I don't think so that there is need of pointers... remove the pointers and try diffrent variables of 2 ints and 1 char in void main block..

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Output for 1st value shows as 0 for some reason.
    And did you change
    scanf ("%s", val2);
    to
    scanf (" %c", val2);

    If you read a string, you're going to be trashing memory - like your int variables.
    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.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    Quote Originally Posted by Salem View Post
    > Output for 1st value shows as 0 for some reason.
    And did you change
    scanf ("%s", val2);
    to
    scanf (" %c", val2);

    If you read a string, you're going to be trashing memory - like your int variables.
    Salem
    As soon as I replace the %s with a %c, the program does not accept a value for the second value. The output I get is:

    Enter value 1: 7
    Enter char value 2: Enter value 3: 5
    read values: 7
    5


    Thanks

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    Quote Originally Posted by DevoAjit View Post
    I don't think so that there is need of pointers... remove the pointers and try diffrent variables of 2 ints and 1 char in void main block..
    If I remove the pointers, it seems to hang after I input the value 1. Thanks

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    23
    Quote Originally Posted by wearld97 View Post
    Salem
    As soon as I replace the %s with a %c, the program does not accept a value for the second value. The output I get is:

    Enter value 1: 7
    Enter char value 2: Enter value 3: 5
    read values: 7
    5


    Thanks
    Shouldn't the second variable be a character instead of a number?

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    Quote Originally Posted by 7h3_0r4c1_3 View Post
    Shouldn't the second variable be a character instead of a number?
    Correct, but it doesn't wait to allow typing the 2nd value. Instead after displaying the line "Enter char value 2:"
    For some reason it it immediately displays the line "Enter value 3:" on the same line.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Did you specifically note the extra SPACE before the %c ?

    Yes, it really matters!
    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.

  13. #13
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    Salem,
    Do you mean to check on the space on line 25 before the %c? If yes, I did but it didn't help. Thanks.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post your latest code then please.
    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.

  15. #15
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    Quote Originally Posted by Salem View Post
    Post your latest code then please.
    Here it is...Thanks

    Code:
    #include <stdio.h>
    #include <string.h>  //not sure needed
    #include <stdlib.h>  // not sure needed
    
    void input(int* val1, char* val2, int* val3);
    
    void main()
    {
        int val1;       // value 1
        char val2;    // value 2
        int val3;          // value 3
    
        input(&val1, &val2, &val3);
    
        printf ("read values: %d %c %d", val1, val2, val3);
    
    }
    
    void input(int* val1, char* val2, int* val3)
    {
        //Input from the user
        printf ("Enter value 1: ");
        scanf ("%d", val1);
        printf ("Enter char value 2: ");
        scanf ("%c", val2);
        printf ("Enter value 3: ");
        scanf ("%d", val3);
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Push ints into a char vector
    By Prediluted in forum C++ Programming
    Replies: 10
    Last Post: 06-11-2011, 01:36 PM
  2. Dealing with ints that are in char array
    By Wiretron in forum C Programming
    Replies: 4
    Last Post: 05-15-2006, 04:06 PM
  3. reading 3 ints from one line, then 3 from another
    By Tokay in forum C++ Programming
    Replies: 10
    Last Post: 11-13-2005, 09:42 PM
  4. need help with char and ints in an array
    By satory in forum C Programming
    Replies: 1
    Last Post: 12-05-2004, 01:41 PM
  5. Reading an unknown # of ints into array
    By jds in forum C Programming
    Replies: 3
    Last Post: 10-10-2003, 04:16 PM