Exception handing
CC People :: Studies :: Programming
Page 1 of 1•
Exception handing
Haiz....facing 2 problem now...
first....how to avoid the program run error when we declare a integer...but user key in the character???
i heared can use ATOI...bt i dunno how to use it....cant find on book also....pls share the experience...tq
second....when i load the data from the text file....the datatype i load cant both string and integer....if i do that, the arrangement of the data will error....just like i omnly hv 2 student den the data will come out with 3 student.....bt den if i delete the integer part...the program will run with no error....i cant solve it.... (include the space for the data....if no space for data....no problem for the program....)
ycy
first....how to avoid the program run error when we declare a integer...but user key in the character???
i heared can use ATOI...bt i dunno how to use it....cant find on book also....pls share the experience...tq
second....when i load the data from the text file....the datatype i load cant both string and integer....if i do that, the arrangement of the data will error....just like i omnly hv 2 student den the data will come out with 3 student.....bt den if i delete the integer part...the program will run with no error....i cant solve it.... (include the space for the data....if no space for data....no problem for the program....)
ycy
Re: Exception handing
- Code:
int atoi(char* input);
above is how atoi works, you put a char* in, and it returns a integer from it, eg:
- Code:
int x = atoi("1234");
cout<<x; //this will print 1234
x = atoi("12a34"); //x is 12
x = atoi("abc"); //x is 0
x = atoi("a123b"); //x is also 0 here
the advantage of atoi is it can read the first integer until the end or until something that is not an integer (refer example above). However the disadvantage is you don't know user had input an invalid input, ("12a34" returns as 12, which is an integer anyway).
to know if the user had input an invalid input (as string), you need to check the user input one by one, via a loop. Then you confirmed that the user had input a valid input, then only you atoi it, else your show error to user.
and for your 2nd question... can see your file input and output function?
my second problem......
- Code:
getline(in,s);
old->name=s;
getline(in,s);
old->gender=s;
in>>mat;
old->matrix=mat;
getline(in,s);
old->religion=s;
gt error when print out.....coz gt integer and string.....
- Code:
getline(in,s);
old->name=s;
getline(in,s);
old->gender=s;
getline(in,mat);
old->matrix=mat;
getline(in,s);
old->religion=s;
cannot......coz mat is integer......
- Code:
getline(in,s);
old->name=s;
getline(in,s);
old->gender=s;
//getline(in,mat);
//old->matrix=mat;
getline(in,s);
old->religion=s;
no error when print out......coz no hv integer......all is string......
i dunno wat is the problem (y like this)......
Re: Exception handing
getline returns string, you cant force it into an integer, change mat into string, then atoi it to get integer
Re: Exception handing
I'm facing another problem while doing the exception handling. Since i want it to be possible for the user to key in anything for the input, i made the input as string, then only i check if it's a valid input. If it's not a valid input, an error message shows (if else statement).
But now the problem is, no matter what kind of input i enter, the things inside the else() comes out as there's no else there.
But now the problem is, no matter what kind of input i enter, the things inside the else() comes out as there's no else there.
Re: Exception handing
Can't really just "show my code" since i used this everywhere that has user's input.
For example: i let the user input choice
choice1 = add student
choice2 = delete student
choice3 = quit
invalid input will be for example '5', 'b', '$' this kind of things.
For example: i let the user input choice
choice1 = add student
choice2 = delete student
choice3 = quit
invalid input will be for example '5', 'b', '$' this kind of things.
Re: Exception handing
Getting a valid selection between lower and upper from user and return the user selection, anything (including character) that not within the range will print an error and ask the user to input again.
- Code:
int GetValidSelection(int lower, int upper)
{ char sel[64];
bool b = true;
int j;
while(b)
{ fflush(stdin);
cin.getline(sel,64);
for(int i=0;sel[i]!='\0';i++)
{ if(!isdigit(sel[i]))
{ b=false;
break;
}
}
j = atoi(sel);
if(!b||j<lower||j>upper)
{ cout<<" Your selection is invalid, please enter again: ";
b=true;
}
else
{ break;
}
}
return j;
}






