It gives a weird error (according to me).
It says I didn't associate a try block with the catch handler, I tried already a few things, but the errors kept appearing when I compiled the programm. Anyone who knows how to fix this?
This is the code:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <windows.h>
usingnamespace std;
struct id {
int Year;
int Day;
int Month;
int Brother;
int Sister;
int BroSis;
};
int main(){
id first;
int a;
cout<<"Hello!\n";
previous:
try{
cout<<"On which day of the month are you born?\n";
cin>>first.Day;
if ((first.Day > 31) | (first.Day < 1))
throw(1);
cout<<"In what month are you born? \n";
cin>>first.Month;
if ((first.Month < 1) | (first.Month > 12))
throw(2);
cout<<"In what year are you born? \n";
cin>>first.Year;
if (first.Year < 1900)
throw(3);
cout<<"How many brothers/sisters do you have?\n";
cin>>first.BroSis;
if (first.BroSis < 0)
throw(4);
cout<<"How many brothers do you have?\n";
cin>>first.Brother;
if ((first.Brother > first.BroSis) | (first.Brother < 0))
throw(5);
catch(int a)
{
cout<<"Error occurred: ";
if(a==1)
{
cout<<"This day isn't recognized (numbers only allowed).\n";
goto previous;
}
elseif(a==2)
{
cout<<"This month isn't recognized (numbers only allowed).\n";
goto previous;
}
elseif(a==3)
{
cout<<"Invalid year entered (numbers only allowed).\n";
goto previous;
}
elseif(a==4)
{
cout<<"Invalid number of brothers/sisters entered (numbers only allowed).\n";
goto previous;
}
elseif(a==5)
{
cout<<"Invalid number of brothers entered (numbers only allowed).\n";
goto previous;
}
else
{
cout<<"Unknown error.\n";
Sleep(500);
return 1;
}
}
first.Sister = first.BroSis - first.Brother;
ofstream file ("ID.txt");
string line;
char exit;
if (file.is_open())
{
file<<"You are born on "<<first.Day<<"-"<<first.Month<<"-"<<first.Year<< ".\n";
file<<"You have "<<first.Brother<<" brothers and "<<first.Sister<<" sisters.\n";
file.close();
ifstream file ("ID.txt");
while(! file.eof())
{
getline (file,line);
cout<<line<<endl;
}
file.close();
}
else cout<<"Unable to open file\n";
next:
cout<<"Do you want to continue? Y/N\n";
cin>>exit;
if (exit == 'N'){
cout<<"Exiting...\n";
Sleep(500);
return 0;
}
elseif (exit == 'Y')
goto previous;
else
cout<<"Invalid Entry\n";
goto next;
}
These are the errors (compiling included):
1>------ Build started: Project: C++ - Experimental, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>c:\users\t\documents\visual studio 2008\projects\c++ - experimental\c++ - experimental\main.cpp(46) : error C2318: no try block associated with this catch handler
1>c:\users\t\documents\visual studio 2008\projects\c++ - experimental\c++ - experimental\main.cpp(119) : error C2317: 'try' block starting on line '24' has no catch handlers
1>c:\users\t\documents\visual studio 2008\projects\c++ - experimental\c++ - experimental\main.cpp(119) : fatal error C1075: end of file found before the left brace '{' at 'c:\users\thijs\documents\visual studio 2008\projects\c++ - experimental\c++ - experimental\main.cpp(18)' was matched
1>Build log was saved at "file://c:\Users\Thijs\Documents\Visual Studio 2008\Projects\C++ - Experimental\C++ - Experimental\Debug\BuildLog.htm"
1>C++ - Experimental - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Lol, yeah I'm noob, but I don't like for and while loops =]
What do you mean you don't like for and while loops? If you plan to make a career out of programming or want to make anything remotely useful you'll start using them!
Lawl, guys, I'm programming (in C++) for 2 weeks now, I replaced it already, but you can't blame me for not making 10 while loops in one stupid thingy...
We are trying to teach something.
You are free to use as many gotoes as you want
But in most cases you have things which are better than a goto
Anyway, how is making 10 whiles worse than 10 gotoes?
10 whiles would veritably own 10 gotos. At least you can see where the while is going. Goto is so impossible to use correctly that, in general, you'd be better off assuming it's not there. And that is not even close to an acceptable use of goto. So yeah, I'd blame you if you used goto again.
10 while loops would be better than 10 goto statements. The beauty of cin is that it can tell you from its return value whether it worked. You can encapsulate an error test and the range test within the while loop. Better yet, you could write a common function for the I/O where you can test the validity of the operation as well as the range and have only one while loop in that. Take a look at these FAQs and see if you can improve the program with that knowledge. Take a look at items 15.2-15.4. http://www.parashift.com/c++-faq-lite/input-output.html