Vectors!

Hi there! Long time listener, first time caller... j/k!

No really, I'm trying to convert the following file that using I/O to input data from a .txt file from using an array, to using a vector.

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <fstream>
using namespace std;

int main ( )
{
	// array of grades to be assigned
	// 91 - 100 is an A
	// 81 -  90 is a B
	// 71 -  80 is a C
	// 61 -  70 is a D
	// Below 61 is an E
	char grades[] = {'A', 'B', 'C', 'D', 'E'};

	// declare a place to hold scores read from a file 
	int scores[10]={0};
	
	// declare the stream object and open the file
	ifstream theDataFile("c:\\scores.txt");
	if (!theDataFile)
	{
		cout << "\nError opening file.";
		exit(1);
	}
	
	// read the scores into the array
	int index = 0;
	int aScore;
	while(!theDataFile.eof( ))
	{
		theDataFile >> aScore;
		if(!theDataFile.good( )) // the read failed ...
		{
			if (!theDataFile.eof( )) // and it was not an eof condition
			{
				cout << "\nError reading file.";
				exit(1);
			}
			break; // it was an eof, so break out of the loop
		}
		scores[index++] = aScore;
	}
	
	// print out the values just read and give each a grade
	for (int i = 0; i < index; i++)
	{
		cout << scores[i] ;
		if (scores[i] < 61)
			cout << "-" <<grades[4];  // grade is an 'E'
		else if (scores[i] < 71)
			cout << "-" << grades[3];  // grade is a 'D'
		else if (scores[i] < 81)
			cout << "-" << grades[2];  // grade is a 'C'
		else if (scores[i] < 91)
			cout << "-" << grades[1];  // grade is a 'B'
		else
			cout << "-" << grades[0];  // grade is an 'A'
		cout << endl;
	}

	system("PAUSE");
	return 0;
}



What I have so far in the new file that I want to use a vector with is:

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main ( )
{
	// array of grades to be assigned
	// 91 - 100 is an A
	// 81 -  90 is a B
	// 71 -  80 is a C
	// 61 -  70 is a D
	// Below 61 is an E
	char grades[] = {'A', 'B', 'C', 'D', 'E'};

	// declare a place to hold scores read from a file 
	vector <int> scores;
	
	// declare the stream object and open the file
	ifstream theDataFile("c:\\scores.txt");
	if (!theDataFile)
	{
		cout << "\nError opening file.";
		exit(1);
	}
	
	// read the scores into the array
	int index = 0;
	int aScore;
	while(!theDataFile.eof( ))
	{
		theDataFile >> aScore;
		if(!theDataFile.good( )) // the read failed ...
		{
			if (!theDataFile.eof( )) // and it was not an eof condition
			{
				cout << "\nError reading file.";
				exit(1);
			}
			break; // it was an eof, so break out of the loop
		}
		scores.push_back(aScore);
	}
	
	// print out the values just read and give each a grade
	for (int i = 0; i < index; i++)
	{
		cout << scores[i] ;
		if (scores[i] < 61)
			cout << "-" <<grades[4];  // grade is an 'E'
		else if (scores[i] < 71)
			cout << "-" << grades[3];  // grade is a 'D'
		else if (scores[i] < 81)
			cout << "-" << grades[2];  // grade is a 'C'
		else if (scores[i] < 91)
			cout << "-" << grades[1];  // grade is a 'B'
		else
			cout << "-" << grades[0];  // grade is an 'A'
		cout << endl;
	}
	
	system("PAUSE");
	return 0;
}


I think I'm on the right track, but I don't know what the next step is... Any help would be appreciated.
Yes, you are indeed on the right track! There is only one small thing that's actually missing, in the 'while' loop.

HINT: look at the 'index'

:)
I don't see anything in the while loop that would throw me off... Do you mean the for loop?
From what I understand of your program, I'm guessing that you would want to keep track of the number of scores in the vector.

So that would be where your 'index' variable would come into play...

I'll leave that for you to figure out :)
Wow... Maybe it was just too late to be working on this lol.

index++;

:P

Thanks!
It might be a good idea to close the ifsteam when you're done with it, too.
Topic archived. No new replies allowed.