• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 547
  • Last Modified:

Problem with scanf in a structure program

/*When I run this program the prog stops taking input after the..
..second scanf statement.???Why.
 Also the in the output of the printf statement the the 2nd and 3rd..
 outputs are really wierd.Why?????????
 What is the reason for both these abnormal outcomes????*/

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
 struct book
 {
     char name;
     float price;
     int pages;
 };
   struct book b1,b2,b3;

   printf("\nEnter names,prices and no. of pages of 3 books.\n");
   scanf("%c %f %d",&b1.name,&b1.price,&b1.pages);
   scanf("%c %f %d",&b2.name,&b2.price,&b2.pages);
   scanf("%c %f %d",&b3.name,&b3.price,&b3.pages);
   printf("\n%f %d %f",b1.price,b2.pages,b2.price);
}
0
abhaykatekar
Asked:
abhaykatekar
  • 8
  • 4
  • 2
  • +2
1 Solution
 
TriskelionCommented:
You have name set to be only one character.
Is this right?
0
 
TriskelionCommented:
You should make the name an aray of characters

...
char name[50];
...

and change the scanf to receive a string "%s" not just one character "%c"
0
 
TriskelionCommented:
The weird printout is probably due to the memory overwrite that would happen if you entered more than one character for the name.
Also, with the simple "%f" formatting, you'll get a number that would print 123 as 123.000000.
To make it pretty, change the "%f" to "%1.2f".
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AggarwalCommented:
if u change the name ..dun forget to change scanf () to

scanf("%c %f %d",b1.name,&b1.price,&b1.pages);
scanf("%c %f %d",b2.name,&b2.price,&b2.pages);
scanf("%c %f %d",b3.name,&b3.price,&b3.pages);
 
Second thing while entering the data u wont be able to do something line enter the name then press enter then enter rpice and thne press enter and ..input on the command line should look something like ..


name 123.03 12 ( then press enter )..

This will work !!

Cheers
0
 
TriskelionCommented:
Aggrawal, the "%c" in your scanf(s) should be "%s".
0
 
AggarwalCommented:
Yup ..Absolutely  !!!

0
 
sateesh_babuCommented:
Another thing you can make a try is to flush(stdin) and to flush off the buffer. Most of the times, this is the cause of the problem.

Kippis!!!
Babu
0
 
prabhuramCommented:
whenever you enter an 'ENTER' key it'll be taken by the character of the next structure. so that the scanf gets finished by the second one itself.
you must have to clean the scanf buffer before you try to get the second character and so on.

use flushall() bw first scanf and second scanf and
bw second and third scanf()..
i think you 'll get the ans by now.
prabhuram
prabhu_ram_99@yahoo.com
0
 
TriskelionCommented:
prabhuram, I get the impression from this that "name" is a string and not just a character.  What do you think?
0
 
prabhuramCommented:
Triskelion,:
 it need not be a string, can be a character since the function flushall flushes the entire scanf buffer, the character can be scanned properly.
prabhuram
ps: here the flushall function is mainly applied to clear all unneccassary carriage returns and line feeds after you press each 'ENTER'.
0
 
TriskelionCommented:
What is the input you give the program when it runs?
0
 
TriskelionCommented:
...and better...
What is the input 'abhaykatekar' gives the program?
What happens if it's more than one character before the first space?
0
 
abhaykatekarAuthor Commented:
Hi Triskelion,
This was my first question.
And thanks for the cool answer.
Wish u all the best.
Abhay.
0
 
prabhuramCommented:
sorry yaars,
i cannot go with abhay, that he's accepted the ans of tri..
the pbm here is that obvious, the compiler tries to overwrite the memory it all known to everybody, but the solution is what i think need to be evaluated...
0
 
TriskelionCommented:
prabhuram, what answer (in code) would you give?
Did you test my solution?
If not, will you?
What about my solution needs to be evaluated?
Please be specific.
0
 
prabhuramCommented:
sorry!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now