Rick Doll

I am just begining my C++ excursion. I have some experiance in C#.

In console based program in C# you can run a try catch and parse the imput to catch a system format exception to check for non intergers.

After a bit of looking on the net I have not found a good way yet to do the same for C++.

I would appreciate a solid link or good explanation for catching errors on non numeric imput for C++ if you may.

Thank you.



Re: Visual C++ General Parsing/errorcheck.

Sahir Shah

Rick Doll wrote:

I would appreciate a solid link or good explanation for catching errors on non numeric imput for C++ if you may.

You can use the Parse method for this. As in

Code Snippet

double value = 0;

Console::WriteLine("Please enter the value ");

try {

value = double::Parse(Console::ReadLine());

}

catch(Exception^ e) {
Console::WriteLine("Error!!!");
}

Rgds

Sahir Shah





Re: Visual C++ General Parsing/errorcheck.

Ramkrishna Pawar

Sahir Shah wrote:
Rick Doll wrote:

I would appreciate a solid link or good explanation for catching errors on non numeric imput for C++ if you may.

You can use the Parse method for this. As in

double value = 0;

Console::WriteLine("Please enter the value ");

try {

value = double:: Parse(Console::ReadLine());

}

catch(Exception^ e) {
Console::WriteLine("Error!!!");
}

Rgds

Sahir Shah

P.S. ignore the space between :: and Parse. It's to prevent the forum software from turning : P into the smiley . The remaining part of the method name kinda makes it worse

Sahir,

You can use the 'Code Block' feature, just select the code and press the code block button in the toolbar.






Re: Visual C++ General Parsing/errorcheck.

Sahir Shah

Ramkrishna Pawar wrote:

Sahir,

You can use the 'Code Block' feature, just select the code and press the code block button in the toolbar.

I thought it was too short a block to use the code block. You are right , using the code block prevents the appearance of smilies and vulgar method names Big Smile





Re: Visual C++ General Parsing/errorcheck.

Rick Doll

Isn't that code C# or is C++ using some C# code now

I made a method called testing and placed all that code in there and called it from the main and it blasted out all sorts of errors not recognizing console, and saying catch needed just one argument, and parse was not a part of global namespace, etc.





Re: Visual C++ General Parsing/errorcheck.

Shakje

If you create a C++ .NET project you will get all the functionality of .NET that you are used to in C# (although there are a couple of differences and some syntax differences in the way you call things and create things). If you don't compile it as a .NET project you won't be able to access .NET classes or namespaces and so won't be able to do the above that way.

If you want to use a non-.NET project, you can use the ato functions (replacing the with the appropriate letter, eg. atoi). Unfortunately these functions do not throw exceptions, but return the rather ambiguous 0 if they fail, or various defines if they are out of range. If you know the value will not hit 0 you could do the following:

Code Snippet

char szInput[] = "1234";

int a = atoi(szInput);

if(a == 0)

printf("Could not convert string, not a valid number.");

if(a == INT_MAX || a == INT_MIN)

printf("Number out of integer range");

It all depends on how you want to do things, another example would be using atof for getting floats from strings, it's used in pretty much the same way.

Sorry for the printfs, I'm working on C at the moment.

Also, C# is based on C++, and both have had access to .NET for the same period of time. Asking if C++ is using C# code just sounds wrong.

While it sounds pretty certain that you are using a native C++ project, if you're not have you made sure you're using the System namespace






Re: Visual C++ General Parsing/errorcheck.

Rick Doll

Be honest with you not sure lol.

This is actually my first C++ project, and I am using a text book copywrited 2002 and talks code that is to be used with visual C++ 6 so it is a bit out of date. Being that I am using VS2005 a few things they talk about don't even work lol, and I have to research the new way of doing it when I am not even totaly sure of the old way Smile

Thew following is the code I am using for my simple like project we have to do.

The project itself is easy enough, but coming from 8 months of working on C# classes I feel error checking is almost the most important part of any program and flipping through my textbook it doesn't seem to show one bit of catching an error if they enter something invalid...

I am not in the habbit of turning in a program that can be broke Smile

Code Snippet

#include <iostream>

#include <string>

using namespace std;

void GetNumberOne();

void GetNumberTwo();

void MaximumValue();

int numberOne;

int numberTwo;

int main()

{

GetNumberOne();

GetNumberTwo();

MaximumValue();

return 0;

}//closes function.

void GetNumberOne()

{

string errorFlag = "";

cout << "Please enter your first number.\n";

cin >> numberOne;

}//closes function.

void GetNumberTwo()

{

string errorFlag = "";

cout << "Please enter your second number.\n";

do

{

cin >> numberTwo;

if(numberTwo == numberOne)

{

cout << "You have entered a number of the same value as your first. Please select another.\n";

errorFlag = "error";

}//closes if.

else

{

errorFlag = "noError";

}//closes else.

}//closes do.

while(errorFlag == "error");

}//closes function.

void MaximumValue()

{

if(numberOne > numberTwo)

{

cout << numberOne << " is the greater number.\n";

}//closes if.

else

{

cout << numberTwo << " is the greater number.\n";

}//closes else.

}//closes function.





Re: Visual C++ General Parsing/errorcheck.

Sahir Shah

Rick Doll wrote:

I made a method called testing and placed all that code in there and called it from the main and it blasted out all sorts of errors not recognizing console, and saying catch needed just one argument, and parse was not a part of global namespace, etc.

That's because the project you created in VS2005 is either an MFC or Win32 project. The code I posted was for a CLR (Common Language Runtime) application.

Rick Doll wrote:

I would appreciate a solid link or good explanation for catching errors on non numeric imput for C++ if you may.

C++ does have an exception handling mechanism. Here is an example

Code Snippet

#include "stdafx.h"
#include <iostream>
using namespace std;
class A {
public :
void ErrMsg(){
cout<<"An error was thrown\n";
}
};

void foo() {
throw A();
}
int _tmain(int argc, _TCHAR* argv[]) {
try{
foo();
}
catch(A a) {
a.ErrMsg();
}
try {
foo();
}
catch(...) {
cout<<"an unspecified exception was thrown\n";
}
return 0;
}

For converting a string to a numeric type please see this FAQ

http://www.codeguru.com/forum/showthread.php t=231054

Many methods are described there. Use one you are comfortable with, my recommendation is to use the one with the sub heading "The C++ way". Put the code in a function and throw an exception (as described above) when the function is called with a non numeric argument.

Regards

Sahir Shah