473,938 Members | 8,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Basic cin - testing for extra unwanted input

hokasu
1 New Member
In the following example, I want the user to input something like:
1 + 2 = 3, then (not included) test its validity.

string op, eq;
int num1, num2, user_result;
cout<<"Please key in your equation.";
cin >> num1>>op>>num2> >eq>>user_resul t;

However, I need to "invalid input" this kind of input:
1 + 2 = 3 + 0

I can't think how to test for the extra bit. I tried an additional variable then checking if it was nul, but obviously, the program waits for the extra input.
Is there a way to check if there is extra input? I thought of getline(cin, s) or something, but thats for strings.
Oct 5 '08 #1
1 1854
weaknessforcats
9,208 Recognized Expert Moderator Expert
When you use cin >>, you are using formatted input. That is, you know ahead of time what type of data is coming in. That means 1 + 2 = 3 + 0 won't happen.

If you don't know that, then don't use the >> operator. Instead, use cin.get and parse the input a character at a time.
Oct 6 '08 #2

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

Similar topics

0
1502
by: scooterm | last post by:
### Question Can anyone name a text editor or XML editor (prefer open source) that can open a simple table XML file in a spreadsheet-type GUI, without adding any extra formatting or junk to the XML file? For example, given the following very simple XML file: <table> <row> <fname>fred</fname>
1
1748
by: Stephen | last post by:
I have a really annoying problem with a datagrid. I have an application which populates a datagrid on the onclick event of a button. The datagrid is bound to an ArrayList which holds the values. Everything worked perfectly for me in my test application however when I copied and pasted the code accross to my main project something strange started to happen. On the on-click event of the button (cmdExcAdrContinue) the datagrid is populated...
10
1567
by: Andre | last post by:
I know I am a newbie and this will sound very elementary. Still I need help, and don't know where to find it. OK, here it is: ----
72
5316
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: http://geosoft.no/development/unittesting.html Thanks.
2
2469
by: ricky | last post by:
Can anybody help with the function to get rid of extra characters in the file. I want to remove the string from the file.So i read from input file and pass the string say "john" if found dnt write it to the ouput file but if not found write all the line to the output file so i read line by line cin.get(input,line) if(line != s) output<<line
3
6693
by: marknoten | last post by:
I'm quite a newbie to Visual Basic and have a problem with the Input # statement in the Visual Basic Editor from Excel. I would like to read in a tab delimeted text file selected by a user with records like this: <snip> O 21.266 17.733 28.155 N 20.649 17.703 30.320 C 20.883 17.336 31.723 </snip> I'm using the code below to read each line from the file and store them in the variables organicAtom, xCoord, yCoord and zCoord. The problem...
29
3243
by: Marco | last post by:
Hello, I have : float f = 36.09999999; When I do : char cf; sprintf(cf,"%0.03lf", f); I get : 36.100
28
3634
by: Randy Reimers | last post by:
(Hope I'm posting this correctly, otherwise - sorry!, don't know what else to do) I wrote a set of programs "many" years ago, running in a type of basic, called "Thoroughbred Basic", a type of business basic. I need to re-write it, bring it kicking and screaming to run on Windows XP. This is for a video rental place, tracks movie and game rentals, customers, employee transactions, reservations, does reports,..... and on. I know some of...
24
2549
by: David | last post by:
Hi list. What strategies do you use to ensure correctness of new code? Specifically, if you've just written 100 new lines of Python code, then: 1) How do you test the new code? 2) How do you ensure that the code will work correctly in the future? Short version:
0
10126
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11515
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
11099
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...
1
11282
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10652
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...
0
6072
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6287
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4442
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3495
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.