Using Vectors!

I am having some trouble. I am supposed to do the following:

1. Create a Vector to hold a set of exam scores.

2. Read exam scores into a vector from Scores.txt

3. Display scores in rows of five(5) scores.

4. Calculate average score and display.

5. Find the median score and display.

But it looks like my main problem is number 2 reading exam scores from the file into the vector. Any help, tips or suggestions would be great!

Thanks!



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//  main.cpp
//  Program 7
//  Created by William Blake Harp on 7/14/14.

#include <iostream>
#include <vector>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <algorithm>


using namespace std;

double average;
double median;
int sum_of_exam_scores;

vector<int> exam_scores;
int const SIZE = sizeof(exam_scores)/sizeof(exam_scores[0]);

int main()
{
    ifstream inputFile;
    
    string scores = "scores.txt";
    
    size_t size = scores.size();
    
    // Puting the scores from the txt file to the vector.
    inputFile.open("scores.c_str()");
    if (inputFile)
    {
        int count = 0;
    
    
        while ( inputFile >> scores )
        {
            scores.push_back(count);
            ++count;
        }
    }
    
    // displaying all the scores 5 in a row.
    for (unsigned i=0; i != scores.size(); ++i)
    {
        if ((i % 5)== 0)
        {
            cout << endl;
        }
        cout << scores.at(i) << " ";
    }
    cout << endl;
    
    
    // Calculate average score and display.
    for (int count = 0; count != SIZE; count++)
    {
        sum_of_exam_scores += exam_scores[count];
    }
    average = sum_of_exam_scores / SIZE;
    
    cout << setprecision(2) << fixed;
    cout << "The average of the exam scores are: " << average << endl;
    
    
    // Find the median.
    sort(scores.begin() , scores.end());
    if (size  % 2 == 0)
    {
        median = (scores[size / 2 - 1] + scores[size / 2]) / 2;
        cout << "The median is: " << median ;
    }
    else
    {
        median = scores[size / 2];
        cout << "The median is: " << median ;
    }
    
    
    
    
    
    inputFile.close();
    
    
    return 0;
}// End Code!


  
You seem to be confusing scores (a string) and exam_scores (a vector).

Line 20: This construct works for arrays, but does not work for vectors. The storage for a vector is allocated indirectly. SIZE will always be 1.

Line 26: Should be declared as const There is no reason you should be modifying this variable.

Line 28: You get the length of the file name for no apparent reason.

Line 37: You're trying to read a value from the file into the string that holds the filename. Since exam_score is a vector of ints, you really want to read into an int. It's also generally not a good idea to use the same variable for multiple purposes.

Line 39: You can't do a push_back on a string. You should be using exam_scores here. Also, why are you pushing count? Don't you want to push the int score you read from the file?

Line 45: You using the size of the filename string, not the size of exam_scores.

Line 57,61: Use of SIZE here is incorrect.



ok... i am still way off it seems. Do I need to add another int to make this work better. Or what do you think. Any help would be great! Thank you so much!

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//  main.cpp
//  Program 7
//  Created by William Blake Harp on 7/14/14.

#include <iostream>
#include <vector>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <algorithm>


using namespace std;

double average;
double median;
int sum_of_exam_scores;

vector<int> exam_scores;


int main()
{
    ifstream inputFile;
    
    const string scores = "scores.txt";
    
    
    
    // Puting the scores from the txt file to the vector.
    inputFile.open("scores.c_str()");
    if (inputFile)
    {
        int count = 0;
    
    
        while (inputFile > exam_scores)
        {
            exam_scores.push_back(scores);
            ++scores;
        }
    }
    
    // displaying all the scores 5 in a row.
    for (unsigned i=0; i != exam_scores.size(); ++i)
    {
        if ((i % 5)== 0)
        {
            cout << endl;
        }
        cout << exam_scores.at(i) << " ";
    }
    cout << endl;
    
    
    // Calculate average score and display.
    for (int count = 0; count != exam_scores; count++)
    {
        sum_of_exam_scores += exam_scores[count];
    }
    average = sum_of_exam_scores / exam_scores;
    
    cout << setprecision(2) << fixed;
    cout << "The average of the exam scores are: " << average << endl;
    
    
    // Find the median.
    sort(scores.begin() , scores.end());
    if (size  % 2 == 0)
    {
        median = (scores[size / 2 - 1] + scores[size / 2]) / 2;
        cout << "The median is: " << median ;
    }
    else
    {
        median = scores[size / 2];
        cout << "The median is: " << median ;
    }
    
    
    inputFile.close();
    
    return 0;
}// End Code!
Last edited on
Why are arbitrary variables global while others are not?

1
2
3
4
5
        while (inputFile > exam_scores)
        {
            exam_scores.push_back(scores);
            ++scores;
        }
scores is a string that is the file name and you want to read in as an int. You also seem to be using scores for everything when it is only needed for one thing (opening the input file)

It should be something like

1
2
3
4
5
6
int temp = 0;
while(inputFile >> temp)
{
    exam_scores.push_back(temp);
    ++count;
}
ok thanks wow that makes seance! But now the rest of my program is not working. it is telling me that I have invalid operands to a binary expression...

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
// displaying all the scores 5 in a row.
    for (unsigned i=0; i != exam_scores.size(); ++i)
    {
        if ((i % 5)== 0)
        {
            cout << endl;
        }
        cout << exam_scores.at(i) << " ";
    }
    cout << endl;

 // Calculate average score and display.
    for (int count = 0; count != exam_scores; count++)
    {
        sum_of_exam_scores += exam_scores[count];
    }
    average = sum_of_exam_scores / exam_scores;
    
    cout << setprecision(2) << fixed;
    cout << "The average of the exam scores are: " << average << endl;
    
    
    // Find the median.
    sort(scores.begin() , scores.end());
    if (exam_scores  % 2 == 0)
    {
        median = (scores[size / 2 - 1] + scores[exam_scores / 2]) / 2;
        cout << "The median is: " << median ;
    }
    else
    {
        median = scores[exam_scores / 2];
        cout << "The median is: " << median ;
    }
    
    
    inputFile.close();
    
    return 0;
}// End Code!
Last edited on
I had an idea why it was not working. I think it was because my vector was not in the right place. Am I on the right track with this set of code? But when I run it I get nothing!

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
//  main.cpp
//  Program 7
//  Created by William Blake Harp on 7/14/14.

#include <iostream>
#include <vector>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <algorithm>


using namespace std;

double average;
double median;
int sum_of_exam_scores;
int temp = 0;
int count = 0;

int main()
{
    ifstream inputFile;
    
    vector<int> exam_scores;

    const string scores = "scores.txt";
    
    // Puting the scores from the txt file to the vector.
    inputFile.open("scores.c_str()");
    if (inputFile)
    {
        int count = 0;
    
    
        while (inputFile >> temp)
        {
            exam_scores.push_back(temp);
            ++count;
        }
    }
    
    // displaying all the scores 5 in a row.
    for (unsigned i=0; i != exam_scores.size(); ++i)
    {
        if ((i % 5)== 0)
        {
            cout << endl;
        }
        cout << exam_scores.at(i) << " ";
    }
    cout << endl;
    
    
    
        
    
    
    inputFile.close();
    
    return 0;
}// End Code! 
Last edited on
The variable count just adds clutter.
the data can be read from the file like this:
1
2
3
4
5
    inputFile.open("scores.c_str()"); 
    int temp;
    
    while (inputFile >> temp)
        exam_scores.push_back(temp);
I did that but still get nothing

1
2
3
4
5
6
7
8
9
10
11
// Puting the scores from the txt file to the vector.
    inputFile.open("scores.c_str()");
    int temp;
    if (inputFile)
    {
        while (inputFile >> temp)
        {
            exam_scores.push_back(temp);
        }
    }


or this

1
2
3
4
5
6
7
// Puting the scores from the txt file to the vector.
    inputFile.open("scores.c_str()");
    int temp;
    while (inputFile >> temp)
        {
            exam_scores.push_back(temp);
        }
Last edited on
1
2
3
ifstream inputFile;
const string scores = "scores.txt";
inputFile.open("scores.c_str()");

can be shortened to
ifstream inputFile ( "scores.txt" );
ok... @Yay295 what can I do to get my program to
2. Read exam scores into a vector from Scores.txt

3. Display scores in rows of five(5) scores.
Is your file named 'scores.txt' or 'Scores.txt'?
scores.txt
I'd suggest starting simple, put in some messages to confirm progress (or lack of it) and build from there.
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
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

int main()
{
    string filename = "scores.txt";
    ifstream inputFile(filename.c_str());
    if (!inputFile)
    {
        cout << "could not open input: " << filename << endl;
        return 1;
    }
        
    vector<int> exam_scores;
    
    int temp;    
    while (inputFile >> temp)
        exam_scores.push_back(temp);

    cout << "number of scores read was: " << exam_scores.size() << endl;
    
    return 0;
}

I did that but why does it give me

""could not open input: scores.txt"

do I need to do something with the code before I can go of of it?
The file is either not in the same directory as the project or it is not named "scores.txt."

By the way in the c++11 standard there is an overload that takes a string so you don't necessarily need the c-string function.
Last edited on
ok thanks I got that part to work but now what do i put in line 28 to make these numbers in the vec to show up in rows of 5?

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
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

int main()
{
    string filename = "scores.txt";
    ifstream inputFile(filename.c_str());
    if (!inputFile)
    {
        cout << "could not open input: " << filename << endl;
        return 1;
    }
    
    vector<int> exam_scores;
    
    int temp;
    while (inputFile >> temp)
        exam_scores.push_back(temp);
    
    for (int i = 0; i < temp; temp++)
    {
        if ((temp % 5) == 0)
            cout << endl;
        
        cout <<  << ", ";

    }
    
    
    return 0;
}//End Code. 
ugh that for loop :P It should be for(int i = 0; i < exam_scores.size(); ++i) I would also change line 26 to cout << endl << exam_scores.at(i); //.at(i) or [i] then line 28 to
1
2
 else
    cout << ", " << exam_scores.at(i); //.at(i) or [i] 
I think you need to read up on for loops and vectors so you get a grasp on how to use them before you actually use them.

Please read: http://www.cplusplus.com/doc/tutorial/control/ and http://www.cplusplus.com/reference/vector/vector/ (or read up on classes instead of vector)
Topic archived. No new replies allowed.