// 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>
usingnamespace std;
double average;
double median;
int sum_of_exam_scores;
vector<int> exam_scores;
intconst 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.
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!
// 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>
usingnamespace 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!
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)
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...
// 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!
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!
// 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>
usingnamespace 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!
// 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);
}
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.