Keep getting strange results with char and scanf
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: Keep getting strange results with char and scanf

  1. #1
    Join Date
    Nov 2013
    Posts
    10

    Keep getting strange results with char and scanf

    Code:
    #include <stdio.h>
    
    int main(){
    unsigned char x, y=10;
    
    scanf ("%c", &x);
    
    x = (x)*(y);
    
    printf("%i", x);
    
    if (x==100){
        printf ("correct");
    }
    
    return 0;
    
    }
    If I take out scanf and assign x a value then it works, but if I enter, for example, 10 into scanf it comes up as 234 rather than 100.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,276

    Re: Keep getting strange results with char and scanf

    It seems to me that you want x to be an int, not an unsigned char, upon which you should read in with say %d or %i instead of %c.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Nov 2013
    Posts
    10

    Re: Keep getting strange results with char and scanf

    I want it to be a char, the program will only ever hold numbers from 1-12 so I'm using char to save memory.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,276

    Re: Keep getting strange results with char and scanf

    Quote Originally Posted by cassas00
    I want it to be a char, the program will only ever hold numbers from 1-12 so I'm using char to save memory.
    Unfortunately, then you need to read in the input as a string which you parse into a char (or unsigned char). You cannot use scanf since scanf with %c would read in just a char, e.g., your input of "10" was read in as '1', which in ASCII is 49. This 49 was multiplied by 10 to get 490. Presumably 490 is larger than UCHAR_MAX, so the result was wrapped around to get 234.

    Frankly, it would be simpler to just use an int. Your concern for saving memory is misplaced here, especially since the char will be promoted to int for the calculation anyway.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Nov 2013
    Posts
    10

    Re: Keep getting strange results with char and scanf

    I see your point.

    How would I ensure scanf only accepts numbers?

    while ((answer >='a' && answer <='z')||(answer >='A' && answer <='Z'))
    {
    printf ("Error, invalid input, please re-enter answer ");
    scanf(" %i", &answer);
    }

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,276

    Re: Keep getting strange results with char and scanf

    Quote Originally Posted by cassas00
    How would I ensure scanf only accepts numbers?
    Approach #1: use scanf to read into an int, then check that scanf returns 1.
    Approach #2: read into a string then parse it into an int with error checking. Unfortunately, atoi does not do proper error checking, so one way is to use a long instead of an int with strtol.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Nov 2013
    Posts
    10

    Re: Keep getting strange results with char and scanf

    can you elaborate on approach #1 please? Having a little trouble implementing it.

  8. #8
    Join Date
    Nov 2013
    Posts
    10

    Re: Keep getting strange results with char and scanf

    I have this

    #include <stdio.h>

    int main()
    {
    int v;

    while (scanf("%d", &v) != 1) {
    printf("ERROR");
    v = getchar();
    scanf("%d", &v);
    }

    }

    It works but I need to type in integer twice to end the loop?

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,276

    Re: Keep getting strange results with char and scanf

    Quote Originally Posted by cassas00
    can you elaborate on approach #1 please? Having a little trouble implementing it.
    Hmm... okay. Here's an example derived from yours involving a controlled infinite loop:
    Code:
    #include <stdio.h>
    
    int main(void) {
        int x;
        for (;;) {
            printf("Enter an integer: ");
            if (scanf("%i", &x) == 1) {
                break;
            }
        }
    
        x *= 10;
    
        printf("%i\n", x);
        if (x == 100) {
            printf("correct\n");
        }
    
        return 0;
    }
    You probably want something slightly different to give an initial prompt for input and then an error message on invalid input, but I leave that as an exercise.

    Notice that I indent my code properly so that it is readable.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: Keep getting strange results with char and scanf

    Quote Originally Posted by cassas00 View Post
    can you elaborate on approach #1 please? Having a little trouble implementing it.
    Show your implementation and describe the "trouble"
    Victor Nijegorodov

  11. #11
    Join Date
    Nov 2013
    Posts
    10

    Re: Keep getting strange results with char and scanf

    Sorry, when I paste the code doesn't indent

    #include <stdio.h>

    int main()
    {
    int v;

    while (scanf("%d", &v) != 1) {
    printf("ERROR");
    v = getchar();
    scanf("%d", &v);
    }

    }

    This gives me what I'm looking for, an error if a number isn't entered, but when a number is entered it doesn't end the loop, only when I enter a second number does it end the loop and accept that number

    so it will do

    enter number f //enter f
    error f
    error f
    error f
    error 4 // enter 4, loop doesn't end
    5 // second number inputted ends loop and accept that number (if another letter is entered it carries on with Error)

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: Keep getting strange results with char and scanf

    Quote Originally Posted by cassas00 View Post
    Sorry, when I paste the code doesn't indent
    It is because you didn't use Code tags!
    Victor Nijegorodov

  13. #13
    Join Date
    Nov 2013
    Posts
    10

    Re: Keep getting strange results with char and scanf

    Code:
    #include <stdio.h>
    
    int main()
    {
       int v;
    
       while (scanf("%d", &v) != 1) {
           printf("ERROR");
           v = getchar();
           scanf("%d", &v);
       }
    
    }
    Option wasn't there on quick reply so I just typed it in, any insight why I have to enter a number twice to end the loop?

  14. #14
    Join Date
    Nov 2013
    Posts
    10

    Re: Keep getting strange results with char and scanf

    Ah got it now, was because I had more than one scanf so it repeated the question, thanks for your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center