Originally Posted by
Teiji
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.