Solved

Whats wrong with tthis code?

Posted on 2003-11-22
2
241 Views
Last Modified: 2010-04-01
Hi I'm new at Borland c++ and having a hard time understanding why this code don't work right?

This code is for me to import from a file it works for the FName and goes into the if (count == 2) but what happens is that num is ' ' so it don't get LNAme it goes into else if (num == ' ') so it never gets the LName from the file and sets it to LName..  THANKS change the code how you like to get this to work also if you would like to see how exporting to a file i can display that also


char num;
AnsiString FName = "";
AnsiString LName = "";
AnsiString DSOS = "";
AnsiString Program = "";
AnsiString Degree = "";
AnsiString Course = "";
AnsiString Space = "";
int count = 1;

student_schedule.student_id.FName = "";
student_schedule.student_id.LName = "";
student_schedule.student_id.DSOS = "";
student_schedule.student_program.Program = "";
student_schedule.student_program.Degree = "";
student_schedule.Course = "";


ifstream infile("outfiledata.txt");
assert(infile);
Form1->ListBox1->Clear();

while(!infile.eof())     // Keeps going until there are no more records
 {
   while(infile.get(num))   // Keeps on going until I hit the end of a record then resets and starts again
     {


 if (count == 1)
  {

    if((num != '\n') && (num !='*') && (num != ' ')) // Triggers until I hit the end of a word or the end of the record
     {
      FName = FName + AnsiString(num);
     }
       else if (num == ' ') // Triggers when I hit the end of a word
        {
         student_schedule.student_id.FName = FName;
         count++;
        }
   }


  if (count == 2)
  {
    if ((num != '\n') && (num !='*') && (num != ' ')) // Triggers until I hit the end of a word or the end of the record
     {
      LName = LName + AnsiString(num);
     }
       else if (num == ' ') // Triggers when I hit the end of a word
        {
         student_schedule.student_id.LName = LName;
         count++;
        }
   }

 if (count == 3)
  {

    if ((num != '\n') && (num !='*') && (num != ' ')) // Triggers until I hit the end of a word or the end of the record
     {
      DSOS = DSOS + AnsiString(num);
     }
       else if (num == ' ') // Triggers when I hit the end of a word
        {
         student_schedule.student_id.DSOS = DSOS;
         count++;
        }
   }

 if (count == 4)
  {
    if ((num != '\n') && (num !='*') && (num != ' ')) // Triggers until I hit the end of a word or the end of the record
     {
      Program = Program + AnsiString(num);
     }
       else if (num == ' ') // Triggers when I hit the end of a word
        {
         student_schedule.student_program.Program = Program;
         count++;
        }
   }

 if (count == 5)
  {
    if ((num != '\n') && (num !='*') && (num != ' ')) // Triggers until I hit the end of a word or the end of the record
     {
      Degree = Degree + AnsiString(num);
     }
       else if (num == ' ') // Triggers when I hit the end of a word
        {
         student_schedule.student_program.Degree  = Degree;
         count++;
        }
   }

 if (count == 6)
  {
    if ((num != '\n') && (num !='*') && (num != ' ')) // Triggers until I hit the end of a word or the end of the record
     {
      Course = Course + AnsiString(num);
     }
       else if (num == ' ') // Triggers when I hit the end of a word
        {
         student_schedule.Course  = Course;
         count = 100;
        }
   }

   if (num == '*')   // Triggers when I have hit the delimiter *
     {
         Form1->ListBox1->Items->Add("This is the end of the file");
         FName = "";
         LName = "";
         DSOS = "";
         Program = "";
         Degree = "";
         Course = "";
      }


      }
  }


   infile.close();
   Form1->ListBox1->Items->Add("Data Fname");
  Form1->ListBox1->Items->Add(student_schedule.student_id.FName);
     Form1->ListBox1->Items->Add("Data Lname");
  Form1->ListBox1->Items->Add(student_schedule.student_id.LName);
     Form1->ListBox1->Items->Add("Data DSOS");
  Form1->ListBox1->Items->Add(student_schedule.student_id.DSOS);
   Form1->ListBox1->Items->Add("Data Program");
  Form1->ListBox1->Items->Add(student_schedule.student_program.Program);
     Form1->ListBox1->Items->Add("Data Degree");
  Form1->ListBox1->Items->Add(student_schedule.student_program.Degree);
        Form1->ListBox1->Items->Add("Data Course");
  Form1->ListBox1->Items->Add(student_schedule.Course);
0
Comment
Question by:rwatson119
2 Comments
 
LVL 5

Expert Comment

by:arjanh
ID: 9804449
After you have found the first word and found the space, you have to read the next character from file. So, you have to always process the num character in exactly 1 if statement (currently, because of incrementing count in the count==1 if block, you immediately enter the count==2 block, but instead you need to first read the next character from file):

while(!infile.eof())     // Keeps going until there are no more records
 {
   while(infile.get(num))   // Keeps on going until I hit the end of a record then resets and starts again
     {

 if (count == 1)
  {
      ...
  }
 else if (count == 2)
  {
      ...
  }
 else if (count == 3)
  {
      ...
  }
 else if (count == 4)
  {
      ...
  }
 else if (count == 5)
  {
      ...
   }
 else if (count == 6)
  {
      ...
  }

   if (num == '*')   // Triggers when I have hit the delimiter *
     {
      ...
      }


      }
  }
0
 
LVL 22

Accepted Solution

by:
grg99 earned 250 total points
ID: 9804690
Also you might note that you have basically the same code repeated six times, the only difference being the variable you put the result into.  You could factor out the similar code into a function, or just do the same code each time, just put the result into a different array element, something like AllTheData[ count ].  That will shorten your code considerably, and you'll have 1/6the the amount of code to debug.


0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question