Error Traping

Pages: 12
yes. if the amount is invalid, it will go to the Insert coin:

You get it sir andy. I just want to trap the error that if the user inputs an invalid amount or a letter, the program will not accept it :)
closed account (SECMoG1T)
well for avoiding letters you'll have to clear your streams because an error will be set
cin.clear(); cin.ignore(number of characters to ignore,'\n'); is probably the way to do it.
So if i put this codes, cin.clear(); cin.ignore(2,3,4,5,6,7,8,9,'\n) it will not accept the following numbers? Or i just don't get it?
Last edited on
closed account (SECMoG1T)
ooh sorry for the late reply but i just got home, well that's not the way 'ignore' works it just help you to empty the stream buffer, for example if a stream was expecting an integer but instead you gave a alpha character then an error state will be set but the character will persist in the buffer so that your stream will always attempt to read it in a loop like manner , you will be required to empty the buffer to continue your program, you might use a condition to check the state of your stream, for example

1
2
3
4
5
6
if(!cin)
{
   cin.clear();/// clear the error
   cin.ignore(10,'\n');///ignore about 10 characters until end-of -line that might have caused 
                               ///the error
}
Last edited on
Its okay sir. Now I get it :D

But why when i try this, it doesn't ignore the alpha character?

1
2
3
4
5
6
7
8
        char choice;

	cout<<"x. Buy a drink"<<endl;
	cout<<"y. Check the item list"<<endl;
	cout<<"z. Exit"<<endl<<endl;

	cout<<"Enter your choice: "; cin>>choice;
	if (!cin) { cin.clear(); cin.ignore(10,'\n'); }
Last edited on
closed account (SECMoG1T)
well it will not ignore them because the type of variable "choice"{char} is compatible with the data you provided, probably i need to make things more clearer to avoid some confusion, this how it goes:

- the library difines an input stream for each primitive data type {ints,doubes,floats,char}
- a stream will only read data into a variable if it is compatible with the variable's data type or
the value available can be implicitly converted to the data type for example you can
read a char to a string, a double to an int and vice versa but you can't read a char into
an int or an integral type because there is no implicit conversion available, that's
a dead end and the stream doesn't know what to do so instead it will flag an error
to indicate something went wrong somewhere.

Basically three error states can be set
1. when the stream hits the end-of -file marker the {eofbit} is set.
2. when the stream encounters an error of invalid types like i described above the
{failbit} is set;
3. when the stream encounters a system level error a {badbit} is set.

in all of those you can recover from the 1st two by clearing the stream and continue using it like in the example i gave you, the last error
is unrecoverable and rarely occurs.

1
2
3
4
5
6
7
8
if(!cin)///here the stream provides an implicit conversion to a bool that allows you to
 {       /// enquire about it's usage state {goodbit} will return true while the above bits
          ///returns false;
    cin.clear();///this sets the goodbit
    cin.ignore(100,'\n');///this tells the stream to ignore 100 characters that are in the 
                                  ///stream buffer or any characters less that hundred but before the 
                                 ///'\n' character is encountered.
}///the stream can then be used again. 
Last edited on
I'll use an easier way.

1
2
3
4
5
6
7
8
9
10
11
12
string a;
int b;

try{
if(!(istringstream (a) >> b)) throw a;
return b;
}

catch(string)
{
cout<<"Error Message";
}


I 4get bout whether we need to include a library or not.
@andy so if I want that choice will not accept letter, I'll make it int? :)

@quisite, that is try-catch right? How's that work?
closed account (SECMoG1T)
so if I want that choice will not accept letter, I'll make it int? :)
more generally an integral type but if you use letters you'll have to take care of the error states.
okay andy. thanks!
Andy, can I use void function inside the int main?
closed account (SECMoG1T)
ooh any function can be used{called} within main, however all declarations and definitions must happen outside main.

edit: thought you said yesterday you didn't have an idea about functions xD.
Thanks! So I can use this if possible?

1
2
3
4
5
6
7
8
int main()
{
    int x,y,z;

void Operations(void)
{
    z = x + y;
}


I still research so I can expand my knowledge. Haha :D
closed account (SECMoG1T)
close to that but you have to define the function outside the scope of main similar to this

1
2
3
4
5
6
7
8
9
10
11
12
13
int Operations(int a,int b);//function prototype| declaration

int main()
{
    int answer=Operations(5,5);// calling the function
    std::cout<<answer<<std::endl;
}

int Operations(int a,int b) ///function definition
{
    return a+b;
}
@ryanjoshiii
about that try-throw-catch that i provide, im actually nt really sure about what it mean but as what i understand (istringstream (s) >> b) this code will stream all the character in string s and try to put inside int b if the character is int then i put a !(istringstream (s) >>b) this mean if the character in string contain a character which is not int it will become true as istringstream (s) >> b will give false if the string contain a character not an int
Topic archived. No new replies allowed.
Pages: 12