FalloutRanger FalloutRanger - 9 days ago 4
C Question

Input format using scanf

I can't solve this problem found in a C textbook:

Input data using the following scanf function, so that a=10, b=20, c1='A', c2='a', x=1.5, y=-3.75, z= 67.8. What do you type on the keyboard?

scanf("%5d%5d%c%c%f%f%*f,%f",&a,&b,&c1,&c2,&x,&y,&z);


Basically it's a question about the input format in C. I tried typing the following, but it didn't work:

00010凵00020Aa凵1.5凵-3.75凵1.5,凵67.8


where 凵 represent a space. When I use printf to output the variables, it gives the following error message:

Unhandled exception at 0x000007FEEC8243CE (msvcr120d.dll) in Project1.exe: 0xC0000005: Access violation writing location 0x0000000ACCCCCCCC.

Can anyone help me, please?

Ben Ben
Answer

Your input doesn't need spaces, and needs a little adjustment:

Lets go through it:

%5d 00010  
%5d 00020
%c  A
%c  a
%f  1.5
%f  -3.75 // you got all of these
%*f -1.5   // Here *f means that the input will be ignored, 
          // but you still need a way to separate the number 
          // before and this one. A + or - will suffice.
,%f ,67.8 // Easy.

Then put it all together, no space:

0001000020Aa1.5-3.75-10,67.8

Here is a live example.