Programming tutorials online .NET, Java, C++, Visual Basic, and More
 

Home
.NET Tutorials
General Programming Tutorials
Community/Forums
Site Extras

 

C/C++ Forum
 

Open forum for posting questions or comments regarding C/C++.


POST REPLY
Posted By: lngaurav   6/10/2006 6:11:00 PM
Reply To: Miroslav who posted...
 
Hi,
I've tested your code and found that there was no prblem with the logic however, once you have provided some input through the input unit(Keybord in our example) the stdin buffer which keeps track of inputed values is already got some value and sometimes automatically supplies values to the variable which is supposed to take value from this buffer through the input device.
to overcome this situation we have a function i.e
fflush(buffer) to clear memory buffers.
to clear the standerd input buffer we can call this function with the argument , " stdin".
like... fflush(stdin).
clrscr() is also used at the begining to clear any previous information from the console.
Here is the debugged code......

#include
#include
#include
struct books
{
char title [50];
char author [20];
float price;
};
main ()
{
struct books book1;
struct books book2;
clrscr();
printf("\nType the title of the first book: ");
gets(book1.title);
printf("\nType the author of the first book: ");
gets(book1.author);
printf("\nType price of the first book: ");
scanf("%f", &book1.price);
fflush(stdin);
printf("\nType the title of the second book: ");
gets(book2.title);
printf("\nType the author of the second book: ");
gets(book2.author);
printf("\nType price of the second book: ");
scanf("%f", &book2.price);
printf("\nThe title of the first book is: %s \n", book1.title);
printf("\nThe author of the first book is: %s \n", book1.author);
printf("\nThe price of the first book is: %1.2f \n", book1.price);
printf("\nThe title of the second book is: %s \n", book2.title);
printf("\nThe author of the second book is: %s \n", book2.author);
printf("\nThe price of the second book is: %1.2f \n", book2.price);
getche();
}


Regards
Naresh Gaurav Lavania
Agra
India





 
Sign In
Email
Password
  Forgot my password

Search

Featured Book

Introducing Microsoft .Net, Third Edition ($19.79)
Introducing Microsoft .Net, Third Edition


Featured Software

Microsoft Visual Studio Standard 2005 ($249.99)
Microsoft Visual Studio Standard 2005