Hello,

I am a student and I'm having trouble figuring out why my if statement won't evaluate. I'm not getting any error in my compiler - I'm using Dev-C++ 4.9.9.2

The if statement in question is on line #160 in the code below (full code). You will need the students.txt file also if you plan to run this - I included it's contents at the bottom.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

const int NO_OF_STUDENTS = 6;

struct studentType
{
  string studentFName;
  string studentLName;
  int testScore[4];
  char grade[4];
};

int highScore[4];

int topScores[6][4];

void initialize(ifstream& indata, studentType list[], int listSize);
void processScores(studentType list[], int listSize);

int main()
{
 ifstream infile;
 string inputFile;
 studentType studentList[NO_OF_STUDENTS];

 cout << "Enter the file name for file containing all test records: ";
 cin >> inputFile;
 cout << endl;

 infile.open(inputFile.c_str());

 if(!infile)
 {
  cout << "Cannot open the input file." << endl;
  return 1;
 }

 initialize(infile, studentList, NO_OF_STUDENTS);
 processScores(studentList, NO_OF_STUDENTS);
 
 infile.close();

// The following 6 lines of code delays termination of the program until
// the user presses the enter key. This is a convenience for those using
// a windows environment but it will work on every platform.  
char any_keystroke;
//reads and ignores up to 200 characters, or '\n' is read and ignored
cin.ignore(200, '\n');                                                                
cout << "\n\n";
cout << "Program execution has ended normally.\n";
cout << "Press the enter key to exit.";
cin.get(any_keystroke);

return 0;
}

void initialize(ifstream& indata, studentType list[], int listSize)
{
     int index;
     int allTestScores;

     for (index = 0; index < listSize; index++)
     {
         indata >> list[index].studentFName;
         cout << "fname: " << list[index].studentFName << endl;
         indata >> list[index].studentLName;
         cout << "lname: " << list[index].studentLName << endl;
         for (allTestScores = 0; allTestScores < 4; allTestScores++)
         {
           indata >> list[index].testScore[allTestScores];
           // begin assigning letter grades
           if(0 <= list[index].testScore[allTestScores] && list[index].testScore[allTestScores] <= 50)
             list[index].grade[allTestScores] = 'F';
           else if (51 <= list[index].testScore[allTestScores] && list[index].testScore[allTestScores] <= 65)
             list[index].grade[allTestScores] = 'D';
           else if (66 <= list[index].testScore[allTestScores] && list[index].testScore[allTestScores] <= 75)
             list[index].grade[allTestScores] = 'C';
           else if (76 <= list[index].testScore[allTestScores] && list[index].testScore[allTestScores] <= 89)
             list[index].grade[allTestScores] = 'B';
           else
             list[index].grade[allTestScores] = 'A';
         
           cout << "test score: " << list[index].testScore[allTestScores] << " " << list[index].grade[allTestScores] << endl;
         }
         cout << endl;
         cout << "Student #:" << index << endl;
     }
     cout << "Preparing Grades for Processing..." << endl;
     cout << endl << endl;
}

void processScores(studentType list[], int listSize)
{
     int index;
     int allTestScores;
     int maxXIndex = 0;
     int maxYIndex = 0;
     int absoluteHighest = 0;
     int count = 0;
     int highByStudent[listSize][4];
     int highByTest[listSize][4];
     
     cout << "Begin Grade Processing..." << endl;
     cout << "Found " << listSize << " students.." << endl;
     cout << endl << endl;
     
     // start initialization of category arrays.  This sets all values within the array to 0.
     for (index = 0; index < listSize; index++)
     {
         if (index == 0)
         {
           cout << " Entering Scores Array Initialization Loop. " << endl;
           cout << "<------------------------------------------>" << endl;
         }
         cout << "Row #" << index << ": ";
         for (allTestScores = 0; allTestScores < 4; allTestScores++)
         {
           highByTest[index][allTestScores] = 0;
           cout << highByTest[index][allTestScores] << " ";
           
         }
         cout << endl;
         if (index == 5)
         {
           cout << "<------------------------------------------>" << endl;
           cout << endl;
         }
     }
     for (index = 0; index < listSize; index++)
     {
       cout << "Row #" << index << ": ";
       for (allTestScores = 0; allTestScores < 4; allTestScores++)
         {
           highByStudent[index][allTestScores] = 0;
           cout << highByStudent[index][allTestScores] << " ";
         }
         cout << endl;
         if (index == 5)
         {
           cout << "<------------------------------------------>" << endl;
           cout << endl;
         }
     }
     
       if(100 > 99) { cout << "100 is greater than 99"; }
       if(99 > 100) { cout << "99 is greater than 100"; }
	   // First we find the absolute highest value in the entire testScore array in this loop:
       for (index = 0; index < listSize; index++)
	   {
         cout << "outer loop" << endl << endl;
   	     for (allTestScores = 0; allTestScores < 4; allTestScores++) 
  	     {
           cout << "inner loop" << endl;
           cout << maxXIndex << "." << maxYIndex << "." << endl << endl;
           if (list[maxXIndex].testScore[maxYIndex] < list[index].testScore[allTestScores])
             {
             cout << "inner loop:if" << endl;
             cout << "Row Location of old High Score: " << maxXIndex << endl;
             cout << "Column Location of old High Score: " << maxYIndex << endl;
             cout << "Old High Score: " << list[maxXIndex].testScore[maxYIndex];
             maxXIndex = index;  //Stores the Row Location of the last highest score found in the struct
             maxYIndex = allTestScores;  //Stores the Column Location of the last highest score found in the struct
             absoluteHighest = list[index].testScore[allTestScores]; // Stores the actual high score value
             cout << "New High Score: " << list[index].testScore[allTestScores] << "(" << absoluteHighest << ")" << endl;
             cout << "<------------------------------------------>" << endl;
             cout << endl << endl;
             count++;
             }
         }
         
       }
       
       for (index = 0; index < listSize; index++)
     {
       cout << list[index].studentFName << list[index].studentLName << ": ";
       for (allTestScores = 0; allTestScores < 4; allTestScores++)
         {
           cout << list[index].testScore[allTestScores] << " ";
         }
         cout << endl;
         if (index == 5)
         {
           cout << "<------------------------------------------>" << endl;
           cout << endl;
         }
     }
       
       cout << "total highs: " << count << endl;
       // Print Single Highest Test Score.
       cout << "Student with Highest Score: " << list[maxXIndex].studentFName << " " << list[maxXIndex].studentLName << " - " << absoluteHighest << endl;
       
       // Find every entry that is equal to the absolute highest max score and output the student's name and the test score.
       for (index = 0; index < 4; index++)
       {
         for (allTestScores = 0; allTestScores < listSize; allTestScores++)
         {
           // begin find highest grade by test
           if (absoluteHighest = list[allTestScores].testScore[index])
           {
             highByTest[allTestScores][index] = list[allTestScores].testScore[index];
             highByTest[allTestScores][index] - list[allTestScores].testScore[index];
           }
         }
       }
}

"students.txt" (name it whatever you want, the program prompts you for the file name):

seth wb 89 99 98 97
missy peterson 97 98 99 100
roger hampton 55 87 65 75
samilla achebe 88 66 76 100
shereth balasubramanian 100 100 99 99
maria montessori 100 100 100 100

Please help me understand what I am doing wrong here so I can fix it!

Thanks in advance,
Seth

Edited 5 Years Ago by sethwb: n/a

It completely skipped everything inside the if statement. I was expecting it to encounter the first high score (array position 0,1) output that, then output the next one which is at 1,3.

Oddly enough I recompiled a short time after posting this and it worked. I have no idea why because I did not change a thing.

It completely skipped everything inside the if statement. I was expecting it to encounter the first high score (array position 0,1) output that, then output the next one which is at 1,3.

Oddly enough I recompiled a short time after posting this and it worked. I have no idea why because I did not change a thing.

While I have you here though - do you know of a better way to find all the matching top scores than what I have done in the last for loop?

I updated it:

for (index = 0; index < 4; index++)
       {
         cout << "Highest test scores for test #" << index << ":" << endl;
         for (allTestScores = 0; allTestScores < listSize; allTestScores++)
         {
           // begin find highest grade by test
           if (absoluteHighest == list[allTestScores].testScore[index])
           {
             cout << list[allTestScores].studentFName << list[allTestScores].studentLName << ": " << list[allTestScores].testScore[index] << endl;
           }
         }
         cout << endl;
       }

Edited 5 Years Ago by sethwb: n/a

Hello sethwb

One thing I would recommend is to change the initialize function like this:

for (index = 0; index < listSize; index++)
     {
         indata >> list[index].studentFName;
         cout << "fname: " << list[index].studentFName << endl;
         indata >> list[index].studentLName;
         cout << "lname: " << list[index].studentLName << endl;
         for (allTestScores = 0; allTestScores < 4; allTestScores++)
         {
           int current_mark = 0;
           indata >> current_mark;
           list[index].testScore[allTestScores] = current_mark;
           // begin assigning letter grades
           if(0 <= current_mark && current_mark <= 50)
             list[index].grade[allTestScores] = 'F';
           else if (51 <= current_mark && current_mark <= 65)
             list[index].grade[allTestScores] = 'D';
           else if (66 <= current_mark && current_mark <= 75)
             list[index].grade[allTestScores] = 'C';
           else if (76 <= current_mark && current_mark <= 89)
             list[index].grade[allTestScores] = 'B';
           else
             list[index].grade[allTestScores] = 'A';
         
           cout << "test score: " << current_mark << " " << list[index].grade[allTestScores] << endl;
         }
         cout << endl;
         cout << "Student #:" << index << endl;
     }

basically created a new integer called current_mark. and replaced all list[index].testScore[allTestScores] with current_mark;

I did that because it is not good programming practice.

Right now, you have 6 students, and going through all the arrays looking for what to read and write won't take the computer long, but when you have more students, it will be much less efficient than this code. Plus it is cleaner code. :)

Edited 5 Years Ago by PathikRaval: n/a

This article has been dead for over six months. Start a new discussion instead.