I've been going back and forth to my books for array help; but I need something more to figure this out. While probably simple errors to you guys, arrays are not my strong suit.
This program is intended to read in from a user spec'd file- then read in the names one at a time, followed by the student's grade(dbl). A function will then calculate that double grade into a char value. Then output the names in vertical array and the grades in another vertical array to the right of the names array. anyways- upon running it I am getting crazy symbols and what not. Any help would be greatly appreciated.
Here is the input file:
16
Smith, Jenny
95
Jones, Tommy
55
Johnson, James J.
86
Lamb, Danny
83
George, Constantine
79
Clark, Sammy
60
Lewis, Charles
100
Hudson, Lester
89
Hampton, Lenny
40
Iacoca, Jimmie
93
Warren, Gina
85
Tansil, Tara
83
Gullett, Nelson P.
100
Jones, Jonathan
77
Allen, Josephine
90
Smith, Allen P.
81
here is my code:
//Comments go here
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main ( )
{
string names[100];
char grades[100];
string filename;
int howMany;
cout << "Please enter the name of your file: "; //promt user to enter filename
getline(cin,filename); //read in file name
cout << endl;
cout << "Processing: " << filename << endl;
loadArray(filename,howMany,names,grades); //call load array function
I noticed a couple of errors in your load function. After you read in the number of entries using >> the end of line marker is not read so the next getline() simply reads the end of line marker rather than the following line as you would want.
Also you are indexing your array using count which is the size of the array. I think you meant to use i:
void loadArray(string nameOfFile, int &count, string names[], char grades[])
{
ifstream inData;
string stringIn;
inData.open(nameOfFile.data());
if(!inData)
{
cout << "Could not open " << nameOfFile << endl;
cout << "Terminating program." << endl;
system("pause");
exit(1);
}
count = 0;
inData >> count; // THIS will NOT extract the end of line character!
inData.ignore(); // so we need to ignore it.
getline(inData, stringIn);
for(int i = 0; i < count; i++)
{
// names[count] = stringIn; // should not be indexing on count
names[i] = stringIn; // index using i
getline(inData, stringIn);
}
inData.close();
return;
}