Solved

Whats wrong with tthis code?

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

757 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

18 Experts available now in Live!

Get 1:1 Help Now