Public Group

# C++ - cin and errorchecking

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

## 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

It repeats the error message once for each character in the input. Also:
Please enter the first number:  5 foo

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 on other sites
Quote:
 Original post by realcoreDoes anyone have some pointers

0x3A28213A
0x6339392C
0x7363682E

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 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 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 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 on other sites
Quote:
 Original post by realcoreHey 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?

1. 1
Rutin
69
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633430
• Total Posts
3011831
• ### Who's Online (See full list)

There are no registered users currently online

×