Hi I can't seem to get this right I've looked online but I can't find the right page.

I'm trying to make a simple program that takes a number but what if a character is entered. Well so far I get "140" as an answer.


Here's what I mean in pseudo code:

int main()
{
....
try
{
get a number
if (the number is a character) //don't quit get that
{
throw //something?
}
catch(some variable)
{
err<<some varible<<endl;
}
//end

So as you can see I'm puzzeled about this try and catch command. If someone could take the time to explain the concept to me that would be greatly appreciated!!!
thanks...

>>if (the number is a character) //don't quit get that

You will need to get the number in a char variable, not an int variable. Then the above line will make sense because you need if( !isdigit(character)) maybe this will make more sense

#include <iostream>
using namespace std;

int foo()
{
	char c;
	cout << "Enter a character\n";
	cin.get(c);
	if( !isdigit(c) )
		throw "Not a digit";
	return c;

}

int main()
{
	char c = 0;
	try
	{
		c = foo();
	}
	catch(const char* msg)
	{
		cout << msg << "\n";
	}
}

Edited 7 Years Ago by Ancient Dragon: n/a

This question has already been answered. Start a new discussion instead.