By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,213 Members | 1,401 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,213 IT Pros & Developers. It's quick & easy.

Try/Catch/Throw...Can't get data read in right, and stuck in an infinite loop!

P: 17
I am trying to read in several lines, each should have exactly 5 pieces of data. I am using try/catch/throw to determine if the data is in the correct format, and trying to use iss to separate the parts. However, this is my first time using both, and I am pretty certain that I am missing something obvious. Firstly, by debugging, I found that the first piece of data is being checked in the second case, giving me errors. I believe I may need to read in more from the iss>> than just the one, but I am lost. Any suggestions?? Here is my code:

Expand|Select|Wrap|Line Numbers
  1.     ifstream indata;
  2.     ifstream indata2;
  3.     string instring;
  4.     string instring2;
  5.     istringstream iss_string;
  6.     istringstream iss_string2;
  7.     string data[5];
  8.     string data2[5];
  9.     int count = 0;
  10.     int count2 = 0;
  11.     int datacount = 0;
  12.     int offset;
  13.     int offset2;
  14.  
  15.     indata.open("student.data");
  16.     if(!indata){
  17.         cout<<"student.data file could not be opened.";
  18.         exit(1);}
  19.  
  20.     while(indata){
  21.         getline(indata, instring);
  22.  
  23.         try{
  24.             while(instring != "0"){
  25.                 iss_string.str(instring);
  26.                 iss_string >> data[count];
  27.                 datacount++;
  28.                 offset = iss_string.tellg();
  29.                 while(offset >= 0){
  30.                     switch(count){
  31.                         case 0:
  32.                             if(data[count].length() != 9){
  33.                                 throw 0;
  34.                                 break;
  35.                             }
  36.                             for(int i=0; i < (data[count].length()); i++){
  37.                                 if(!isdigit(data[count][i])){
  38.                                     throw 0;
  39.                                     break;
  40.                                 }
  41.                             }
  42.                         case 1:
  43.                             if(data[count].length() > 30 || data[count].length() < 1){
  44.                                 throw string("Invalid number of characters in Last Name of student.data file");
  45.                                 break;
  46.                             }
  47.                             for(int i=0; i<data[count].length(); i++){
  48.                                 if(!isalpha(data[count][i])){
  49.                                     throw string("Invalid characters in Last Name of student.data file");
  50.                                     break;
  51.                                 }
  52.                             }
  53.  
  54.                         case 2:
  55.                             if(data[count].length() > 30 || data[count].length() < 1){
  56.                                 throw string("Invalid number of characters in First Name of student.data file");
  57.                                 break;
  58.                             }
  59.                             for(int i=0; i<data[count].length(); i++){
  60.                                 if(!isalpha(data[count][i])){
  61.                                     throw string("Invalid characters in First Name of student.data file");
  62.                                 }
  63.                             }
  64.  
  65.                         case 3: 
  66.                             if(data[count].length() != 2){
  67.                                 throw string("Invalid number of characters in Major of student.data file");
  68.                                 break;
  69.                             }
  70.                             for(int i=0; i<data[count].length(); i++){
  71.                                 if(!isalpha(data[count][i])){
  72.                                     throw string("Invalid characters in Major of student.data file");
  73.                                 }
  74.                             }
  75.  
  76.                         case 4: 
  77.                             if(data[count].length() != 2){
  78.                                 throw string("Invalid number of characters in Year of student.data file");
  79.                             }
  80.                             for(int i=0; i<data[count].length(); i++){
  81.                                 if(!isalpha(data[count][i])){
  82.                                     throw string("Invalid characters in Year of student.data file");
  83.                                 }
  84.                             }
  85.                             //break;
  86.                     }
  87.  
  88.                 }
  89.             }
  90.  
  91.             count++;
  92.             iss_string>>data[count];
  93.             offset = iss_string.tellg();
  94.             if(datacount != 5){
  95.                 throw string("Invalid number of items in line of student.data.");
  96.  
  97.                 //break;
  98.             }
  99.             getline(indata, instring);
  100.             iss_string.clear();
  101.             count = 0;
  102.         }
  103.         catch(int x){
  104.             cout<<"Invalid ID number in student.data file";
  105.             exit(1);
  106.         }
  107.         catch(string s){
  108.             cout<< s <<endl;
  109.             exit(1);
  110.         }
  111.     }
  112.     indata.close();
  113.  
Apr 2 '08 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
Start by reducing the size of your try block.

A try block should cover one or may be two statements.

Also, a parser is built using a finite-state automaton and not a giant switch staement. You might read: http://bytes.com/forum/thread660060.html.
Apr 3 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.