reading in text files with arrays

I have a lab that is almost complete but I'm stuck with a minor problem that I can't seem to figure out. Any help would be greatly appreciated.

Lab:
The local pet store has 35 different bowls with fish in them arranged in a circle in the “fish room.” The first six have goldfish, the next seven have guppies, the next nine have angel fish, the next eight have goldfish, and the last five hold tiger fish. As the bowls vary in size, the number of fish that are in each bowl at the start of the day also varies. The count of fish in each bowl is as follows:
Bowls 1 – 3: 15 fish
Bowls 4 – 7: 8 fish
Bowl 8: 19 fish
Bowls 9 – 12: 16 fish
Bowls 13 – 22: 14 fish
Bowls 23 – 24: 31 fish
Bowls 25 – 29: 9 fish
Bowls 30 – 33: 26 fish
Bowls 34 – 35: 8 fish
Houston…We have a Problem:
A trained seal finds its way into the store. The seal begins to eat fish in the following manner.
a. It walks over to bowl #1.
b. It counts four bowls and then eats a fish (the first fish it eats comes from bowl #4 and it’s a goldfish).
c. It again counts four, starting with the next available bowl, and eats a fish (the second fish it eats comes from bowl #8 and it’s a guppy.
Method:
The conditions that controls the seal are: a. If there are no fish left in a bowl, the seal does not count the bowl and skips it and goes to the next bowl containing fish. (It starts counting four from the bowl it ate from.)
b. The bowls are arranged in a circle, there is no last bowl.
c. After the seal eats a bowl #35, it counts over 4 bowls and finds itself at bowl #4. (Bowl #3 if four bowls away from 34, etc.)
d. The seal continues to eat until 361 fish are consumed.
Your Task:
You are to write a C++ program that will calculate and report the following (put breaks in so the user can read):
a. The number and type of fish in each bowl before the seal begins eating.
b. The total number of each type of fish before the seal begins to eat.
c. The number and type of fish left in each bowl after each time the seal eats.
d. The total number of each type of fish remaining after the seal eats.

The input file I am reading from looks like this;

0 G 15
1 G 15
2 G 15
3 G 8
4 G 8
5 G 8
6 U 8
7 U 19
8 U 16
9 U 16
10 U 16
11 U 16
12 U 14
13 A 14
14 A 14
15 A 14
16 A 14
17 A 14
18 A 14
19 A 14
20 A 14
21 A 14
22 G 31
23 G 31
24 G 9
25 G 9
26 G 9
27 G 9
28 G 9
29 G 26
30 T 26
31 T 26
32 T 26
33 T 8
34 T 8

I can't seem to get the bowl number to loop back to zero. It goes up to 35 and gives me a long negative number and skips zero and goes to one.

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;

int main()
{
	ifstream infile;
	ofstream outfile;
	string type[35];
	int numBowls[35], numFish[35], afterFish = 0, fishEaten = 0;
	int goldfish = 202, guppies = 105, angel = 126, tiger = 94;

	infile.open("C:\\Users\\User\\Downloads\\fish.txt");
	outfile.open("C:\\Users\\User\\Downloads\\fish_out.txt");

	if (infile.fail())
	{
		cout << "Error, cannot open input file." << endl;
		exit(1);
	}
	if (outfile.fail())
	{
		cout << "Error, cannot open output file." << endl;
		exit(1);
	}

	outfile << "Total number of each type of fish before eating." << endl;
	outfile << "------------------------------------------------" << endl;
	outfile << "Goldfish (G) : " << goldfish << endl;
	outfile << "Gupppies (U) : " << guppies << endl;
	outfile << "Angel    (A) : " << angel << endl;
	outfile << "Tiger    (T) : " << tiger << endl;
	outfile << endl << endl;

	int i = 0;
	while (!infile.eof())
	{
		infile >> numBowls[i] >> type[i] >> numFish[i];
		i++;
	}

	outfile << setw(15) << left << "Bowl Number" << setw(15) << left << "Type of Fish" << setw(27) << left << "# of Fish Before Eating" << setw(20) << left << "# of Fish Eaten" << setw(0) << left << "# of Fish After Eating" << endl;
	outfile << setw(15) << left << "-----------" << setw(15) << left << "------------" << setw(27) << left << "-----------------------" << setw(20) << left << "---------------" << setw(0) << left << "----------------------" << endl;

	for (int i = 3; fishEaten < 361; i += 4)
	{
		if (i > 35)
		{
			i = i - 35;
			numBowls[i] < 35;
		} //go in circles
	EAT:
		if (numFish[i - 1] > 0)
		{
			//eat
			numFish[i - 1]--;
			afterFish = numFish[i] - 1;
			if (afterFish < 0)
			{
				afterFish = 0;
			}
			fishEaten++;
		}

		else
		{
			//skip
			i++;
			goto EAT;
		}

		outfile << setfill(' ') << setw(5) << " " << setw(15) << left << i << setw(20) << left << type[i - 1] << setw(24) << left << numFish[i] << setw(24) << left << fishEaten << setw(20) << left << afterFish << endl;
	}


	infile.close();
	outfile.close();
	return 0;

}
On line 52, if you're going to get the bowl number back to 0 just set i to 0! i = 0;

If you do i = i - 35;, then you will be doing 36 - 35!

Make things simple.
Last edited on
Topic archived. No new replies allowed.