Not Throwing Error

So I am trying to get my program to throw an error if the user enters a letter or anything that isn't a number but whenever I run the program it jumps straight to the while loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cstdlib>
#include <ctype.h>

int main()
{	
	int x = 0;
	int a = 0;
	int b = 0;
	int area = 0;
	std::cout << "Please Enter A Value Between 1 And 4: ";
	try
	{
		std::cin >> x;
		if (isalpha(x))
		{
			throw "Error Input Must Be Between 1 And 4";
		}
	}
	catch (const char* msg)
	{
		std::cout << msg << std::endl;
	}
	if (x <= 0 || x > 4)
	{
		while (x <= 0 || x > 4)
		{
			std::cout << "Please Enter A Value Between 1 And 4: ";
			std::cin >> x;
		}
	}
return 0;
}
for throwing exceptions on reading failure, take a look at
http://www.cplusplus.com/reference/ios/ios/exceptions/


as to why your code doesn't work
it seems that you are angy because you didn't find an elephant on your icebox
an int (integer) holds one number, if you try to put anything that's not a number the operation will fail
Your input is expecting an int. When you enter a character std::cin enters an error state you never reset.

You are throwing an error, but it is being swallowed up by std::cin being in a continual error state.

Accept any input into a string, and throw on an attempt to convert the string to int.

If that fails, print out an error message and continue the loop.

If you have a valid int check if it is outside your minimum and maximum accepted numbers.

If that fails, print out an error message and continue the loop.

Received a number between the min and max, inclusive? SUCCESS!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>

int main()
{
   const int min     { 1 };
   const int max     { 4 };
   int response      { };
   std::string input { };

   while (response < min || response > max)
   {
      std::cout << "Please Enter A Value Between 1 And 4: ";

      std::getline(std::cin, input);
      std::cout << '\n';

      try
      {
         response = std::stoi(input);
      }

      catch (...)
      {
         std::cout << "** INVALID INPUT! **\n\n";
         continue;
      }

      if (response < min || response > max)
      {
         std::cout << "** INVALID CHOICE! **\n\n";
         continue;
      }
   }

   std::cout << "You entered " << response << '\n';
}

Please Enter A Value Between 1 And 4: -5

** INVALID CHOICE! **

Please Enter A Value Between 1 And 4: 5

** INVALID CHOICE! **

Please Enter A Value Between 1 And 4: a

** INVALID INPUT! **

Please Enter A Value Between 1 And 4: 3

You entered 3
Thank you for replying. Your code did exactly what I wanted however I am confused as to why you did not throw any error message. Also what does the (...) in catch(...) mean? Why did you use "continue;" ? Is it possible to use the throw syntax with this method you used?
Thanks in advance for any replies.
Last edited on
> I am confused as to why you did not throw any error message.
http://www.cplusplus.com/reference/string/stoi/
std::stoi() will throw an exception if it can't convert the string to a number

> what does the (...) in catch(...) mean?
catch any kind of exception
std::stoi() may throw `invalid_argument' or `out_of_range', both will be handled the same way

> Why did you use "continue;" ?
¿because the loop should keep going?
Leverage the power the Standard Library/Standard Template Library gives you to use.

ne555 has the answers to your questions. I'll just add that continue skips over code that doesn't need to execute, so the loop can run again from the start.

https://en.cppreference.com/w/cpp/language/continue

BTW, continue; at line 32 is not strictly needed, there is no code afterwards that need to be "skipped over."

I pulled the code from a larger project that required I bypass more code with continue.

I got lazy and didn't remove the continue.

It doesn't create a problem if not removed. It is just redundant.
Topic archived. No new replies allowed.