473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

help on char data type

40
my code:
char name;
float price, no_of_pieces;
cout<<"Enter your name: ";
cin>>name;
cout<<"Enter price: ";
cin>>price;
cout<<"Enter no of pieces: ";
cin>>no_of_pieces;

output:

Enter your name: John
Enter price:
Enter no of pieces:

Question:

Why is it that it bypass the enter price and enter no of pieces? when I enter JOHN and then press enter, it bypass the enter price and no of pieces. Meaning I was not ask to enter the price and no of pieces? why is it like that?
Jul 16 '07 #1
2 1281
gpraghuram
1,275 Expert 1GB
my code:
char name;
float price, no_of_pieces;
cout<<"Enter your name: ";
cin>>name;
cout<<"Enter price: ";
cin>>price;
cout<<"Enter no of pieces: ";
cin>>no_of_pieces;

output:

Enter your name: John
Enter price:
Enter no of pieces:

Question:

Why is it that it bypass the enter price and enter no of pieces? when I enter JOHN and then press enter, it bypass the enter price and no of pieces. Meaning I was not ask to enter the price and no of pieces? why is it like that?
Use this call to flush the stdin.
fflush(stdin); //this is c variant.
Sometimes some characters may be left in the standard input which will be picked up by the cin.

Raghuram
Jul 16 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
What happens here is you enter JOHN. These characters are now in the input buffer.

Now you do

cin >> name;

name is a char so one character is extracted. name is now J and OHN are still in the input buffer.

Now you do

cin>> price;

where price is a float. cin>> sees the O and declares that O is not a floating point value so it sets a error flag and returns having done nothing. OHN are still in the input buffer.

Now you do:

cin>>no_of_pieces;

Here cin sees the error flag is set, so it does nothing since there has been an error. It returns having done nothing.

You have to remember that the >> operator means formatted input. That is, you know ahead of time what will be input. Any deviation, the error flag is set and all subsequent cin statements cease to work.

You can get around this problem ny declaring name an array:

char name[5];

Then you can cin >> name using JOHN.

The array is just large enough for JOHN and a null terminator a and the name must be one word. If it has spaces, like JOHN DOE, then cin >> won't work even if the array is made larger becuse it will stop on the space.

When you are first starting out, enter exactly what you expect. Leave input editing for another day.
Jul 16 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: AKF | last post by:
I am trying to use data structures to open a file (void display_Stock_Item(Stock_Item si);) from a text file. I also need to be able to write to the file and also to delete from the file using...
3
by: Mahmood Ahmad | last post by:
Hello, I have written a program that reads three types of records, validates them acording to certain requirements and writes the valid records into a binary file. The invalid records are...
2
by: Gandu | last post by:
Could some C++ guru shed some light as to what might be going on? I have the following two classes which are simply data containers: const unsigned int MAXSIZE = 40; class DataItem{ char...
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
2
by: James Radke | last post by:
Hello, I have a vb.net windows application that is calling an older DLL provided by a third party. They supplied a VB 6 application that, when run on my systemn successfully passes data to the...
9
by: Jordan Tiona | last post by:
I can't get this code to work right. It seems to be skipping some of the cin functions. Can someone help me with this? ClassTrack.cpp: #include <iostream> #include "ClassTrack.h" using...
1
by: robinsand | last post by:
I am a new C++ programmer. I am still having trouble with certain data types and constructors, among other things. I'm not sure if I've used "std::string" properly throughout this program. I need...
5
by: chapeau_melon | last post by:
Hello, I'm basicly not a programmer... I found some C++ codes on the net that almost satisfy me needs, wich is to communicate with an other device that sends data to me, wich I have to receive...
0
by: SOI_0152 | last post by:
Hi all! Happy New Year 2008. Il hope it will bring you love and happyness I'm new on this forum. I wrote a stored procedure on mainframe using DB2 7.1.1 and IBM language c. Everything works...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.