Thread: For some reason, my program keeps a newline in an integer

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    1

    For some reason, my program keeps a newline in an integer

    This is my program.
    Code:
    #include <stdio.h>
    int main ()
    {
        int num1, num2;
        int num3;
        char letter;
        /* part a */
        scanf("%d", &num1);
        scanf("%d",&num2);
        printf("%d\n", num1/num2);
        printf("%d\n", num1 % num2);
        printf("%.2f\n", (float)num1/num2);
        /* part b */
        scanf("%c", &letter);
        printf("%d\n", letter);
        printf("%c\n", letter+32);
        /* part c */
        scanf("\n %d", &num3);
        printf("%d\n", num3);
    }
    It does some really crazy things. The output I get on part c is something like -854892179.
    Just input something like "3" "2" "a" and watch it all go to heck.

    and when I look at debugger windows I see "letter" contains a newline, but I can't figure out why. Would appreciate your help.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    http://stackoverflow.com/questions/5...char-in-buffer
    try putting a space before the %c when you scan into letter
    Ex.)
    Code:
    scanf(" %c", &letter);
    Last edited by camel-man; 11-03-2012 at 04:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on Classic big integer program
    By DaNxTh3xMaNx in forum C Programming
    Replies: 16
    Last Post: 09-14-2010, 10:11 PM
  2. Program crashing when replacing newline character
    By mdekom12 in forum C Programming
    Replies: 2
    Last Post: 05-01-2010, 08:49 PM
  3. Need to create an integer-to-word program
    By toadkiwi in forum C Programming
    Replies: 8
    Last Post: 02-22-2008, 10:00 AM
  4. Integer creation program
    By HLMetroid in forum C Programming
    Replies: 4
    Last Post: 09-22-2007, 12:57 AM
  5. smallest integer program, help please
    By STUDENT2 in forum C Programming
    Replies: 9
    Last Post: 09-06-2002, 03:48 PM