December 30th, 2013, 07:19 AM
Need a solution! STRUCTURE!!(i am learner)
Hello friends. I am following a book. and this is a sample program in that book for STRUCTURE.
but when i run the program
1. it should ask 4 ques but after entering 3rd answer it start the new iteration and jump to the 2nd question....please help me.
struct bookInfo books;
for (ctr = 0; ctr < 3; ctr++)
printf("What is the name of the books #%d?\n", (ctr+1));
puts("Whos the author? ");
puts("How much did the book cost? ");
scanf(" $%.2f", &books[ctr].price);
puts("How many pages in the books? ");
scanf(" %d", &books[ctr].pages);
printf("\n\nHere is the collection of books: \n");
for (ctr = 0; ctr <3; ctr++)
printf("#%d: %s by %s", (ctr+1), books[ctr].title, books[ctr].author);
printf("\nIt is %d pages and costs $%.2f", books[ctr].pages, books[ctr].price);
December 30th, 2013, 08:41 AM
Get rid of the loop,
Originally Posted by arifuddin
it is not needed here.
December 30th, 2013, 11:38 AM
What did the warnings tell you? And why are you ignoring warnings?
To begin with, you need to actually declare main to return int; you must not do it implicitly.
Here is Line 25:
The compiler is telling you that it doesn't know anything about periods within a conversion format string. When you read the documentation on scanf (eg, at http://linux.die.net/man/3/sscanf, obtained by Google'ing on man page scanf), did it say anything about periods in a format string? %, yes. Maximum field width, yes. Type modifier character (in this case, the f for float), yes. Periods? Not a single word (I even did a find on that page).
scanf(" $%.2f", &books[ctr].price);
The next warning, "warning: too many arguments for format", is because the format string was invalid, so no conversion could be performed, hence the address you provided scanf (ie, &books[ctr].price) had no format and hence no conversion was even attempted.
The rest falls right into place. I will assume that you entered something like 12.34. The next scanf converted the 12 and stopped at the period. The getchar swallowed the period, and the next input operation, the gets for the title, was satisfied by the 34.
Lose that period. You might also not want to specify the maximum length of the price field; no damage done if you make it large enough for any price, but then what benefit would you get by doing that? After all, you're leaving yourself wide open for buffer overflow run-time headaches by using gets, which is deprecated and whose use is advised against -- read the documentation on gets.
Also, you are requiring the user to enter a dollar sign every time he enters the price. If he neglects to, then that conversion fails and the price amount gets accepted by the next scanf as the number of pages.
RTFM! ("Read The Manual!") scanf has a return value. Read what that return value is and realize how you can use it to tell you whether it had succeed or failed in performing all the conversions you told it to perform. A very experienced forum member would have told you that with just a few keystrokes he could send your program running out into the weeds to vomit all over its own shoes. Your program is very brittle and will break very easily.
Also, never ignore warnings! If you had heeded the warnings your compiler was throwing at you, you would have been able to solve your problem on your own four and a half hours ago. Instead, you waited all that time for somebody else to show you those warnings again. Programmers cannot afford to waste that kind of time.
Warnings are more important than error messages. Always turn warnings on and up! And never ignore warnings!
daveyerwin is wrong about that loop. But maybe he can try to explain and justify his reply.
Last edited by dwise1_aol; December 30th, 2013 at 12:00 PM.