Since the number of lines and the number of values on each line will change (the program must work with several different data files), I'm not sure how to read in the values to an array. The array will be two dimensional, with the first line of the file being the first line of the array and so on. Any help will be much appreciated.
What do these values represent? Do different lines represent different students and different values in a line grades of that particular student? A two dimensional array is not the best container you could choose to store that kind of data. Are you allowed to use structs? Vectors (or dynamic arrays)?
The values represent sales numbers, with each line being a different department. My professor specifically requires a two-dimensional array, because that's what we've been learning about and other containers are not covered in the intro course. I think I've figured out how I'm going to read the values in to my array, but I'm encountering another problem. For some reason, my loop to read the values in to the array seems not to be running (the code compiles with no errors). Here's what I've got so far :
ifstream infile;
char filename[30];
int numDepartments=-1;
string line;
/*Counters for the loop that
reads values in to the array */
int count;
int countDepartment=0;
/*Ask the user for a file name
and check that the file opens */
cout << "Please enter the name of the data file: ";
cin >> filename;
infile.open(filename);
if(!infile)
{ cout << "Error opening file. \n";
cout << "Terminating Program \n";
exit(1);
}
/* count the number of lines in the data file */
while(!infile.eof())
{ getline(infile,line);
numDepartments++;
}
/*reset the pointer to the beginning of the file */
infile.seekg(0, ios::beg);
/*Array to store sales */
float sales [numDepartments] [12];
/*read in sales numbers from file */
while(!infile.eof())
{if (count<11)
{infile >> sales [countDepartment][count];
count++;
cout << count;}
else
{count=0;
countDepartment++;}
}
}
Try putting a infile.clear() call before attempting to get your values. It resets some flags (including the eof flag) and allows you to use the infile object for input again. There's something else I want to ask. Does the number of values per line remain the same throughout a single file? It doesn't look like it does in your first example, but you seem to assume it's 12 in your code.
I was initially confused; there will always be 12 values per line because each value represents the sales for one month - that's why I'm assuming 12. The infile.clear() worked perfectly.. now on to another issue. I've got the loop to read the values in to the array, but it stops after the first line for some reason :
1 2 3 4 5 6 7 8 9 10 11 12
/*read in sales numbers from file */
while(!infile.eof())
/* this loop should read 12 values in to the array */
{ while (count<12)
{infile >> sales [countDepartment][count];
count++;
cout << count;}
/* after the loop, the counter is reset so that the loop will start over on the next run of the outer loop */
count=0;
countDepartment++;}
}
The comments describe how I want it to work - it should get 12 values from the first line, then move on to the next line for 12 more values. The countDepartment variable is incremented to track which line number is currently being processed, and sets the first value of the array. When I run the program, however, only the first line of the file is processed and the program doesn't move on to the second line..
/* Project 6 - James Bagenstose */
#include <iomanip>
#include <iostream>
#include <fstream>
usingnamespace std;
float getSales();
float averageSales();
void compareSales();
void aboveStandard();
void output();
int main ()
{
getSales();
return 0;
}
float getSales()
{ifstream infile;
char filename[30];
int numDepartments=-1;
string line;
/*Counters for the loop that
reads values in to the array */
int count=0;
int countDepartment=0;
/*Ask the user for a file name
and check that the file opens */
cout << "Please enter the name of the data file: ";
cin >> filename;
infile.open(filename);
if(!infile)
{ cout << "Error opening file. \n";
cout << "Terminating Program \n";
exit(1);
}
/* count the number of lines in the data file */
while(!infile.eof())
{ getline(infile,line);
numDepartments++;
}
/*reset the pointer to the beginning of the file */
infile.clear();
/*Array to store sales */
float sales [numDepartments] [12];
/*read in sales numbers from file */
while(!infile.eof())
{ while (count<12)
{infile >> sales [countDepartment][count];
count++;
cout << count;}
count=0;
countDepartment++;}
}
When I run the program, the loop that uses the 'count variable runs 12 times, but after those 12 runs, it should move on to the next line and run another 12 times, until it reaches the end of the file. It appears that it is only running the initial 12 times, then stopping.
/* Project 6 - James Bagenstose */
#include <iomanip>
#include <iostream>
#include <fstream>
usingnamespace std;
float getSales();
float averageSales();
void compareSales();
void aboveStandard();
void output();
int main ()
{
getSales();
return 0;
}
float getSales()
{ifstream infile;
char filename[30];
int numDepartments=-1;
string line;
/*Counters for the loop that
reads values in to the array */
int count=0;
int countDepartment=1;
/*Ask the user for a file name
reads values in to the array */
int count=0;
int countDepartment=1;
/*Ask the user for a file name
and check that the file opens */
cout << "Please enter the name of the data file: ";
cin >> filename;
infile.open(filename);
if(!infile)
{ cout << "Error opening file. \n";
cout << "Terminating Program \n";
exit(1);
}
/* count the number of lines in the data file */
while(!infile.eof())
{ getline(infile,line);
numDepartments++;
}
/*reset the pointer to the beginning of the file */
infile.clear();
/*Array to store sales */
float sales [numDepartments] [12];
/*read in sales numbers from file */
for(numDepartments;numDepartments>0;numDepartments--)
{ for(count;count<13;count++)
{infile >> sales [1][count];
cout << count;
if (count==12) break;}
cout << "\n";
count=0;
countDepartment++;}
cout << sales [1] [1];
}
figured out the nested loops issue; changed them both to for loops and everything's good; the updated code is posted above.
clear() doesn't move the file pointer to the beginning of the file, it clears any status bits. to move to the beginning of the file, you could use seekg(ios::beg)
this is the contents of my data file. Using the code above and this data file, I get "NaN" when I try to cout a value from the array (sales [1] [1]) - I've tried changing several things and this error persists; does anyone know what could be causing this?
1. Lines 37-38 is a repeat of Lines 31-32 (won't compile)
2. Line 60 is a little dangerous. This kind of construct is known as a VLA - variable length array or figuring out the size of your automatic array variable at runtime. Before C99, this wasn't even legal. Afterwards, some C++ compilers support it (eg gcc) while others don't (eg Visual Studio).
3. Rewrite and reformat your code to comply with the standard way of doing for loops:
1 2 3
int count, limit=12;
for ( count=0; count<limit; ++count ) {
}
The way you have it coded with break; and variables initialized all over the place obfuscates your code and makes it harder to debug.
Follow these suggestions, retry, and then repost your new code with questions.