Why won't my program work?
Jan 27, 2013 at 5:08pm UTC
It is meant to take in numbers and put them into an array then display the low/high/average using 3 functions
I think I am having a brain snap because I can't see what I am missing, it is taking in 0's even though it is not supposed to..
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
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
void createFile();
void readFile(int [], int &, int );
void showData(int [], int );
int main()
{
const int SIZE = 200;
int numArray[SIZE],
counter = 0;
createFile();
readFile(numArray, counter, SIZE);
showData(numArray, counter);
return 0;
}
void createFile()
{
int sentinel;
ofstream numbers;
numbers.open("numbers.txt" );
cout<<"You will now enter numbers into the file." <<endl;
do
{
cout<<"Enter a number: " ;
cin>>sentinel;
if (sentinel!=0)
{
numbers<<sentinel<<endl;
}
} while (sentinel!=0);
numbers.close();
}
void readFile(int num[], int & counter, int SIZE)
{
ifstream numbers;
numbers.open("numbers.txt" );
if (!numbers)
{
cout<<"Error opening file." ;
exit(1);
}
while (numbers>>num[SIZE])
{
counter++;
}
numbers.close();
}
void showData(int numArray[], int counter)
{
int low,
high,
average = 0;
low = numArray[0];
high = numArray[0];
average+=numArray[0];
for (int i = 1; i<counter; i++)
{
if (low>numArray[i])
low=numArray[i];
if (high<numArray[i])
high=numArray[i];
average+=numArray[i];
}
average/=counter;
cout<<"The lowest number is: " <<low<<endl;
cout<<"The highest number is: " <<high<<endl;
cout<<"The average is: " <<average<<endl;
}
Jan 27, 2013 at 5:18pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
void readFile(int num[], int & counter, int SIZE)
{
ifstream numbers;
numbers.open("numbers.txt" );
if (!numbers)
{
cout<<"Error opening file." ;
exit(1);
}
//********* *This next loop does not make sense *************//
while (numbers>>num[SIZE])
{
counter++;
}
numbers.close();
}
Last edited on Jan 27, 2013 at 5:20pm UTC
Jan 27, 2013 at 5:20pm UTC
Never mind think I got it, but is this the correct thing to do? Modified the second function by adding a variable that goes up every time instead of having SIZE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
void readFile(int num[], int & counter)
{
int up = 0;
ifstream numbers;
numbers.open("numbers.txt" );
if (!numbers)
{
cout<<"Error opening file." ;
exit(1);
}
while (numbers>>num[up])
{
counter++;
up++;
}
numbers.close();
}
Last edited on Jan 27, 2013 at 5:20pm UTC
Jan 27, 2013 at 5:23pm UTC
¿what's the difference between `counter' and `up'?
You may want to make sure to not step out of bounds
Jan 27, 2013 at 5:24pm UTC
Yea sorry about that I thought it would magically sort itself out in order
Jan 27, 2013 at 5:28pm UTC
¿what's the difference between `counter' and `up'?
You may want to make sure to not step out of bounds
Well the teacher wanted us to add a counter so that we count the number of times it reads from the file, now that I think about it, indeed I think counter would do the same job
Topic archived. No new replies allowed.