What's wrong with my code?

Question:


I honestly don’t know why my code isn’t outputting anything to either my console or my output file. Could my code be stuck in a while loop?

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

using namespace std;

// Define the salesPersonRec structure before the initialize function
struct salesPersonRec {
  int ID;
  int Quarter[4];
  int totalSale;
};

// Declare the initialize function after the salesPersonRec structure
void initialize(ifstream &indata, salesPersonRec list[], int listSize);

void getData(ifstream &infile, salesPersonRec list[], int listSize);

void saleByQuarter(salesPersonRec list[], int listSize, double totalByQuarter[]);

void totalSaleByPerson(salesPersonRec list[], int  listSize);

void printReport(ofstream &outfile, salesPersonRec list[], int listSize, double saleByQuarter);

int main() {
  ifstream inputID;
  ifstream input;
  ofstream output;  

  inputID.open("salesManID.txt");
  input.open("salesData.txt");
  output.open("annualReport.txt");
  int counter = 0;
  double allSalesByQuarter = 0;

  // Count the number of employees
  while (!inputID.eof()) {
    counter++;
  }

  salesPersonRec employees[counter];

  // Assign each employee in the struct array their ID's
  initialize(inputID, employees, counter);
  getData(input, employees, counter);

  printReport(output, employees, counter , allSalesByQuarter);

  input.close();
  inputID.close();
  output.close();

  return 0;
}

void initialize(ifstream &indata, salesPersonRec list[], int listSize) {
  int i = 0;
  while (indata >> list[i].ID) {
    i++;
  }
}


void getData(ifstream &infile, salesPersonRec list[], int listSize){
  int ID = 0;
  int month = 0;
  int sales = 0;

  while(!infile.eof()){
    cout << "I'm stuck here." << endl;
  infile >> ID >> month >> sales;
  
  for(int i = 0; i < listSize; i++){
      if(list[i].ID == ID){
        switch(month){
          case 1:
          case 2:
          case 3:
            list[i].Quarter[0] = sales;
            break;
          case 4:
          case 5:
          case 6:
            list[i].Quarter[1] = sales;
            break;
          case 7:
          case 8:
          case 9:
            list[i].Quarter[2] = sales;
            break;
          case 10:
          case 11:
          case 12:
            list[i].Quarter[3] = sales;
            break;
          default:
          cout << "Invalid month";
        }
      }
    }
        
    
    }
    
}  

void saleByQuarter(salesPersonRec list[], int listSize, double totalByQuarter[]){

  //nested loop to add all the sales for the specific quarter
  for( int i = 0; i < listSize; i++){
    for( int j = 0; j < 4; j++){
      totalByQuarter[j] += list[i].Quarter[j];
    }
  }
}

void totalSaleByPerson(salesPersonRec list[], int listSize){

  for( int i = 0; i < listSize; i++){
    for(int j = 0; j < 4; j++){
      list[i].totalSale += list[j].Quarter[j]; 
    }
    
  }
}

void printReport(ofstream &outfile, salesPersonRec list[], int listSize, double saleByQuarter){

    outfile << left<< setw(20) << "ID" << setw(20) << "QT1"<< setw(20) << "QT2" << setw(20) << "QT3" << setw(20) << "QT4" << setw(20) << "TOTAL" << endl;

    // Loop through the list array and print out the sales data for each employee
    for (int i = 0; i < listSize; i++) {
        outfile << left << setw(20) << list[i].ID;
        for (int j = 0; j < 4; j++) {
            outfile << setw(20) << list[i].Quarter[j];
        }
        outfile << setw(20) << list[i].totalSale << endl;
    }
}

Maybe try using ifstream.good() instead of ! ifstream.eof()

No changes were noticed after doing that. :frowning_face:

(post deleted by author)