Vector, eating contest.

Hi, I am creating a small program that allows for the user to enter an amount of people to enter an eating contest, followed by the amount of pancakes eaten by each contestor who has joined the competition.

Here is my code:

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

void getAmount(std::string &input) {
	std::cout << "How many people are contesting?: ";
	std::getline(std::cin, input);
}

int main() {
	std::string input = "";

	std::cout << "You are holding an eating contest, to see who can eat the most pancakes!" << std::endl;

	getAmount(input);

	int number;
	std::vector<int> contestants;

	bool loop = true;
	while (loop) {
		std::stringstream temp(input);

		if (temp >> number) {
			if (number != 0) {
				std::cout << "The number of people contesting: " << number << std::endl << std::endl;

				for (int i = 0; i < number; i++) {
					std::cout << "How many pancakes did contestant #" << i << " eat?: ";
					std::getline(std::cin, input);
					contestants.push_back(atoi(input.c_str()));
				}
				
				std::cout << "Results:" << std::endl;
				for (int x = 0; x < (signed)contestants.size(); x++) {
					std::cout << "Contestant #" << x << " ate ";
					for (std::vector<int>::iterator i = contestants.begin() + x; i != contestants.end(); ++i) {
						std::cout << *i;
					}
					std::cout << " Pancakes!" << std::endl;
				}
				std::cout << '\n';

				loop = false;
			}
			else {
				std::cout << "There cannot be 0 contestants!" << std::endl;
				getAmount(input);
			}
		}
		else {
			std::cout << "Invalid input, please enter an integer number to represent the amount of people contesting" << std::endl;
			getAmount(input);
		}
	}

	std::cin.ignore();
	std::cin.get();

	return 0;
}


Output is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
You are holding an eating contest, to see who can eat the most pancakes!
How many people are contesting?: 5
The number of people contesting: 5

How many pancakes did contestant #0 eat?: 4
How many pancakes did contestant #1 eat?: 2
How many pancakes did contestant #2 eat?: 1
How many pancakes did contestant #3 eat?: 6
How many pancakes did contestant #4 eat?: 1
Results:
Contestant #0 ate 42161 Pancakes!
Contestant #1 ate 2161 Pancakes!
Contestant #2 ate 161 Pancakes!
Contestant #3 ate 61 Pancakes!
Contestant #4 ate 1 Pancakes! 


As you can probably figure out, that is not quite the output I want.
What is the error in this code? I am new to vectors :D
Also any bad things I do in the code, please also mention, as I want to learn to write clean code :)
The problem is in the output.
1
2
3
4
5
6
std::cout << "Results:" << std::endl;
for (size_t i = 0; i < contestants.size(); ++i) 
{
   std::cout << "Contestant #" << i << " ate " << contestants[i];
  std::cout << " Pancakes!" << std::endl;
}

Output:

You are holding an eating contest, to see who can eat the most pancakes!
How many people are contesting?: 5
The number of people contesting: 5

How many pancakes did contestant #0 eat?: 4
How many pancakes did contestant #1 eat?: 2
How many pancakes did contestant #2 eat?: 1
How many pancakes did contestant #3 eat?: 6
How many pancakes did contestant #4 eat?: 1
Results:
Contestant #0 ate 4 Pancakes!
Contestant #1 ate 2 Pancakes!
Contestant #2 ate 1 Pancakes!
Contestant #3 ate 6 Pancakes!
Contestant #4 ate 1 Pancakes!


ooo, thank you!
Also any bad things I do in the code, please also mention

To get the number of people, you read a string in a function (getAmount()), then you decode the string as an integer at lines 23 & 25. Then you handle bad input at lines 48-49 and 53-54. It would be better to centralize all of that code inside getAmount().

First, rather than reading a string and then converting to a number, just read the number directly:
1
2
3
int numPeople
std::cout << "How many people are contesting?: ";
cin >> numPeople;


Also, you have two places where you need to read a number that is greater than or equal to zero (number of contestants and number of pancakes). So you could make a function handles both cases. The only difference is the prompt so make that a parameter.

When reading and validating input, I find it easiest to break out of the middle of the loop.

Putting this together, getAmount becomes:
1
2
3
4
5
6
7
8
9
10
11
12
13
// Prompt for and read an unsigned number.
unsigned getAmount(const char *prompt)
{
    unsigned result;
    while (true) {
        std::cout << prompt;
        if (std::cin >> result) {
            return result;
        } else {
            std::cout << "Please enter a number that is zero or more\n";
        }
    }
}

Note that I have not done error recovery when the user enters a bad number. I'll leave that to you if you want to add it.

With this the main program shrinks considerably:
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
int main() {
    std::cout << "You are holding an eating contest, to see who can eat the mos\
t pancakes!" << std::endl;

    int number = getAmount("How many people are contesting?: ");
    std::cout << "The number of people contesting: " << number << std::endl << \
std::endl;

    std::vector<int> contestants;

    for (int i = 0; i < number; i++) {
        std::stringstream prompt;
        unsigned pancakes;
        prompt << "How many pancakes did contestant #" << i << " eat?: ";
        pancakes = getAmount(prompt.str().c_str());
        contestants.push_back(pancakes);
    }

    std::cout << "Results:" << std::endl;
    for (unsigned x = 0; x < contestants.size(); x++) {
        std::cout << "Contestant #" << x
                  << " ate " << contestants[x] << " pancakes\n";;
    }

    std::cin.ignore();
    std::cin.get();

    return 0;
}
Topic archived. No new replies allowed.