I am writing a section of code to display the median score, I am receiving a warning that reads. warning C4715: 'findMedian' : not all control paths return a value. And the output for the median is really messed up. I have been at this for a while now and any help would be great. Thanks!
#include <iostream>
#include <vector>
#include <cstdio>
#include <iomanip>
#include <algorithm>
usingnamespace std;
//Declaring Constants
constint ZERO = 0;
constint ONE_HUNDRED = 100;
double median = 0.00;
double totalPoints;
vector<int> scores;
int size;
double findMedian(vector<int> &scores)
{
size_t mid = scores.size() / 2;
if (scores.size() == 0) return 0.0; // change this to whatever it should be
// Make scores[mid] the mid'th element
nth_element(scores.begin(), scores.begin() + mid, scores.end());
if (size % 2 == 0)
{
// size is even. Need to average of scores[mid] and scores[mid-1]
nth_element(scores.begin(), scores.begin() + mid - 1, scores.end());
median = (scores[mid] + scores[mid - 1]) / 2;
}
else
{
median = scores[mid];
}
}
int main()
{
int firstExam, secondExam, thirdExam;
int error;
cout << "Dr. DoLittle's Grading Program ..... (by Kirk Kelley)" << endl;
do
{
error = 0;
cout << "Please enter in the score for the first exam: ";
cin >> firstExam;
if (firstExam < 0 || firstExam > 100)
{
cout << "Please enter a valid score between 0 and 100" << endl;
cin >> firstExam;
cin.clear();
}
} while (error);
do
{
error = 0;
cout << "Please enter in the score for the second exam: ";
cin >> secondExam;
if (secondExam < 0 || secondExam > 100)
{
cout << "Please enter a valid score between 0 and 100" << endl;
cin >> secondExam;
cin.clear();
}
} while (error);
do
{
error = 0;
cout << "Please enter in the score for the third exam: ";
cin >> thirdExam;
if (thirdExam < 0 || thirdExam > 100)
{
cout << "Please enter a valid score between 0 and 100" << endl;
cin >> thirdExam;
cin.clear();
}
} while (error);
//declaring variable
int homework;
//initiating loop
while (!cin.eof())
{
homework = EOF;
cout << "\nEnter score for homework assignment (press Ctrl+Z to quit): ";
cin >> homework;
if (!cin.good())
if (homework == EOF)
break;
else
{
cout << "\nInvalid Input. Entry must be an integer." << endl;
cin.clear();
cin.ignore(std::numeric_limits<streamsize> ::max(), '\n');
}
else
{
if (homework >= ZERO && homework <= ONE_HUNDRED)
{
scores.push_back(homework);
}
else
{
cout << "\nInvalid Input. Grade must be between 0-100." << endl;
cin.clear();
cin.ignore(std::numeric_limits<streamsize> ::max(), '\n');
}
}
}
median = findMedian(scores);
totalPoints = median + firstExam + secondExam + thirdExam;
cout << "The total points earned was " << totalPoints << endl;
cout << "The median homework score was " << median << endl;
cout << "The letter calculated letter grade is ";
system("Pause");
return 0;
}
warning C4715: 'findMedian' : not all control paths return a value.
You only return a value from the median function at line 12. In the if/else structure over lines 25 to 34, you calculate a median, but don't return it back to the function call at line 117.
That makes so much sense! thanks for you help. I cannot get it to make a correct median though! I can get it to work with the even numbers but when it is an odd number it still calculates and average. here is my code thus far.
#include <iostream>
#include <vector>
#include <cstdio>
#include <iomanip>
#include <algorithm>
usingnamespace std;
//Declaring Constants
constint ZERO = 0;
constint ONE_HUNDRED = 100;
double median = 0.00;
double totalPoints;
vector<int> scores;
int size;
double findMedian(vector<int> &scores)
{
size_t mid = scores.size() / 2;
if (scores.size() == 0) return 0.0; // change this to whatever it should be
// Make scores[mid] the mid'th element
nth_element(scores.begin(), scores.begin() + mid, scores.end());
if (size % 2 == 0)
{
// size is even. Need to average of scores[mid] and scores[mid-1]
nth_element(scores.begin(), scores.begin() + mid - 1, scores.end());
median = (scores[mid] + scores[mid - 1]) / 2;
}
else
{
median = scores[mid];
}
return median;
}
int main()
{
int firstExam, secondExam, thirdExam;
int error;
cout << "Dr. DoLittle's Grading Program ..... (by Kirk Kelley)" << endl;
do
{
error = 0;
cout << "Please enter in the score for the first exam: ";
cin >> firstExam;
if (firstExam < 0 || firstExam > 100)
{
cout << "Please enter a valid score between 0 and 100" << endl;
cin >> firstExam;
cin.clear();
}
} while (error);
do
{
error = 0;
cout << "Please enter in the score for the second exam: ";
cin >> secondExam;
if (secondExam < 0 || secondExam > 100)
{
cout << "Please enter a valid score between 0 and 100" << endl;
cin >> secondExam;
cin.clear();
}
} while (error);
do
{
error = 0;
cout << "Please enter in the score for the third exam: ";
cin >> thirdExam;
if (thirdExam < 0 || thirdExam > 100)
{
cout << "Please enter a valid score between 0 and 100" << endl;
cin >> thirdExam;
cin.clear();
}
} while (error);
//declaring variable
int homework;
//initiating loop
while (!cin.eof())
{
homework = EOF;
cout << "\nEnter score for homework assignment (press Ctrl+Z to quit): ";
cin >> homework;
if (!cin.good())
if (homework == EOF)
break;
else
{
cout << "\nInvalid Input. Entry must be an integer." << endl;
cin.clear();
cin.ignore(std::numeric_limits<streamsize> ::max(), '\n');
}
else
{
if (homework >= ZERO && homework <= ONE_HUNDRED)
{
scores.push_back(homework);
}
else
{
cout << "\nInvalid Input. Grade must be between 0-100." << endl;
cin.clear();
cin.ignore(std::numeric_limits<streamsize> ::max(), '\n');
}
}
}
median = findMedian(scores);
totalPoints = median + firstExam + secondExam + thirdExam;
cout << "The total points earned was " << totalPoints << endl;
cout << "The median homework score was " << median << endl;
cout << "The letter calculated letter grade is ";
system("Pause");
return 0;
}