Arrays

I need some help...
I wrote a program that reads an input file containing names and numbers.
I mapped the nunbers to letter grades and wrote the output to another file as well as the screen.

What I have to do now is use arrays to keep track of names, numeric grades, and letter grades.

This is what i have so far.

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
30
31
32
33
34
35
36
37
38
void inputFile()
{
	ifstream iFile;
	ofstream oFile;
	string lines;
	int num;
	char grade;
	iFile.open("inputFile.txt");
	if (!"inputFile.txt")
	{
		cout << "ERROR: Could not open file." << endl;
	}
	oFile.open("outputFile.txt");
	if (!oFile)
	{
		cout << "ERROR: Could not open file." << endl;
	}
	while (!iFile.eof())
	{
		iFile >> lines >> num;
		// cout << lines << "	" << num << endl;
		if (num > 90){
			grade = 'A';
		}else if (num > 80){
			grade = 'B';
		}else if (num > 70){
			grade = 'C';
		}else if (num > 60){
			grade = 'D';
		}else{
			grade = 'F';
		}
		oFile << lines << "	" << num << "	" << grade << endl;
		cout << lines << "	" << num <<	"	" << grade << endl;
	}
	iFile.close();
	oFile.close();
}


Now, this program runs fine..

I figured the arrays should be

1
2
3
string names[6]; // there are 6 names
int numScores[6]; // there are 6 "numerical scores"
char letterGRade[6]; //there should be 6 letter grades. 


what's inside inputFile.txt:
Joe	93
Mary	96
Travis	82
Seif	61
Omar	75
Louis	56


whats inside outputFile.txt:
Joe	93	A
Mary	96	A
Travis	82	B
Seif	61	D
Omar	75	C
Louis	56	F


So the question I have is: Where do i implement these and couldn't i just put everything in one giant for loop and use #define to my advantage if more names were to be added?
Last edited on
What I have to do now is use arrays to keep track of names, numeric grades, and letter grades.
If you're not sure about the size of your array, use vector.
couldn't i just put everything in one giant for loop and use #define to my advantage
You already put everything in one giant while loop, so I don't see your problem. You should avoid using #define, also it is only for the compiler and cannot be used in runtime.
Now, this program runs fine..
Really? I don't see how !"inputFile.txt" could work. "inputFile.txt" is a string and not a file. You need iFile here.
Well, there's one mistake in your iFile.Open failure test. I think you mean if(!iFile).

And if you don't know a priori how many students there are, you'll need to allocate those arrays dynamically. Open iFile and simply run through it counting every other 'word' (since there's a name followed by a grade). That'll give you the number of students. Then rewind(iFile) to get it back to the start and, after allocating the different arrays (where x= #of students)
1
2
3
string* names = new string[x];
int* numScores = new numScores[x];
char* letterGrade = new letterGrade[x];

Just go ahead as you were before.

Just then remember to delete[] the allocated space.
hamsterman (198) Jan 14, 2010 at 8:02am
You already put everything in one giant while loop, so I don't see your problem. You should avoid using #define, also it is only for the compiler and cannot be used in runtime.
hamsterman (198) Jan 14, 2010 at 8:02am
Really? I don't see how !"inputFile.txt" could work. "inputFile.txt" is a string and not a file. You need iFile here.


iFile.open is openning inputFile.txt
that line is simply saying, "If the file named "inputFile.txt" is not opened, then cout 'ERROR: could not open file' and then it will stop"


as far as the array sizes, They are all 6. I am basing them off of the files: i.e 6 names, 6 scores, 6 letter grades.

I just need to find out how i would implement the arrays to modify the program, i figured i need three functions:

1] initialize the name and numGrade arrays by reading inputFile.txt. alsom initialize the letterGrade to blank.

2] compute the letter grades and store them into letterGrade array.

3] Display the names, numGrade, and letterGrade

I'm just not quite sure if i can go about this without the three functions.

wasabi (46) Jan 14, 2010 at 8:04am

1
2
3
string* names = new string[x];
int* numScores = new numScores[x];
char* letterGrade = new letterGrade[x];



I completely forgot the const *

Thank you!
Last edited on
I see what you two were talking about with the !iFile.

I'm pretty sure I had a brain fart :/
Last edited on
Well, you're still wrong regarding the fopen-failure test. !"Whatever" will never be true. Think about it.

Hell, you did it right for oFile. Just go and see what's different about them. It shouldn't take more than a moment to see it.

And the OP says "if more names were to be added." That means you don't know the true number of students in the list, since it is variable. In which case you cannot initialize them as
1
2
3
string names[6];
int numScores[6];
char letterGrade[6];

Since if one more student is added to that six-student list, you're gonna need more space. You need to use dynamic allocation.
And the OP says "if more names were to be added." That means you don't know the true number of students in the list, since it is variable. In which case you cannot initialize them as

1
2
3
string names[6];
int numScores[6];
char letterGrade[6];


Since if one more student is added to that six-student list, you're gonna need more space. You need to use dynamic allocation.


Got it, i'll post once again with either the finished code or another question :/
once again, thank you.
1>...utils.cpp(5) : error C2062: type 'void' unexpected

I'm getting this error, for the function:
1
2
3
4
void inputFile()
{
	...
}


When I click on the error, it highlights the v in void inputFile()

any ideas? I tried using int, and it still didn't work.
I'm guessing you are including a header file that forgot a ; somewhere.
firedraco (2247) Jan 14, 2010 at 8:56am
I'm guessing you are including a header file that forgot a ; somewhere.


Oh my lord, thank you!
I don't know what's up with me today. I must be tired.

BACK TO WORK!! :)
Alright i got it down. I did it a little different though.

Thank you for your guys' help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "utils.h"

const int SIZE = 6;

void main()
{

string names[SIZE];
int scores[SIZE];
char grades[SIZE];

void initData(names, scores);
void calcData(scores, grades);
void printData(names, scores, grades);

}


i probably messed up a bit, because i don't have my code with me (It's in my car and i'm lazy)
I'm sure i missed a [] or another minor thing.

But it works and thank you guys ^-^
Last edited on
dont text(or write code for that matter) while driving!!! >:(

:)
Last edited on
Topic archived. No new replies allowed.