473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
1 1986
weaknessforcats
9,208 Expert Mod 8TB
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

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

Similar topics

6
by: Erik Cruz | last post by:
Hi. I have read several articles recommending avoid to raise exceptions when possible, since exceptions are expensive to the system. Removing code from Try... Catch blocks can help performance?...
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
5
by: Doug Gifford | last post by:
WROX "Beginning VB.Net" 2nd Edition Using this code from Chapter 11 Page 472: Do Try ' Read a line from the file... currentLine = myReader.ReadLine lineCounter = lineCounter + 1 Catch e As...
9
by: Ed Staffin | last post by:
Hi, In vb6, if I had an error handler I could catch an error, make a correction and then try it again using resume. I want to do the same thing in vb.net. I am trying to deal with some...
7
by: dick | last post by:
in the "try{throw}catch" structure, how the C++ code return the "type" thrown by a function?
2
by: Gary Wessle | last post by:
Hi I am using exceptions in my code and not sure how things are suppose to work in my case. while (1){ try { // call some methods here and use their side effect and returns. break;
4
by: Jeff Jarrell | last post by:
I have a block of code that during development is prone to casting errors. It is mostly a DataReader type thing. It looks something like this. _prtPNID = myDLReader.GetString("prtPNID")...
8
by: Luke Davis | last post by:
I'm new so bear with me. Is there a way for the catch to fix a problem then start the try over again? For example, I have this console application sync a remote database and it takes hours. ...
3
by: yeye.yang | last post by:
hey everybody Does everybody can help me or give me some advise for the cross thread exception catch Here is what I want to do: I have 2 classes "Scenario" and "Step", which have a...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.