Link to home
Start Free TrialLog in
Avatar of gndcdosps
gndcdosps

asked on

Infinite loop

I have an infinite loop and I am not sure why... If someone could take a look and give me some idea on how to put the breaks on this thing, that would be awesome!  I am simply trying to display from my 'infile'.  I have done this in the main as a test and was successful.  
void VehMakeList(VEHICLE vehRec[], int count){
  cout << "Available vehicle makes:";
int i = 0;
for (in i = 0 ; i < count; i++){
cout << vehRec[i].make;
cout << endl
}
}
Finally, once I get this figured out I need to be able to display each one time.. Here is a sample of my mater file:
Car
Boat
Car
Car
So when I go to display it, I want it to only show a listing of:
Car  //displaying Car a single instance
Boat
Any suggestion in this direction would also help....This portion is not actually part of the assignment, I am just trying to make it a little more "user friendly".

Avatar of Axter
Axter
Flag of United States of America image

The first declaration of int i, does not do anything, because it's being declared within the for loop.

Please post the code you're using to call this function.
Except of a typo in the loop (just 'i', not 'in i'), that looks fine and should work when passing in the correct value for 'count'. What problems are you getting?
FYI:
The posted code would not compile, since it's missing a semicolon.

It would be better if you copied and paste your actual code here, so we can see exactly what your compiler is looking at.

Don't try to transcribe it, because you could leave out a bug, or in this case add a compile error that's not in the original code.
I tried to compile the following

    int i = 0;
    for (int i = 0; i < 2; ++i)
    {
        cout << i << endl;
    }

and it compiled with a warning. however, it was *not* an infinite loop as the incrementation was made for the second (loop) variable. But other compilers may see that differently

Regards, Alex
>> But other compilers may see that differently

No, because the scope rules make sure it doesn't (unless the compiler is broken).
Avatar of gndcdosps
gndcdosps

ASKER

Here is my actual code:
void VehMakeList (VEHICLE vehRec [], int count) {
      cout << "Available vehicle makes: ";
      int i = 0;

      for (int i =0; i < count; i++){
      
            cout << (vehRec[i].make);
                  cout << endl;
            }
}

My screen is outputting what appears to be a bunch of [[[[[[[[[[ ... brackets.  

Here is how it is outputted in my Main....
int main () {

const int MAXREC = 100; // max vehicle records
VEHICLE vehRec [MAXREC];
// Loading Master File of Vehicle Records
ifstream infile ("C:\\Documents and Settings\\Alowe\\Desktop\\CMPSC212\\Vehicle_Finder\\master.txt");
if(!infile) {
      cerr << "Error:  cannot open master.txt file\n";
      infile.close();
      return 1;
}
int count;
count = LoadArray (vehRec, MAXREC, infile);

VehMakeList ( vehRec, MAXREC);
........
 
SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually, we have not started Classes yet... Here is my Vehicle struct:
struct VEHICLE {
      char make [MAXCHAR];
      char model [MAXCHAR];
      short year;
      int doors;
      int color;
      char engineSize [MAXCHAR];
      int airCond;
      int stereo;
      double milage;
      int oilLeak;
      int tires;
      int rust;
};

Here is my LoadArray:
int LoadArray (VEHICLE vehRec [], int limit, istream& infile) {


int i = 0;
while ( i < limit && infile) {
      infile.get (vehRec[i].make, sizeof (vehRec[i].make));
      infile >> ws;
      infile.get (vehRec[i].model, sizeof (vehRec[i].model));
      infile >> ws;
      infile >> vehRec[i].year;
      infile >> ws;
      infile >> vehRec[i].doors;
      infile >> ws;
      infile >> vehRec[i].color;
      infile >> ws;
      infile.get (vehRec[i].engineSize, sizeof (vehRec[i].engineSize)) ;
      infile >> ws;
      infile >> vehRec[i].airCond >> vehRec[i].stereo >> vehRec[i].milage
      >> vehRec[i].oilLeak >> vehRec[i].tires >> vehRec[i].rust;
      infile >> ws;
        if (infile) i++;
}

      // bad data input
      if ( infile.fail() && !infile.eof() )
      {
              cerr << "Bad  data.\n" << "on Line: " << i;

            exit (2);
      }
      //too much data for array
      if (i == limit && infile >> ws && infile.good())
      {
            cerr << "Error:  array bounds exceeded\n";
            exit (3);
      }
      return i;
}

WhenI take the VehMakeList function out of the program, and tested just reading in and outputting the LoadArray, everything worked fine.  

Here is what I was doing to test the output:
/*
ofstream outfile ("C:\\Documents and Settings\\Desktop\\CMPSC212\\Vehicle_Finder\\outfile.txt");
if (!outfile) {
      cerr << "Error:  cannot open the output file outfile.txt\n";
      outfile.close();
      return 2;
}



/*
outfile << fixed << setprecision (2);
for (int i=0; i < count; i++) {

      outfile << left << setw(9) << vehRec[i].make
      << setw(15) << vehRec[i].model
      << setw(8) << vehRec[i].year
      << setw(8) << vehRec[i].doors
      << setw(8) << vehRec[i].color
      << setw(8) << vehRec[i].engineSize
      << setw(8) << vehRec[i].airCond
      << setw(8) << vehRec[i].stereo
      << setw(8) << vehRec[i].milage
      << setw(8) << vehRec[i].oilLeak
      << setw(8) << vehRec[i].tires
      << setw(8) << vehRec[i].rust
      << endl;
}
*/

I will take a look at your other comments in the meanwhile.  Thank you!