My array program is spitting out symbols!?!?

Hey all,

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;

void loadArray(string nameOfFile,int &count,string names[ ], char grades[ ]);
void displayArray(string names[ ], char grades[ ], int size);
void selectionSort(string list[ ], char grades[ ], int listSize);
double gradeScale(double gradeIn,char grades[]);


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

cout << endl << endl;
cout << " STUDENT GRADE " << endl;
displayArray(names,grades,howMany); //display names and grade arrays
cout << endl << endl;
system("pause");
return 0;
}

//LOADARRAY---------------------------------------------------------------------
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;
getline(inData, stringIn);
for (int i=0; i < count; i++)
{
names[count] = stringIn;
getline(inData, stringIn);

}
inData.close();
return;
}

//DISPLAY ARRAY-----------------------------------------------------------------
void displayArray(string names[ ],char grades[ ], int size)
{
cout << endl << endl;
for(int index = 0; index < size ; index++)
cout << names[index] << endl;
for(int index = 0; index < size ; index++)
cout << grades[index] << endl;
return;
}

//SORT--------------------------------------------------------------------------
void selectionSort(string list[ ], char grades[ ], int listSize)
{
for( int i = 0; i < listSize ; i++)
{
string currentMin = list[i];
int currentMinIndex = i;
for( int j = i; j < listSize; j++)
{
if( currentMin > list[j])
{
currentMin = list[j];
currentMinIndex = j;
}
}//end of for j
list[currentMinIndex] = list[i];
list[i] = currentMin;
}
return;
}

//CONVERT GRADE TO LETTER GRADE-------------------------------------------------
double gradeScale(double gradeIn,char grades[ ])
{
int i;

if (gradeIn < 60)
grades[i]= 'F';
else if (gradeIn < 69.9)
grades[i]= 'D';
else if (gradeIn < 79.9)
grades[i]= 'C';
else if (gradeIn < 89.9)
grades[i]= 'B';
else if (gradeIn < 100)
grades[i]= 'A';

return grades[i];
}
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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;
}
Last edited on
Also you are only reading into one array, you need to read into the grades[] array too.
K. Thank you both for the input. I'm going to make these changes and see where it gets me. Again, thanks!
Topic archived. No new replies allowed.