Exception handing

Post new topic   Reply to topic

View previous topic View next topic Go down

Exception handing

Post by zeon01 on Thu Mar 27, 2008 2:42 pm

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 Cool

zeon01
New here!
New here!

Gender:MaleCapricornCat
Posts : 3
Joined : 27 Mar 2008
Age : 20
Location : Jenjarom

Back to top Go down

Re: Exception handing

Post by Lion on Thu Mar 27, 2008 4:16 pm

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?

Lion
Admin
Admin

Gender:MalePiscesCat
Posts : 59
Joined : 23 Mar 2008
Age : 21
Location : Petaling Jaya

Back to top Go down

my second problem......

Post by zeon01 on Sat Mar 29, 2008 10:46 pm

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)......

zeon01
New here!
New here!

Gender:MaleCapricornCat
Posts : 3
Joined : 27 Mar 2008
Age : 20
Location : Jenjarom

Back to top Go down

Re: Exception handing

Post by Lion on Sat Mar 29, 2008 11:07 pm

getline returns string, you cant force it into an integer, change mat into string, then atoi it to get integer

Lion
Admin
Admin

Gender:MalePiscesCat
Posts : 59
Joined : 23 Mar 2008
Age : 21
Location : Petaling Jaya

Back to top Go down

YES......

Post by zeon01 on Sun Mar 30, 2008 12:10 am

yes....yes....yes....

finally i can solve it after headache for long time....

thx, lion...

Very Happy

zeon01
New here!
New here!

Gender:MaleCapricornCat
Posts : 3
Joined : 27 Mar 2008
Age : 20
Location : Jenjarom

Back to top Go down

Re: Exception handing

Post by Zane Cidan on Mon Mar 31, 2008 1:39 pm

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.

Zane Cidan
New here!
New here!

Gender:MaleLeoCat
Posts : 8
Joined : 26 Mar 2008
Age : 21
Location : Imaginary World

Back to top Go down

Re: Exception handing

Post by Lion on Mon Mar 31, 2008 2:17 pm

show me your code see see? which input are you put as "invalid input"?

Lion
Admin
Admin

Gender:MalePiscesCat
Posts : 59
Joined : 23 Mar 2008
Age : 21
Location : Petaling Jaya

Back to top Go down

Re: Exception handing

Post by Zane Cidan on Mon Mar 31, 2008 2:49 pm

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.

Zane Cidan
New here!
New here!

Gender:MaleLeoCat
Posts : 8
Joined : 26 Mar 2008
Age : 21
Location : Imaginary World

Back to top Go down

Re: Exception handing

Post by Lion on Tue Apr 01, 2008 8:09 pm

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;
}

Lion
Admin
Admin

Gender:MalePiscesCat
Posts : 59
Joined : 23 Mar 2008
Age : 21
Location : Petaling Jaya

Back to top Go down

View previous topic View next topic Back to top


Post new topic   Reply to topic
Permissions of this forum:
You cannot reply to topics in this forum