Using Arrays With Files! [Help] C++

I am Trying to make my program read 10 integers from a user-named file using arrays, but I encountered logic error because the calculations turns out to be wrong. my question is, Am I supposed to use arrays with files? if so, What is wrong? it runs fine but only the calculation is WAY OFF! Thank You.

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
117
118
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int SIZE = 10;

void input(const int a[], int size);
int getTotal(const int a[], int size);
double getAvg(const int a[], int size);
double getEvenAvg(const int a[], int size);
double getOddAvg(const int a[], int size);

int main()
{
   ifstream fin;
   int a[10] = {4,6,8,10,2,3,5,7,9,11}, b[10];
   char ans, file_name[16]; 
   
   cout << "This Program will Calculate the Total, Average, \n"
        << "Average of Odd numbers, and Average of Even numbers all from a list of 10 integers from file or this Program.\n";
   cout << "\nDo You want the list of 10 integers provided by the program? (Type 'Y' or 'y')\n";
   cin  >> ans;
   if(ans =='Y' || ans == 'y')
   {
      input(a, SIZE); 
   }
   else
   {
      cout << "You Chose to Calculate using the 10 integers from a file!\n";
      cout << "Please Enter the name of the Input File? (Maximun of 15 characters):(ex. test.txt)\n";
      cin  >> file_name;
   
      fin.open(file_name);
      if(fin.fail())
      {
         cout << "Input File Opening Failed.\n";
         system("PAUSE");
         exit(1);
      }
      while(fin >> b[10])
      {
         fin >> b[10];
         getTotal(b, SIZE);
         getAvg(b, SIZE);
         getEvenAvg(b, SIZE);
         getOddAvg(b, SIZE);
      }
      cout << "The Total sum of the integer from the file is: " << getTotal(b, SIZE) << endl;
      cout << "The Average of the integer from the file is: " << getAvg(b, SIZE) << endl;
      cout << "The Average of Even Numbers from the file is: " << getEvenAvg(b, SIZE) << endl;
      cout << "The Average of Odd Numbers from the file is: " << getOddAvg(b, SIZE) << endl;
      fin.close();
   }
   cout << "Program Terminated!\n";
   system("PAUSE");
   return 0; 
}
void input(const int a[], int size)
{
   cout << "You Chose to Calculate using the integers provided by the program!\n";
   getTotal(a, SIZE);
   getAvg(a, SIZE);
   getEvenAvg(a, SIZE);
   getOddAvg(a, SIZE);
      
   cout << "The Total sum of the integer is: " << getTotal(a, SIZE) << endl;
   cout << "The Average of the integer  is: " << getAvg(a, SIZE) << endl;
   cout << "The Average of Even Numbers is: " << getEvenAvg(a, SIZE) << endl;
   cout << "The Average of Odd Numbers is: " << getOddAvg(a, SIZE) << endl;
   
}
int getTotal(const int a[], int size)
{
   int total = 0;
   for(int i=0; i < size; i++)
   {
      total+= a[i];
   }
   return total;
}
double getAvg(const int a[], int size)
{
  double total = 0;
   for(int i=0; i < size; i++)
   {
      total+= a[i];
   }
   return (total/size);
}
double getEvenAvg(const int a[], int size)
{
   int count = 0;
   double total=0;
   for(int i=0; i< size; i++)
   {
      if(a[i] % 2==0)
      {
         total+= a[i];
         count++;
      }
   }
   return (total/count);
}
double getOddAvg(const int a[], int size)
{
   double total = 0;
   int count = 0;
   for(int i=0; i< size; i++)
   {
      if(a[i] %2 != 0)
      {
         total+=a[i];
         count++;
      }
   }
   return (total/count);
}
I think it's because you are reading in ASCII characters and treating them as numeric characters. A byte of memory containing the ASCII character for the numeral 1 is 0011 0001 and is equal to a numeric value of 49. The numeric value of 1 is 0000 0001. Simply storing the ASCII character into a int type does not change its value.

You can test this by reading in a single value of 1 and storing in an int. Print it out and see whether you get 49. If this is the case you need to research a method for converting the character strings into integers.
Last edited on
sorry, Im kind of lost. the naming of files is okay, its just the part with the program gathering integers from the file im having problems with.
Your problem is here:
40
41
42
while(fin >> b[10])
    {
        fin >> b[10];
Rather than filling up the array with values from the file, you're continually trying to write to the 11th element of the array (which is out of bounds).

It should probably be something more like
40
41
42
43
44
45
46
int count = 0;
while (count < 10 && fin >> b[count])
    ++count;
cout << "The Total sum of the integer from the file is: " << getTotal(b, count) << endl;
cout << "The Average of the integer from the file is: " << getAvg(b, count) << endl;
cout << "The Average of Even Numbers from the file is: " << getEvenAvg(b, count) << endl;
cout << "The Average of Odd Numbers from the file is: " << getOddAvg(b, count) << endl;
By the way, you don't need to call your getTotal, getAvg, getEvenAvg, and getOddAvg functions twice like you do in your input function -- the first set of calls compute and return the values, but you don't store them to anything so it's just like you didn't do anything at all.
So you can just delete those calls.
Great! Thanks!! lol its Cleaner now
Topic archived. No new replies allowed.