Solved

Problem with scanf in a structure program

Posted on 2001-08-13
16
524 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
Comment Utility
You have name set to be only one character.
Is this right?
0
 
LVL 6

Expert Comment

by:Triskelion
Comment Utility
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
Comment Utility
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
 
LVL 1

Expert Comment

by:Aggarwal
Comment Utility
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
Comment Utility
Aggrawal, the "%c" in your scanf(s) should be "%s".
0
 
LVL 1

Expert Comment

by:Aggarwal
Comment Utility
Yup ..Absolutely  !!!

0
 
LVL 2

Expert Comment

by:sateesh_babu
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 6

Expert Comment

by:Triskelion
Comment Utility
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
Comment Utility
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
Comment Utility
What is the input you give the program when it runs?
0
 
LVL 6

Expert Comment

by:Triskelion
Comment Utility
...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
Comment Utility
Hi Triskelion,
This was my first question.
And thanks for the cool answer.
Wish u all the best.
Abhay.
0
 

Expert Comment

by:prabhuram
Comment Utility
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
Comment Utility
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
Comment Utility
sorry!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now