#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Red face C programing help


    help hello
    im moni,
    im a real beginner in c programing,
    and im trying to sum two numbers and im doin it with switch
    although i dont know how to use it.
    i know i can do it some other way,
    and when im runing this prog it showing some wrong num,
    and i cant give data to scanf("%d",&b);
    but just wandering so plzzz help
    thanks in advance



    #include<stdio.h>
    #include<conio.h>
    main()
    {
    int a,b,sum;
    char ch;
    clrscr();
    printf("\t\t\t this is calculator program \n\n\n\n");


    printf("enter 1st num: \n");
    scanf("%d",&a);

    printf("enter any operator: \n");
    scanf("%c \n\n",&ch);

    printf("enter 2nd num: \n");
    scanf("%d",&b);

    switch(ch)
    {
    case '+':
    sum=a+b;
    break;
    default:
    } ;

    printf("\n\n\n answer= %d", sum);
    getch();
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,254
    Rep Power
    2222
    scanf("%c \n\n",&ch);
    says to read the very next character in the input buffer, which would be the newline from the Enter key when you entered the first number. If you rewrote the default case to output the ASCII code of ch (ie, printf with "%d") then you would see what ch's value is.

    You want that scanf to ignore all white space that precedes the character you want it to read, so you need to tell it that thus (note the space before the %):
    scanf(" %c \n\n",&ch);
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Smile


    Originally Posted by dwise1_aol
    scanf("%c \n\n",&ch);
    says to read the very next character in the input buffer, which would be the newline from the Enter key when you entered the first number. If you rewrote the default case to output the ASCII code of ch (ie, printf with "%d") then you would see what ch's value is.

    You want that scanf to ignore all white space that precedes the character you want it to read, so you need to tell it that thus (note the space before the %):
    scanf(" %c \n\n",&ch);
    wow nice i gave space before %c and it working...
    thanksuuu very much..
    can u explain why it happend?
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,254
    Rep Power
    2222
    Originally Posted by mo6361
    can u explain why it happend?
    I already did explain why it happened.

    Review scanf format strings. Any literal characters are what scanf is supposed to expect to see and to skip.

    Review the concept of white space.

    Review how scanf works.

    Read the "commonly asked questions" thread, where this subject is undoubtedly covered. Your mistake shows up here with extreme regularity.

    If you then still do not understand something, come back and ask that specific question.
    Last edited by dwise1_aol; January 14th, 2013 at 02:34 PM.

IMN logo majestic logo threadwatch logo seochat tools logo