Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Whats wrong with tthis code?

Posted on 2003-11-22
2
Medium Priority
?
244 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 750 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

688 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