this is what my program looks like but after prompting and reading my files, i get an infinite loop. the program compiles and i just put that simple cout statement just to see if anything will come out. can anyone see where i am messing up within my main that is causing this infinite loop?

#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
#include <iomanip>

using namespace std;

const int MAX = 30;
const int MAX_HW = 10;
const int MAX_E = 3;


struct file1
{
  int id;
  string fname;
  string lname;
  int hw [MAX_HW];
  int exam [MAX_E];
  double exam_score;
  double hw_score;
  int totpts;
};

void fixstring (string&);
void get_data (file1 [], int&);
void alpha (file1 [], int);
void initial (file1 [], int);
void first (file1 [], int);


int main ()
{
  string filename1;
  string studentinfo;
  string filename2;
  string gradeinfo;
  string filename3;
  string results;
  char ch;
  char letter_score;
  int x, x1, y, y1, z, z1;
  double pct;
  int id_temp;
  int hw_score;
  int exam_score;
  int hw_num;
  int exam_num;


  file1 names [MAX];
  //file2 grades [MAX];

  ifstream inf1;
  ifstream inf2;
  ofstream outf;
  int n = 0;

  cout << "Please enter name of student information file:" << endl;
  cin >> filename1;
  cout << "Please enter name of grade information file:" << endl;
  cin >> filename2;
  cout << "Please enter name of output file:" << endl;
  cin >> filename3;

  inf1.open (filename1.c_str());
  inf2.open (filename2.c_str());
  outf.open (filename3.c_str());
 inf1 >> names[n].id;

  while (!inf1.eof())
    {

      inf1 >> names[n].lname >> names[n].fname;
      fixstring (names[n].lname);
      fixstring (names[n].fname);
      n++;
      inf1 >> names[n].id;
    }



  for (int hw_r = 0; hw_r < n; n ++)
    {
      for (int hw_c = 1; hw_c <= MAX_HW; hw_c ++)
        {
          names[hw_r].exam[hw_c] = 0;
        }
    }
  for (int exam_r = 0; exam_r < n; exam_r++)
    {
      for (int exam_c = 1; exam_c <= MAX_E; exam_c ++)
        {
          names[exam_r].exam[exam_c] = 0;
        }
    }

  inf2 >> id_temp;
  while (!inf2.eof())
    {
      for (int x = 0; x < n; x ++)
        if (id_temp == names[x].id)
 {
            inf2 >> ch;
            while (ch != 'Q')
              {
                if (ch == 'H')
                  {
                    inf2 >> hw_num;
                    inf2 >> names[x].hw[hw_num];
                  }

                else if (ch == 'E')
                  {
                    inf2 >> exam_num;
                    inf2 >> names[x].exam[exam_num];
                  }
                inf2 >> ch;
              }
          }
      inf2 >> id_temp;
    }



  for (int x = 0; x < n; x ++)
    {
      names[x].totpts = 0;

      for (int hw_num = 1; hw_num <= MAX_HW; hw_num ++)
        {
          names[x].totpts = names[x].totpts + names[x].hw[hw_num];
        }

      for (int exam_num = 1; exam_num <= MAX_E; exam_num ++)
        {
          names[x].totpts = names[x].totpts + names[x].exam[exam_num];
 }
    }


  alpha(names, n);
  outf << right << setw (4) << "NAME" << setw (40) << "ID#" << endl;
  for ( int x = 0; x < n; x ++)
    {
      //outf << names[n].fname << ", " << names[n].lname << names[n].id << endl;
      outf << names[x].lname << ", " << names[x].fname << right << setw (40 - names [x].lname.length () - names\
 [x].fname.length ()) << names [x].id << endl;
    }
  outf << endl <<endl;

  outf.close ();

  return 0;
}


void fixstring (string& names)
{
  int wlen = names.length();
  names[0] = toupper (names[0]);
  for (int i = 1; i < wlen; i++)
    names [i] = tolower (names[i]);
}

void alpha ( file1 names [], int n)
// Given the string "names", alpha will read in the file and place and arrange the names accordingl \
  // y and put them in a particular order.  The alphabetized array of strings will then be passed back.
{
  int i;// initializing integer datatype to i
  int j;// intiializing integer datatype to j
  file1 temp;// string datatype being justified for temp to be used within function
 for (i=0; i < n-1; i++)
    {
      for (j=0; j < n - (i+1); j++)
        {
          if (names[j].lname > names [j+1].lname)
            {
              temp = names[j];
              names [j] = names [j+1];
              names [j+1] = temp;
            }
        }
    }

}


/*void initial (file1 names[], int n)
  {
  int x, q;
  for (x = 0; x < n; x++)
  {
  cin >> names[n].id;
  for (q = 0; q = 10; q++)
  names[n].hw_num[q] = 0;
  }
  }*/

line 87: array elements are numberd from 0 to but not includeing the number of elements in the array. So that loop should be coded like this:
for (int hw_c = 0; hw_c < MAX_HW; hw_c ++)

line 89: that's initializing the wrong array. It should be initialzing array hw.

I'll need the two text file in order to actually test your code.

The best way (I find) of troubleshooting is to just stick a whole bunch of print statements in your code ( printf()...or I guess you could use cout no difference).

If you stick specific print statements before and after each loop, you can tell whether your program made it to that point or not. This way, you can isolate whatever loop is the problem and work from there. Note that, though your code may enter an infinite loop, the problem may lie in previous code...so keep a sharp eye out!

(Note: I always think it best to force you to think about the problem rather than just giving you a straight up answer. Fact is, every1 runs into these problems, no matter how experienced a programmer you are. So its could practice to learn to troubleshoot your own code. That said, I also didn't bother to analyze your code...thought it would take too long. So this way, its a win win situation -- you learn to troubleshoot, and I don't need to figure out what you are doing in your program ;)

But if you still need help, post back and I, or someone else, will take a closer look...)

EDIT: apparently somebody else has already replied, so nvm :P

this is what my data looks like


studentinfo
567 white robert
43 blackburn donna
722 Grey jAMes
19 bRoWn jAnE


gradeinfo
722 E 1 95 E 2 89 E 3 90 Q
567 E 1 67 H 1 20 H 5 20 E 2 76 Q
83 this line should be ignored, 83 is not a valid ID#
19 H 1 20 H 2 20 H 3 19 H 4 18 H 5 16 Q
43 E 3 91 Q
19 H 6 20 H 7 20 H 8 20 H 9 20 H 10 18 Q
722 H 5 20 H 6 20 H 7 20 Q
19 E 1 85 E 2 90 Q
567 E 3 80 Q
722 H 4 15 H 3 14 H 2 20 H 1 20 Q

i have adjusted all my loops accordingly and i fixed that array but i'm still getting the infinite loop after i prompt and read in my output file

The infinit loop is here: for (int hw_r = 0; hw_r < n; n ++) That should be hw_r++, not n++. That's an easy mistake to make, I think we have all done that at one time or another.

thank you. i just saw it also. there was minore tweakings i had to do through the prog too but i think i'm on the right track now. fingers crossed

The best way (I find) of troubleshooting is to just stick a whole bunch of print statements in your code ( printf()...or I guess you could use cout no difference).

The best was to do it is to learn how to use your compiler's debugger so that you can easily see that the program is doing and the value of all variables at any given time.

The loops that read the data are also coded wrong -- there is no need for using eof() at the top of the loop. Here is a better and more accurate way to code those loops

while(n < MAX && inf1 >> names[n].id >> names[n].lname >> names[n].fname)
  {
      fixstring (names[n].lname);
      fixstring (names[n].fname);
      n++;
  }
This article has been dead for over six months. Start a new discussion instead.