Solved

Problem with scanf in a structure program

Posted on 2001-08-13
16
530 Views
Last Modified: 2010-05-18
/*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
Comment
Question by:abhaykatekar
  • 8
  • 4
  • 2
  • +2
16 Comments
 
LVL 6

Accepted Solution

by:
Triskelion earned 75 total points
ID: 6380819
You have name set to be only one character.
Is this right?
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6380824
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
 
LVL 6

Expert Comment

by:Triskelion
ID: 6381266
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 1

Expert Comment

by:Aggarwal
ID: 6382393
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
 
LVL 6

Expert Comment

by:Triskelion
ID: 6382449
Aggrawal, the "%c" in your scanf(s) should be "%s".
0
 
LVL 1

Expert Comment

by:Aggarwal
ID: 6382485
Yup ..Absolutely  !!!

0
 
LVL 2

Expert Comment

by:sateesh_babu
ID: 6383108
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
 

Expert Comment

by:prabhuram
ID: 6392303
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
 
LVL 6

Expert Comment

by:Triskelion
ID: 6392429
prabhuram, I get the impression from this that "name" is a string and not just a character.  What do you think?
0
 

Expert Comment

by:prabhuram
ID: 6395767
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
 
LVL 6

Expert Comment

by:Triskelion
ID: 6396833
What is the input you give the program when it runs?
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6396840
...and better...
What is the input 'abhaykatekar' gives the program?
What happens if it's more than one character before the first space?
0
 

Author Comment

by:abhaykatekar
ID: 6400923
Hi Triskelion,
This was my first question.
And thanks for the cool answer.
Wish u all the best.
Abhay.
0
 

Expert Comment

by:prabhuram
ID: 6409263
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
 
LVL 6

Expert Comment

by:Triskelion
ID: 6409471
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
 

Expert Comment

by:prabhuram
ID: 6417128
sorry!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question