|
c++ help
Link |
by zerobinary
on 2007-09-02 10:43:14
|
|
http://namelessgeeks.spaces.live.com/default.aspx Need some help on the blog post called c++ script help. Since i can't post in here. I always get an error like this. Sorry about that plz help me. |
|
Re: c++ help
Link |
by
|
|
the code is a complete mess, with quoted strings left unterminated, no indentation, and missing semicolons galore. i've cleaned it up for you, and even corrected spelling in the user interface, because i have nothing better to do with my time. (that was sarcasm) #include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main ()
{
cout << "Enter your first name: ";
string name;
cin >> name;
cout << "Hello, " << name << "!" << endl;
cout << "Enter your midterm and final exam grades: ";
double midterm, final;
cin >> midterm >> final;
cout << "Enter all your homework grades, followed by end-of-file: ";
vector<double> homework;
double x;
while (cin >> x) homework.push_back(x);
if(homework.size() == 0)
{
cout << endl << "You must enter your grades, please try again." << endl;
return 1; // exit program with error
}
sort(homework.begin(), homework.end());
unsigned int mid = homework.size() / 2;
// if there's an odd number of homework grades, take the middle one.
// if there's an even number of homework grades, take the average of the two middle ones.
double median = ((homework.size() % 2) ? homework[mid] : (homework[mid] + homework[mid - 1]) / 2);
double grade = (0.2 + midterm) + (0.4 * final) + (0.4 * median);
printf("%3f", grade);
return 0; // exit program
}
|