Array crashing program

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  void buildSalesArray(double salesArray[])
 {
 int Month = 0;
 double Amount = 0;
 ifstream ifs ("sales7.txt");

  char c = ifs.get();
   if (ifs.fail() )
   {
   cout << "The sales7.txt input file did not open";
   exit(-1);
   }
  while (!ifs.eof()) 
  {
  ifs >> Month >> Amount;
  cout << "Month: " << Month << "Amount: " << Amount << endl;
  salesArray[Month] += Amount; // i'm having trouble here
  cout << "Array: " << salesArray[Month] << endl;
  }
  
  ifs.close();
 }

My program is crashing at a certain point and any suggests why would be greatly appreciated!
What is the size of the array? The program crashing could be it exceeding the bounds of the array.
this is my main:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;
const int NUM_MONTHS = 12;
const string MONTH_NAMES[NUM_MONTHS+1] = { "", "January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December" };
void buildSalesArray(double salesArray[]);
void printSalesArray(double salesArray[]);
double findSum(double salesArray[]);
double findMean(double salesArray[]);
int findHighMonth(double salesArray[]);
int findLowMonth(double salesArray[]);
int main()
{


double salesArray[NUM_MONTHS+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

buildSalesArray(salesArray);
printSalesArray(salesArray);
findSum(salesArray);
findMean(salesArray);
findHighMonth(salesArray);
findLowMonth(salesArray);
return 0;
}
bump
1
2
  char c = ifs.get();
   if (ifs.fail() )
You shouldn't check like this to see if the file opened or not. Try: if(!ifs.is_open())

salesArray[Month] += Amount; // i'm having trouble here it would help if we could see the text file you are trying to read.
Topic archived. No new replies allowed.