473,626 Members | 3,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Forcing the extraction operator to ignore certain input

1 New Member
I'm working on a piece of code that reads in a time from the user.

This time value consists of 3 separate inputs:

1 integer representing hours
1 char representing the ":"
1 integer representing minutes

for example:

cout << "Time: " <<;
cin >> x1 >> ch1 >> y1;

"the user would input 8:35 for example and it would be stored as
x1 = 8
ch1 = :
y1 = 35

Now, my problem is that I need to account for the fact that the user may input a bunch of junk AFTER I've got all the data I need.

For example, they might input:

"8:35 this is the time"

And I need it to ignore anything past 8:35, so essentially, anything past the y1 variable must be ignored.

I've been messing around with cin.ignore but haven't found a way to get it to ignore things after a certain series of inputs. Is it even possible to use cin.ignore in this situation?

I'm new to the C++ scene and need a simple, understandable solution.
Oct 26 '06 #1
1 1979
Banfa
9,065 Recognized Expert Moderator Expert
use getline to read the entire line of input into a string. Then from that string create a stringstream to read the bits you are interested in. You can just discard the rest, the input will be cleared because you have read it all.
Oct 27 '06 #2

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

Similar topics

3
3415
by: Kangol kangoll | last post by:
hi, i've been trying to figure out how to make visual basic start extracting text at a certain line to when i tell it to stop. I am making trying to make a program where it extracts text from a text file and inserts it into a database. But right now i'm stuck at finding a way to code it so the text extraction doesn't start till after the second set of dashes, here is part of text file. I want to extract ACL ET AL SNIPELK 8-27-70-18 and so...
7
3651
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc. And i have things to offer, and to request. And a lot of ideas, but who needs them.... here's an example (from type_struct.py):
2
1990
by: Pmb | last post by:
How are character strings input from the user. For example: Suppose I have the code ____________________________________ #include <iostream.h> int main() { char nameString; cout << "Enter your name: ";
1
1318
by: yeutim | last post by:
I can't find anything wrong this with this class. Especially, Overloading Stream-Extraction Operator (in blue). Any one know what do I need to fix this problem please reply. Thank you for your help! --------------------------------------------- class MyData{
47
3330
by: Roger Lakner | last post by:
I often see operator implemented something like this: class Foo { ... }; class FooList { public: const Foo& operator (unsigned index) const {return array;}; Foo& operator (unsigned index) {return
5
2897
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
16
4336
by: EM.Bateman | last post by:
Working on Visual Studio .Net I've implemented a class: #ifndef CONTRIBUTOR_H #define CONTRIBUTOR_H enum Gender {male=1, female, unk}; #include <iostream> #include <iomanip> #include <string>
5
2122
by: Taras_96 | last post by:
Hi all, Jesse Liberty writes: "cin.get() >>myVarOne >myVarTwo; // illegal The return value of (cin.get() >myVarOne) is an integer, not an iostream object." (http://newdata.box.sk/bx/c/htm/ch16.htm)
5
2113
by: newbarker | last post by:
Hello all, #include <sstream> #include <iostream> int main() { std::istringstream iss("6.5e"); double size; char fit;
0
8202
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8641
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5575
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2628
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.