[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 327
  • Last Modified:

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".

0
gndcdosps
Asked:
gndcdosps
  • 2
  • 2
  • 2
  • +2
2 Solutions
 
AxterCommented:
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.
0
 
jkrCommented:
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?
0
 
AxterCommented:
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
itsmeandnobodyelseCommented:
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
0
 
Infinity08Commented:
>> But other compilers may see that differently

No, because the scope rules make sure it doesn't (unless the compiler is broken).
0
 
gndcdospsAuthor Commented:
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);
........
 
0
 
jkrCommented:
I assume that your file is not read correctly, that should be

if(!infile.is_open()) { // 'if (!infile)' is invalid and never 'false'
      cerr << "Error:  cannot open master.txt file\n";
      infile.close();
      return 1;
}
0
 
itsmeandnobodyelseCommented:
>>>> if(!infile) {

There is an operator! defined for istream which returns !istream::fail();

>>>> int i = 0;
>>>> for (int i = 0;

The variable 'i' was defined twice what at least should give a compiler warning. The 'i' used in the loop should be the second one as it was with my sample and Infinity told you that this behavior is according the scope rules. Nevertheless you should omit the second definition in the for loop and change it to
 
      for (i = 0; i < count; i++){

>>>> My screen is outputting what appears to
>>>> be a bunch of [[[[[[[[[[ ... brackets
That means that vehRec[i].make is corrupt or not a printable output. Can you post both the VEHICLE class and the LoadArray?

Regards, Alex
0
 
gndcdospsAuthor Commented:
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!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now