Input and output

This is a discussion on Input and output within the C Programming forums, part of the General Programming Boards category; (1) If I wanted the following piece of code to input an integer and a character and print them, while ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    18

    Input and output

    (1) If I wanted the following piece of code to input an integer and a character and print them, while assuming the user types as input 100 A, what corrections do I need:

    .
    .
    .
    scanf( "%d", &intVal );
    charVal = getchar();
    printf( "Integer: %d\nCharacter: %c\n", intVal, charVal);
    .
    .
    .

    (2) I want the piece of code to output even integers from 2 to 100.

    .
    .
    .
    counter = 2;

    Do {
    if (counter % 2 == 0)
    printf ( "%d\n", counter );

    counter += 2;

    } While ( counter < 100);

  2. #2
    Watch for flying houses. Nessarose's Avatar
    Join Date
    Sep 2004
    Posts
    46
    1. You could do this whole thing with scanf:
    Code:
    int num;
    char c;
    printf("Enter number followed by character: ");
    scanf("%d %c", &num, &c);
    printf("Int: %d Cha: %c\n", num, c);
    2. In your code, you have counter starting at 2, which makes it even. You don't have to make the check to see if (counter % 2 == 0) because you're incrementing it by 2 in the end. This means that the if statement will always be true since an even number + 2 will give you an even number. In any case, I don't see anything wrong with your code. It prints out even numbers fine on my end.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Bad output -- possible input buffer problems
    By jake123 in forum C Programming
    Replies: 8
    Last Post: 02-18-2008, 02:36 PM
  3. Basic C input output program help
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 01-27-2008, 05:41 PM
  4. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM

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