Writing a 2D array to a file (void function problems)
Nov 11, 2010 at 7:16pm UTC
Hey guys, im currently working on writing the results of this program to a file, the problems come in when i try to write the void functions to a file, is there any help you guys can give me? Heres 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
const int MONKEYS = 3;
const int DAYS = 7;
double average = 0;
int getSum (int [][DAYS]);
void getChart (int [][DAYS]);
void dayAvg (int table [][DAYS]);
int findLowest (int table [][DAYS]);
int findHighest (int table [][DAYS]);
int main(int argc, char *argv[])
{
int table [MONKEYS][DAYS];
ofstream outfile;
cout << "The diet of 3 monkeys over 7 days, please input your info: \n\n" ;
outfile.open("MonkeyBuisnessFile.txt" );
getChart(table);
cout << "The total amount of food consumed by the monkeys: "
<< getSum(table) << " Pounds\n\n" ;
outfile << "The total amount of food consumed by the monkeys: "
<< getSum(table) << " Pounds\n\n" ;
dayAvg(table);
cout << "The lowest amount of food eaten : " << findLowest(table) << " Pounds\n" ;
outfile << "The lowest amount of food eaten : " << findLowest(table) << " Pounds\n" ;
cout << "The highest amount of food eaten : " << findHighest(table) << " Pounds\n" ;
outfile << "The highest amount of food eaten : " << findHighest(table) << " Pounds\n" ;
outfile.close();
system("PAUSE" );
return EXIT_SUCCESS;
}
int getSum (int table [][DAYS])
{
int sum = 0;
for ( int monkey = 0; monkey < MONKEYS; monkey++)
{
for (int day = 0; day < DAYS; day++)
sum += table [monkey][day];
}
return sum;
}
void getChart (int table[][DAYS])
{
for (int monkey = 0; monkey < MONKEYS; monkey++)
{
for (int day = 0; day < DAYS; day++)
{
cout << "Monkey " << (monkey+1) << ", " ;
cout << "Day " << (day+1) << ": " ;
cin >> table [monkey][day];
}
cout << endl;
}
}
void dayAvg (int table [][DAYS])
{
for ( int day = 0; day < DAYS; day++)
{
int total = 0;
for (int monkey = 0; monkey < MONKEYS; monkey++)
{
total += table [monkey][day];
}
average = total/MONKEYS;
cout << "Average food consumed on day " << (day+1)
<< " by all 3 monkeys is: " << average << " Pounds" << endl;
}
}
int findLowest (int table [][DAYS])
{
int lowest = table [0][0];
for (int monkey = 0; monkey < MONKEYS; monkey++)
{
for (int day = 0; day < DAYS; day++)
{
if (table [monkey][day] < lowest)
lowest = table[monkey][day];
}
cout << endl;
}
return lowest;
}
int findHighest (int table [][DAYS])
{
int highest = table [0][0];
for (int monkey = 0; monkey < MONKEYS; monkey++)
{
for (int day = 0; day < DAYS; day++)
{
if (table [monkey][day] > highest)
highest = table[monkey][day];
}
cout << endl;
}
return highest;
}
Nov 11, 2010 at 7:31pm UTC
Also how do i use input validation n the getChart function so the program advertises the user that negative numbers aren't allowed?
Topic archived. No new replies allowed.