void AddPointRoutine()
{
//refresh the openGL window to get rid of the menu
glutPostRedisplay();
cout << "..--==<ADD POINT>==--.." << endl;
char Input[4];
//get the number of the verticies the user would like it to be.
int pos = -1;
while (pos <= 0 || pos > Obj1.GetNumVerticies()+1)
{
cout << "What point would you like it to be? (Valid Range 1-" << Obj1.GetNumVerticies()+1 << ") :";
cin >> Input;
//error checking, see if it is actully in the specified range
if (sscanf(Input, "%f", pos) != 1)
cout << "That is not a valid input, it must be a positive integer." << endl;
else
if (pos <= 0 || pos > Obj1.GetNumVerticies()+1)
cout << "That is not a valid position number." << endl;
}
//now get the x coordinate
//and the y coordinate
cout << "Point Added Successfully" << endl;
glutPostRedisplay();
}
I need some error checking help
I am writing a program that is to add a point to a linked list of points. I''m trying to do error checking to see if the point is in the specified range and that it isn''t a character.
I get an error when I enter a -1, which just locks the system.
The point should be between 0 and X.
Can anyone help me with my problem? The source code for the function is below
some errors:
* sscanf(Input,"%f",pos) puts a float into an int!!!
* the function call should be sscanf(Input,"%d",&pos);
* from MSDN:
Return Value of sscanf(...)
Each of these functions returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end of the string is reached before the first conversion.
So I don''t see why you check if sscanfs return is not 1... Do you only want an integer of 1 digit?
it should be:
You check it to be positive when you if(pos<=0||.....
"Everything is relative." -- Even in the world of computers.
* sscanf(Input,"%f",pos) puts a float into an int!!!
* the function call should be sscanf(Input,"%d",&pos);
* from MSDN:
Return Value of sscanf(...)
Each of these functions returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end of the string is reached before the first conversion.
So I don''t see why you check if sscanfs return is not 1... Do you only want an integer of 1 digit?
it should be:
if (sscanf(Input, "%d", &pos) == EOF) cout << "That is not a valid input, it must be an integer." << endl;
You check it to be positive when you if(pos<=0||.....
"Everything is relative." -- Even in the world of computers.
everything is relative. -- even in the world of computers... so PUSH BEYOND THE LIMITS OF SANITY!
The correct code (runs perfectly)
"Everything is relative." -- Even in the world of computers.
cout << "..--==<ADD POINT>==--.." << endl; char Input[4]; int pos = -1; int result; while (pos <= 0 || pos > Obj1.GetNumVerticies()+1) { cout << "What point would you like it to be? (Valid Range 1-"<<Obj1.GetNumVerticies()+1<<") :"; cin >> Input; //error checking, see if it is actully in the specified range result=sscanf(Input, "%d", &pos); if (result<1||result==EOF) cout << "That is not a valid input, it must be an integer." << endl; else if (pos <= 0 || pos > Obj1.GetNumVerticies()+1) cout << "That is not a valid position number." << endl; } //now get the x coordinate //and the y coordinate cout << "Point Added Successfully" << endl;
"Everything is relative." -- Even in the world of computers.
everything is relative. -- even in the world of computers... so PUSH BEYOND THE LIMITS OF SANITY!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement