Solved

Whats wrong with tthis code?

Posted on 2003-11-22
2
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

630 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