Need help with loop coding with random #s

At the Wichita County Fair, there is a Duck Arcade game on the midway that guarantees that you will win a prize every time you play. In this arcade, there are hundreds of small, plastic ducks floating down a stream of water. On the bottom of each duck and unseen by the player is a positive integer in the range from 1 through 10. There is always an equal number of ducks with each of the 10 integers.
To play the game, the player selects 5 ducks out of the stream. The prize the player wins is based on the sum of the numbers on the bottom of the five ducks selected using the following guidelines:
Level 1–if the sum is 25 or less, the prize is a chalk dog
Level 2 –if the sum is 26 through 30, the prize is a 4" stuffed animal
Level 3 –if the sum is 31 through 35, the prize is a 12" stuffed animal
Level 4 –if the sum is greater than 35, the player can choose any prize in the Duck Arcade.

Your boss wants you to write a program that simulates random games for a large number of players so he can determine if the sum of the numbers on the 5 ducks selected in each game are in a range that will allow him to make a healthy profit.

Input
The first line of input will contain a single integer that indicates the number of simulations that will be run. The second line will contain a long integer that you will use to seed the random number generator for the simulation.The next n lines contain a single integer indicating how many games will be played in a particular simulation.

Output
Display your name on the first line of the output file.For each simulation, you will print the number of games in the simulation that fall into each of the Levels, 1 through 4, following the format shown in the example output below

#include<iostream>
#include<fstream>

using namespace std;
int main()
{

ifstream infile;
ofstream outfile;
infile.open("ducks.dat");
outfile.open("_ducks.txt");
//These commands let you retrieve data from
//a infile source and send it to and outfile source

int SimNum;
int MyRand;
int GamNum;
unsigned int seed = 432;
int sum = 0;
int lev1count = 0;
int lev2count = 0;
int lev3count = 0;
int lev4count = 0;

srand(seed);

while (infile >> SimNum)
{
while (infile >> GamNum)
{
for (int x = 1; x <= 5; x++)
{
infile >> MyRand;
MyRand = (rand() % 10) + 1;
sum += MyRand;
}

if (sum >= 25)
{
lev1count++;
}
else if (sum < 26 && sum >= 30)
{
lev2count++;
}
else if (sum < 31 && sum >= 35)
{
lev3count++;
}
else if (sum > 35)
{
lev4count++;
}

outfile << "Name" << endl;
outfile << "Simulation " << SimNum << endl;
outfile << "Level 1: " << lev1count << endl;
outfile << "Level 2: " << lev2count << endl;
outfile << "Level 3: " << lev3count << endl;
outfile << "Level 4: " << lev4count << endl;

}
}
infile.close();
outfile.close();
}
closed account (48T7M4Gy)
Please use code tags and use proper indentation. Use <> in toolbar on the right after you select the code in your text above.

Also, were you given this code as part of the assignment? Where are the samples referred to in the assignment brief?

Also, what is it doing that's not up to your expectations??
Last edited on
Yes it was an assignment. The input file read:
2 (simulation)
432 (seed)
10 (games)
20(games)

The only thing that I can seem to accomplish with the code with to put the number of games with each level count. So if the user won a game with the sum less than 25 then it should be shown as Level 1 : 1 and they won 5 games with the sum greater than 26 but less than 30 then it should be shown as:
Level 1 : 1
Level 2 : 5
but I can not get the game to increment to a specific level.

My apologies on the code format.
Here is your code indented and with code tags:
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
#include<iostream>
#include<fstream>

using namespace std;
int
main()
{

    ifstream infile;
    ofstream outfile;
    infile.open("ducks.dat");
    outfile.open("_ducks.txt");
//These commands let you retrieve data from
//a infile source and send it to and outfile source

    int SimNum;
    int MyRand;
    int GamNum;
    unsigned int seed = 432;
    int sum = 0;
    int lev1count = 0;
    int lev2count = 0;
    int lev3count = 0;
    int lev4count = 0;

    srand(seed);

    while (infile >> SimNum) {
	while (infile >> GamNum) {
	    for (int x = 1; x <= 5; x++) {
		infile >> MyRand;
		MyRand = (rand() % 10) + 1;
		sum += MyRand;
	    }

	    if (sum >= 25) {
		lev1count++;
	    } else if (sum < 26 && sum >= 30) {
		lev2count++;
	    } else if (sum < 31 && sum >= 35) {
		lev3count++;
	    } else if (sum > 35) {
		lev4count++;
	    }

	    outfile << "Name" << endl;
	    outfile << "Simulation " << SimNum << endl;
	    outfile << "Level 1: " << lev1count << endl;
	    outfile << "Level 2: " << lev2count << endl;
	    outfile << "Level 3: " << lev3count << endl;
	    outfile << "Level 4: " << lev4count << endl;

	}
    }
    infile.close();
    outfile.close();
}

Line 28: why are you reading the number of simulations in a while loop? You should read it only once and then setup a for loop to execute that many times.

Line 29: This shouldn't be in a while loop either. It should execute once for each simulation

Line 31: You shouldn't be reading MyRand at all.

Line 36: >= should be <=. Check the comparisons on lines 38 and 40 too.

Once you fix these, look back at lines 20-24 and consider when those variables need to be set to zero. Once when the prorgram runs? Once per simulation? Once per game? Move their declarations to the appropriate spot.
Topic archived. No new replies allowed.