I already seen this link: http://www.cplusplus.com/doc/tutorial/exceptions/
and it doesn't make a lot of sense to me, since it says "//code here" instead of showing what the code should be.
I want this program to catch letters. so that if someone uses them instead of numbers it will close the program, instead of going into the never ending cycle.
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
int main()
{
//Usings
usingnamespace std;
//Ints
int a, b;
int c;
//No Char, Strings, Double, Float, Bools
//Enter in Value of 'A'
cout << "\n\n\t\t Enter Value For 'A'\n\t\t\t\t====> ";
cin >> a;
//Enter in Value of 'B'
cout << "\n\n\t\t Enter Value For 'B'\n\t\t\t\t====> ";
cin >> b;
//Declare 'C'
c=(a+b);
//Last message
cout << "\n\n\t\t C = " << c << "\n";
//Used to avoid C++ religious wars
cin.get();
//Not a VOID main()
//return value
return 0;
}
I'm not sure of the first step, and all the tutorials (the link you gave included) are talking about more advanced programming, that deal files, etc. I'm just wanting someone to show me the code from my first post, used with the try/catch/throw. So I can have some idea of how to do this in the future.
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
int main()
{
usingnamespace std;
int a, b;
int c;
try
{
cout << "\n\n\t\t Enter Value For 'A'\n\t\t\t\t====> ";
cin >> a;
}
cout << "\n\n\t\t Enter Value For 'B'\n\t\t\t\t====> ";
cin >> b;
c=(a+b);
cout << "\n\n\t\t C = " << c << "\n";
cin.get();
return 0;
}
ne555, I'm thankful you are trying to help, but the code you posted made this harder to grasp. The only thing I understand is that "you must throw something, to catch something". Please keep in mind I'm new. and:
You try to read a number. If that fails you throw an exception (in this case the number 42).
Inside the catch block you are sure that something went wrong in your program (in this case corrupted input).