Help with printf and scanf

This is a discussion on Help with printf and scanf within the C Programming forums, part of the General Programming Boards category; I want to write a program that accepts input like this: Input: 0001111+0110000 where the first 7 characters are digits ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    12

    Help with printf and scanf

    I want to write a program that accepts input like this:

    Input: 0001111+0110000

    where the first 7 characters are digits (I'm try to make them look like a 7 bit binary number), the 8th character is a math symbol, and the remaining 7 characters are also digits.

    How would the printf and scanf be written correctly?

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    why dont u make it a string u can also input or u wanna + them l8er on ? you cant scan a char and a int in same time must be done separately.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Teiji View Post
    I want to write a program that accepts input like this:

    Input: 0001111+0110000

    where the first 7 characters are digits (I'm try to make them look like a 7 bit binary number), the 8th character is a math symbol, and the remaining 7 characters are also digits.

    How would the printf and scanf be written correctly?
    1) You could treat each digit as an integer (or boolean) value:
    Code:
    printf("%d%d%d%d%d%d%d", bit1,bit2,bit3,bit4,bit5,bit6,bit7);
    scanf(" %d", &bit1); //etc.
    2) You could declare them as char's, which really are small int's "under the hood").
    If 'A' == 48, guess what 'A' + 'A' is? Hint: it's not AA!
    Guess what you get when you print out a char with integer format, as in #1?

    3) you could create your own data type.

    Since it looks like you want to do some math on them, I'd be tempted to suggest #1, for you. Maybe put in into an integer array.

    It all depends on what you want to do with the variables, and how comfortable you are (or if you want to stretch your C muscles), with those operations, on that data type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 08:35 PM
  4. Please help debug
    By Wexy in forum C Programming
    Replies: 2
    Last Post: 11-11-2002, 12:40 AM
  5. help with switch statements
    By Wexy in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 05:44 PM

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