-3

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?

Ken White
  • 123,280
  • 14
  • 225
  • 444
FalloutRanger
  • 127
  • 2
  • 8
  • What's wrong with `scanf("%d %d %c %c %f %f %f", ...);`? Your input would then be `10 20 A a 1.5 3.75 67.8`. – Ken White Dec 01 '16 at 23:28
  • Well, it's a question at the end of a Chapter in a C textbook. The purpose of the question is to test the students' familiarity with C scanf specs. That's why it's deliberately made so complicated. Of course I know the best way to do it is just your suggestion, but that's not the point. The point is, how do you solve this problem designed by this retard? – FalloutRanger Dec 01 '16 at 23:32
  • If the question is designed to test the student's familiarity, why are you asking us to solve the problem? We're not the student. If the book hasn't provided you with the necessary information, contact the author or buy a different book. – Ken White Dec 01 '16 at 23:34
  • Brilliant! Thank you so much! Really appreciate your help and your attitude. Now, leave me alone. – FalloutRanger Dec 01 '16 at 23:35
  • No need for that, @KenWhite is trying to give good advice. If you work at it you will come out a better coder. – Fantastic Mr Fox Dec 01 '16 at 23:37
  • 2
    It's a very poor idea to get rude with people you're asking for free help to solve your problem. You may want to keep that in mind. Also keep in mind that when you post here, you do not get to pick and choose who responds or comments on your post. Unless your book is on the subject of *How to learn to copy/paste the work of others instead of doing it yourself*, you should learn to solve problems on your own. – Ken White Dec 01 '16 at 23:41
  • 2
    @FalloutRanger: The input you posted works perfectly fine with the `scanf` format you posted. No need to change anything. As for the "unhandled exception" error, it has nothing to do with what you posted so far. There's no way to say what's going on in your code and why it "didn't work" until you show the rest of it. – AnT stands with Russia Dec 01 '16 at 23:50
  • @AnT is correct: http://ideone.com/ZWEOAy Please un-accept my answer so i can delete it. – Fantastic Mr Fox Dec 02 '16 at 00:24
  • @AnT Thank you I will check it out tomorrow, since I don't have access to Visual Studio now. – FalloutRanger Dec 02 '16 at 00:25
  • @Ben, no need for that, thank you for your help. Appreciate it! – FalloutRanger Dec 02 '16 at 00:26
  • @FalloutRanger No, please do, the answer is irrelevant and should be removed. – Fantastic Mr Fox Dec 02 '16 at 00:27
  • @FalloutRanger: My educated guess would be that you copy-pasted the `scanf` format string to `printf` and transferred the `*` character as well. `*` in `printf` formats means something very very different from `scanf` formats. It could easily make your `printf` to crash. – AnT stands with Russia Dec 02 '16 at 00:35
  • @Ben As you wish :) – FalloutRanger Dec 02 '16 at 00:58

1 Answers1

3

The input you posted works perfectly fine with the scanf format you posted. No need to change anything. As for the "unhandled exception" error, it has nothing to do with what you posted so far. There's no way to say what's going on in your code and why it "didn't work" until you show the rest of it.

My "educated guess" would be that you just copy-pasted the scanf format string to printf, thus thoughtlessly transferring the %*f format specifier to printf as well. However, * in printf format string means something completely different from scanf format string. It will typically make your printf to crash, unless you make some very deliberate changes in the variadic part of printf argument list.

P.S. BTW, there's no need to pad the first two integeres with leading zeros. That 5 in the format string simply limits the maximum number of characters to read, it does not impose any minimum. You can just use "10 20Aa 1.5 3.75 1.5, 67.8".

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765