I recently completed a small console app that accepts input from the user and places it in a floating point array, which was implemented through a for loop. I have not spent much time studying error handling and was wondering if anyone could refer me to a useful error-handling tutorial. I am mostly interested in allowing my program to not only accept erronous input but to seamlessly continue operating. Here is an example of my input function:

bool done = 0;
     int counter = 0;
     float nums[100], regulator;
     cout<<"Input a number (-1 exits): "<<endl;
     while(done == 0)
     {
           cin>>regulator;
           if(regulator != -1)
           {
                 nums[counter] = regulator;
                 counter++;
           }
           else
           {
                 done = 1;
           }
     }

I am mostly interested in recovering from characters being used instead of numbers. Also, is there a way for an int to store the ASCII value of a character in case one was input?
Thank you!

Recommended Answers

All 5 Replies

I recently completed a small console app that accepts input from the user and places it in a floating point array, which was implemented through a for loop. I have not spent much time studying error handling and was wondering if anyone could refer me to a useful error-handling tutorial. I am mostly interested in allowing my program to not only accept erronous input but to seamlessly continue operating. Here is an example of my input function:

bool done = 0;
     int counter = 0;
     float nums[100], regulator;
     cout<<"Input a number (-1 exits): "<<endl;
     while(done == 0)
     {
           cin>>regulator;
           if(regulator != -1)
           {
                 nums[counter] = regulator;
                 counter++;
           }
           else
           {
                 done = 1;
           }
     }

I am mostly interested in recovering from characters being used instead of numbers. Also, is there a way for an int to store the ASCII value of a character in case one was input?
Thank you!

First decide what "legal input" is. If you are talking about an integer, it would probably be a digit or a minus sign as the first digit. If it was a minus sign, it needs to be followed by one or more digits and nothing else, save possibly for a trailing decimal point. If it was a digiti, it needs to be followed by zero or more digits and possibly a trailing decimal point. So you read the input in as a string and pass that to a function that checks whether it's "legal" according to the previous standard for "legality". If it's illegal data, give an error message and prompt for input again. If it was legal, you can then covert that integer to a string using the atoi function and do whatever you're going to do with it. You may also want to check that the integer entered is in the legal range for an integer. atoi actually will check for legality for you, I believe, but I think it returns 0 if it's illegal, so you would have to make sure the number entered wasn't 0.

http://www.cplusplus.com/reference/clibrary/cstdlib/atoi.html

First decide what "legal input" is. If you are talking about an integer, it would probably be a digit or a minus sign as the first digit. If it was a minus sign, it needs to be followed by one or more digits and nothing else, save possibly for a trailing decimal point. If it was a digiti, it needs to be followed by zero or more digits and possibly a trailing decimal point. So you read the input in as a string and pass that to a function that checks whether it's "legal" according to the previous standard for "legality". If it's illegal data, give an error message and prompt for input again. If it was legal, you can then covert that integer to a string using the atoi function and do whatever you're going to do with it. You may also want to check that the integer entered is in the legal range for an integer. atoi actually will check for legality for you, I believe, but I think it returns 0 if it's illegal, so you would have to make sure the number entered wasn't 0.

http://www.cplusplus.com/reference/clibrary/cstdlib/atoi.html

surely you mean convert string to an integer using atoi()?

Well I was thinking more of a cin.clear() method or a way to use getch() to allow me to better accept accidental characters or symbols being input instead of a float or int. I just wondered if there is a better way than simply using cin and clearing the buffer if the input is invalid.

surely you mean convert string to an integer using atoi()?

Yes, that's what I meant. Good catch.

Well I was thinking more of a cin.clear() method or a way to use getch() to allow me to better accept accidental characters or symbols being input instead of a float or int. I just wondered if there is a better way than simply using cin and clearing the buffer if the input is invalid.

I'm not sure I'm visualizing your getch() solution correctly. Care to post it? Using cin with cin.clear() and clearing the buffer in the event of bad input could work:

#include <iostream>
using namespace std;

int main ()
{
    int a;
    bool goodinput = false;
    while (!goodinput)
    {
          cout << "Enter an integer: ";
          cin >> a;
          if (cin.fail())
          {
              cout << "Bad input.  Try again.\n";
              cin.clear ();
          }
          else
          {
              cout << "You entered an integer.\n";
              goodinput = true;
          }
              
          cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
    }
    
    return 0;
}

My idea involving the getch() function was as follows:

//main program here
char temp;
cout<<"Enter a number: ";
getch(temp);
if(temp == 'a')
{
      cout<<"Incorrect character input."
}
else
//program continues

This is a very simplified example but I would prefer to stick with the cin function if it has any more error handling abilities. I do not know if this is the correct way to use the getch() function though. I googled getch() and getch() refernce but the only examples that I saw were on using getch() to pause the program.
I'm only brainstorming at this point but it never hurts to learn :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.