Unexpected change of input values

This is a discussion on Unexpected change of input values within the C Programming forums, part of the General Programming Boards category; I tried a very simple code to add and subtract integer values using functions. The code is listed below. When ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    2

    Question Unexpected change of input values

    I tried a very simple code to add and subtract integer values using functions.
    The code is listed below. When the user inputs a value for the 2nd Integer (int b in this case) it is automatically changed to a zero after the character input.. why does this happen ? The same code perfectly works when integers are substituted by floats.

    Code:
    #include<stdio.h>
    #include <conio.h>
    
    int Add(int a,int b);
    int Sub(int a,int b);
    
    int main()
    {
    int a;
    int b;
    char typ;
    
    printf("\n Value A:");
    scanf("%d",&a);
    printf("\n Value B:");
    scanf("%d",&b);
    
    printf("\n Which? + or - : ");
    scanf("%s",&typ);
    switch(typ)
    {
    case '+':printf("\n The Anser is: %d\n",Add(a,b));
    break;
    case '-':printf("\n The Anser is: %d\n",Sub(a,b));
    break;
    }
    getch();
    return 0;
    }
    
    int Add(int a,int b)
    {
    int add;
    add=a+b;
    return add;
    }
    int Sub(int a,int b)
    {
    int sub;
    sub=a-b;
    return sub;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > scanf("&#37;s",&typ);
    Because your typ is only a single char, and any actual input you provide will take up at LEAST two characters, thus trashing someone elses memory. "%s" conversions append a \0 to everything they do.

    > The same code perfectly works when integers are substituted by floats.
    That just makes you lucky, not good.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >scanf("%s",&typ);
    Because %s is the wrong format specifier for a char. %s is for char arrays, and %c for chars.
    Code:
    scanf("%c",&typ);

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    scanf("%c",&typ);
    %s is for strings

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    And i would really suspect that code would work properly. Since u have used scanf, it would give you a lot of problems, either when you port this code to a different machine.

    You will have to fflush the input buffer before reading the char value that is + or -.

    ssharish2005

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    2
    printf("\n Which? + or - : "); // when the character input is shifted to the top it works fine..
    scanf("&#37;s",&typ); //plus %c did not work.. compiler used is bcc32

    printf("\n Value A:");
    scanf("%d",&a);
    printf("\n Value B:");
    scanf("%d",&b);

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    call this function before the you read the + or -

    Code:
    void flush_buffer(void)
    {
         int ch;
         
         while((ch=getchar()) != '\n' && ch != EOF);
    }
    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 04-12-2009, 05:49 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. print input parameter values
    By George2 in forum Windows Programming
    Replies: 0
    Last Post: 06-30-2008, 03:33 AM
  4. unexpected result, no display of input string
    By xephyr in forum C Programming
    Replies: 11
    Last Post: 08-04-2004, 07:22 PM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 05:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21