c++ need some insight

i have this much done,i need to program it so if a score under 1 0r over 6 is inputted than i will get an error message due to invalid number!also need to average the score out!!!!could do with insight or hints!!!thanx


#include <iostream>
#include <vector>
#include <conio.h>

class Scores
{
private:
std::vector<double> individual_scores;
int no_judges;
public:
//Constructor/deconstructor
Scores(int judges) {no_judges = judges; return;}
Scores();
~Scores() {}

//I/O
void input_scores();
double final_score();
};


void Scores::input_scores()
{
double in;
for(int i=0;i<no_judges;++i)
{
std::cout << "Enter score of judge no. " << i+1 << ":";
std::cin >> in;
individual_scores.push_back(in);
}
return;
}


double Scores::final_score()
{

double max = individual_scores.at(0);
double min = max;
double score = 0;
for(int i=0;i<no_judges;++i)
{
score += individual_scores.at(i);
if(individual_scores[i] < min) min = individual_scores.at(i);
if(individual_scores[i] > max) max = individual_scores.at(i);
}
score -= min;
score -= max;
return score;
}

int main()
{
int no_of_judges = 5;
Scores competition (no_of_judges);
competition.input_scores();
std::cout << "Final score: " << competition.final_score();
std::cin.get();


system ("Pause");
return 0;
}
CODE TAGS
CODE TAGS FOR CRYING OUT LOUD
If I could curse I would. But I'm trying to stay civil.
OK, so for your input, I'd propose a while loop directly after your cin. If the input is invalid, it tells the user to repeat the input.
1
2
3
4
5
6
7
cout << "Number between one and ten please!";
cin >> num;
while (num < 1 || num > 10)
{
    cout << "I SAID 1 TO 10!";
    cin >> num;
}


I'm sure you know how to average things out. Create a sum value, set it to zero and cycle through your vector, adding each element to the sum, then divide by the vector's length.
vector's length.

Vectors size.
i put that in but now i cant put in 1.1 2.4 4.7 ect..........the error message comes up flying accross the screen????????????????
Uh, what are the errors? What is your code? Either update your original code or post new code to reflect your changes. (AND PUT IT IN CODE TAGS)
And repeatedly typing question marks just makes your post look silly. We can tell you are confused; otherwise you wouldn't be asking any questions.
im really grateful for everyones help. here is my full code. u can see what i put in compared to post above!any score i put in now gives me the error message


#include <iostream>
#include <vector>

class Scores

{
private:
std::vector<double> individual_scores;
int no_judges;
public:
//Constructor/deconstructor
Scores(int judges) {no_judges = judges; return;}
Scores();
~Scores() {}

//I/O
void input_scores();
double final_score();
};

void Scores::input_scores()
{
double in;
for(int i=0;i<no_judges;++i)
{
std::cout << "Enter score of judge no. " << i+1 << ":";
std::cin >> in;
{
int num;
while (num < 1 || num > 6)

std::cout << "I SAID 1 TO 6!";
std::cin >> num;
}


individual_scores.push_back(in);

}
return;
}

double Scores::final_score()
{
double max = individual_scores.at(0);
double min = max;
double score = 0;
for(int i=0;i<no_judges;++i)
{
score += individual_scores.at(i);
if(individual_scores[i] < min) min = individual_scores.at(i);
if(individual_scores[i] > max) max = individual_scores.at(i);
}
score -= min;
score -= max;
return score;
}

int main()


{
int no_of_judges = 5;
Scores competition (no_of_judges);
competition.input_scores();
std::cout << "Final score: " << competition.final_score();
std::cin.get();
return 0;
}
CODE TAGS. Dammit, now it's seriously driving me insane. I ought to....
If you don't know how to use them say so. (By the way here is the article on how: http://www.cplusplus.com/forum/articles/16853/) But don't just ignore people when they tell you to do it as though we aren't even here. We have to try and read that crap. That means that it is YOUR problem and not ours to make it legible. It's ridiculous and pointless and I've lost my patience for it.
And you read my code too literally. It was an EXAMPLE. It demonstrated the concept. You need to take that concept and apply it to your code. That does not mean you copy my example into your code, because it shouldn't even work. An example is not a real life situation and it is not applicable to one, it explains a situation from a theoretical point of view. http://www.merriam-webster.com/dictionary/example
What I mean is that, immediately after each input, create a while loop whose condition checks if that input is valid. If it is not, the code in the while loop should get the input AGAIN. You do not just copy the example. It's not relevant to your situation. (And I can tell you copied it.)
Last edited on
here your while loop didnt have a "{" and a "}" so it repeated it forever

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

class Scores

{
private:
std::vector<double> individual_scores;
int no_judges;
public:
//Constructor/deconstructor
Scores(int judges) {no_judges = judges; return;}
Scores();
~Scores() {}

//I/O
void input_scores();
double final_score();
};

void Scores::input_scores()
{
double in;
int num;
for(int i=0;i<no_judges;i++)
{
std::cout << "Enter score of judge no. " << i+1 << ":";
std::cin >> in;
if (in < 1 || in > 6)
{
       std::cout << "Enter a number in the range of 1 and 6\n";
       std::cin >> in;
}

individual_scores.push_back(in);

}
return;
}

double Scores::final_score()
{
double max = individual_scores.at(0);
double min = max;
double score = 0;
for(int i=0;i<no_judges;++i)
{
score += individual_scores.at(i);
if(individual_scores[i] < min) min = individual_scores.at(i);
if(individual_scores[i] > max) max = individual_scores.at(i);
}
score -= min;
score -= max;
return score;
}

int main()


{
int no_of_judges = 5;
Scores competition (no_of_judges);
competition.input_scores();
std::cout << "Final score: " << competition.final_score();
std::cin.get();
std::cout << "\n";
system("pause");
return 0;
}
Last edited on
i hope it helps :)
I wrote the full code under your other post "diving competition".

I never used functions or classes or vectors or whatever else though because I thought you wouldnt have covered it yet but looking here, it seems you did. well then you'll just need to alter that, other than that it works perfect!

Cheers
Topic archived. No new replies allowed.