Solved

Whats wrong with tthis code?

Posted on 2003-11-22
2
239 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now