We help IT Professionals succeed at work.

Infinite loop

gndcdosps
gndcdosps asked
on
340 Views
Last Modified: 2010-04-01
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".

Comment
Watch Question

AxterSenior Software Engineer
CERTIFIED EXPERT

Commented:
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.
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
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?
AxterSenior Software Engineer
CERTIFIED EXPERT

Commented:
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
CERTIFIED EXPERT
Top Expert 2009

Commented:
>> But other compilers may see that differently

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

Author

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);
........
 
jkr
CERTIFIED EXPERT
Top Expert 2012
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.