• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 555
  • 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
Increase Security & Decrease Risk with NSPM Tools

Analyst firm, Enterprise Management Associates (EMA) reveals significant benefits to enterprises when using Network Security Policy Management (NSPM) solutions, while organizations without, experienced issues including non standard security policies and failed cloud migrations

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get Cisco Certified in IT Security

There’s a high demand for IT security experts and network administrators who can safeguard the data that individuals, corporations, and governments rely on every day. Pursue your B.S. in Network Operations and Security and gain the credentials you need for this high-growth field.

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