开发者

Problem with try/catch block - Integer between (0-9) console program

开发者 https://www.devze.com 2023-03-09 10:37 出处:网络
I am having a problem running the code I have put below. All I nee开发者_JAVA百科d to do is have a user input an integer then save it to my cNumber variable.

I am having a problem running the code I have put below.

All I nee开发者_JAVA百科d to do is have a user input an integer then save it to my cNumber variable.

then assign the value of subtracting the ASCII value of zero from cNumber to the variable iNumber and test it in a try/catch block and cout the results of multiplying the valid (0-9) integer by 2.

#include <iostream>
using namespace std;

// Declare variables 
char cNumber;
int iNumber;

int main () {
  // Begin infinite while loop
  while (1) {
    // Prompt user to enter aa number within th range of (0-9)
    cout << "Please enter an number between 0 and 9: ";

    //Get character from the keyboard and validate it as integer 
    //within the range (0-9)
    try {
      //Assign user input alue into cNumber variable
      cin >> cNumber;

      //Subtract ASCII value of zero from cNumber and assign to iNumber 
      iNumber = cNumber - 48;
      if (iNumber < 0) {
        throw string(cNumber + " is not within the range of (0-9)");
      }
      if (iNumber > 9) {
        throw string(cNumber + " is not within the range of (0-9)");
      }
    }
    catch (exception ex) {
      cerr << cNumber + " is not a valid selection.... Input Error" << endl;
    }
    cout << "The result is " + iNumber * 2;
  }
}


It's not clear what you're asking, but I'll take a potshot at a few of your problems.

The expression cNumber + " is not within the range of (0-9)" is addition between char and char const*, which is not valid. You're probably accidentally manipulating pointer addresses.

It is possible to concatenate a char to a string, but it has to be an actual std::string object.

So:

throw cNumber + string(" is not within the range of (0-9)");

Similarly, you're misusing + later in your code.

Write:

cerr << cNumber << " is not a valid selection.... Input Error" << endl;

You're also throwing std::string but catching std::exception. Strings do not derive from exceptions. Read the chapter in your C++ book about try/catch. (Throwing/catching strings is not recommended anyway, and neither is catching by value.)

Also, if the input is not numeric, the extraction into int will fail... but you have no error checking/resetting on the cin stream.

For each line of your code, take a look at each component of it and ask yourself, "what does this do? Why did I write this?" If, for any piece of code, you cannot answer and prove the answer to these two questions, stop and think about whether or not it's correct.


You are throwing a std::string, but your catch block is declared as having a std::exception parameter.

Don't know if this mismatch could be the root of your problem.

In any case, it is not advisable to throw std::string, since this class can throw exceptions,and if and exception is thrown while handling a previous exception, big trouble (sudden termination).


Seems overly complex to me why not just:

#include <iostream>
using namespace std;

int main ()
{
  // Begin infinite while loop
    while (1) 
    {
        // Prompt user to enter aa number within th range of (0-9)
        cout << "Please enter an number between 0 and 9: ";

        // Get character from the keyboard and validate it as integer within the range (0-9)
        // Assign user input value into cNumber variable
        char cNumber;
        cin >> cNumber;

        // Subtract ASCII value of zero from cNumber and assign value to iNumber variable
        int iNumber = cNumber - 48;

        if (iNumber < 0 || iNumber > 9)
        {
            cerr << cNumber << " is not within the range of (0-9)" << endl;
        }
        else
        {
            cout << "The result is " + iNumber * 2;
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消