Sign in to follow this  

C++ - cin and errorchecking

This topic is 3587 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys. I recently picked up on c++ again, and found a few exercises here on the forums. I only got to the third before hitting a brick wall, and after a day of trial and error, I decided to bite the bullet and ask. The code in itself is simple, as is the problem to solve, but the errorschecking is eating at me. Anyways, here's my code:
#include <string>
#include <iostream>

int main() {

	/* 
	 * Prompt for two numbers and report the product.
	 * Add in all the appropriate error handling: for each number, repeatedly ask until you
	 * actually get a valid number.
	 * Read the standard input a line at a time for this, and ignore any "garbage" text that
	 * appears after a valid number on the line (but do complain about garbage at the beginning).
	 */
	 
	double x = 0, y = 0;
	
	std::cout << "Please enter the first number: ";
	while( !(std::cin >> x) ) {
		std::cout << "Bad input, try again: ";
		
		std::cin.clear();
		std::cin.ignore();
	}
		
	std::cout << "Please enter the second number: ";
	while( !(std::cin >> y) ) {
		std::cout << "Bad input, try again: ";
		
		std::cin.clear();
		std::cin.ignore();
	}
	
	std::cout << "The product of " << x << " and " << y << " is: " << x*y << std::endl;
	
	return 0;
}

As you can see, its not the hardest problem in the world, however if I type in bad input it goes crazy. This is the best solution I have come up with yet, but lets try some input to it:
Please enter the first number: foo
Bad input, try again: Bad input, try again: Bad input, try again:
It repeats the error message once for each character in the input. Also:
Please enter the first number:  5 foo
Please enter the second number: Bad input, try again: Bad input, try again: Bad input, try again:
I also tried std::cin.ignore( std::cin.gcount() ), however it seems gcount returns 0, and the program looped infinitely. So thats basicaly my problem, printing the errormessage only once. Does anyone have some pointers or ideas?

Share this post


Link to post
Share on other sites
Quote:
Original post by realcore
Does anyone have some pointers

0x3A28213A
0x6339392C
0x7363682E

(sorry, really bad joke [grin])

As for ideas:
- gcount() only counts characters extracted by unformatted input operations
- ignore() by default only ignores 1 character

One solution would just be to pass a large number into the ignore() function. To do this pass in a very large number or, alternatively (and preferably), input the largest number you can. You can get this number here using std::numberic_limits.
std::cin.ignore(std::numeric_limits<std::streamsize>::max());

Share this post


Link to post
Share on other sites
I don't think anyone ever said weeding out bad input was easy in C++.
This is one of the reasons it's such a bad beginners language.
Anyways try this:
[source lang = "cpp"]
#include <iostream>
#include <string>
using namespace std;

double getdouble();

int main()
{
double x, y;
cout << "type in a number: ";
x = getdouble();
cout << "type in a another number:";
y = getdouble();
cout << "the product of " << x << " and " << y << " is: " << x*y << endl;
cout << "press return";
cin.get();
}

double getdouble()
{
for(;;)
{
double value;
cin >> value;
if(cin.fail()) // cin failed?
{
cin.clear(); // reset cin
string garbage;
getline(cin, garbage); // ignore rest of line
cout << "\nThat wasn't a number, try again.\n";
}
else
{
return value;
}
}
}




Share this post


Link to post
Share on other sites
Here, try this:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
while(cin){
system("CLS");
cout << "Enter 2 numbers to multiply:" << endl;
string sInput;
getline(cin, sInput);
stringstream ss(sInput);
float fFirst, fSecond;
if (ss >> fFirst >> fSecond)
cout << fFirst << " * " << fSecond << " = " << fFirst * fSecond << endl;
else
cout << "Incorrect input!" << endl;
system("PAUSE");
}
}

Share this post


Link to post
Share on other sites
Hey guys.

I can't believe just getting a couple of numbers is this much work, but I finally got it working. Now I can get on with the more interesting exercises.

Thanks to all of you for the help, I really appreciate it.

Share this post


Link to post
Share on other sites
Quote:
Original post by realcore
Hey guys.

I can't believe just getting a couple of numbers is this much work, but I finally got it working. Now I can get on with the more interesting exercises.

Thanks to all of you for the help, I really appreciate it.

Yeah that's why most introductory language books dispense with the error checking and have disclaimers about using the code in production.
I suggest when you are just starting not to worry too much about it otherwise it'll take you forever to get anywhere.

p.s. If you don't like that suggestion there are other languages that handle much more of the error checking for you than a language like C++ so you might want to try one of those?

Share this post


Link to post
Share on other sites

This topic is 3587 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this