not finding the file on the second loop

It is running as it should for a loop and is terminated at line 59 on the second loop. When I enter the file name to go into char in_file [] for the second time it is not finding the file, or is not opening the file. Any ideas?

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;

void get_file(ifstream& fin, ofstream& fout);
/* to get, open, and test the files for input and output data.*/
void find_mean(ifstream& fin, ofstream& fout, double& mean, double list[], int& count);
/* to find the average of all numbers in the file, write the mean to 
the output file, and return the count to the main. */
void deviation (ifstream& fin, ofstream& fout, double list[], int count, double mean);
/*to geet the standard deviation of the group of numbers and write it to the output file.  */
void sort_array(double list[], int count);
/*to sort the numbers in the array in ascending order to get the median*/
void median(double list [], int count, ofstream& fout);
/*to get the median of the list and send it to the file*/
void reset_array(double list[], int count);
/*to reset the values of the array and char*/
void close_files (ifstream& fin, ofstream& fout);
/*to close the opened files*/


int main(int argc, char *argv[])
{
             int count = 0;
             double mean, list[1000];
             ifstream fin;
             ofstream fout;
             char ans;
             do
             {
             
                      get_file (fin, fout); 
                      find_mean (fin, fout, mean, list, count);
                      deviation (fin, fout, list, count, mean);
                      sort_array(list, count);
                      median (list, count, fout);
                      reset_array (list, count);
                      close_files (fin, fout);
                      cout << "Would you like to analize another file?  Y or N:" << endl;
                      cin >> ans;
             }while (ans == 'Y' || ans == 'y');
             

             system("PAUSE");
             return EXIT_SUCCESS;
}

void get_file (ifstream &fin, ofstream &fout)
{
              char in_file[40]= {}, out_file[40]= {};
              cout << "What is the name of the file containing data to be analyzed?\n";
              cin >> in_file;
              fin.open(in_file);
              if (fin.fail())
              {
                               cout << "Input file failed.\n";
                               exit(1);
              }
              cout << "What is the name of the file where you would like to store data?\n";
              cin >> out_file;
              fout.open(out_file, ios::app);
              if (fout.fail())
              {
                               cout << "imput file failed.\n";
                               exit(1);
              }
}

void find_mean (ifstream& fin, ofstream& fout, double& mean, double list[], int& count)
{
               count = 0;
               double next, sum = 0;
               fout.setf(ios::fixed); 
               fout.setf(ios::showpoint);
               fout.precision(2);
               while (fin >> next)
               {
                               list[count] = next;
                               count++; 
                               sum += next;
               }
               mean = (sum/count);      
               fout << "MEAN: " << mean << endl;
}

void deviation (ifstream& fin, ofstream& fout, double list[], int count, double mean)
{
               fout.setf(ios::fixed);
               fout.setf(ios::showpoint);
               fout.precision(2);
               double variance;
               double sum = 0;
               for (int i = 0; i < count; i++)
               {
                              sum += pow((list[i] - mean), 2.0);
               }
               variance = sum/count;
               fout << "Standard deviation:  " << sqrt(variance) << endl;
}

void sort_array(double list[], int count)
{
				double temp =0;
			   for (int i=0; i < (count - 1); i++)
			   {	
						int j = i+1;
						for (; j < count; j++)
						{
								if (list[i]>list[j])
								{
			      					temp = list[i];
									list [i] = list [j];
									list [j] = temp;
								}
						}
			   }
}

void median(double list [], int count, ofstream& fout)
{
               fout.setf(ios::fixed); 
               fout.setf(ios::showpoint);
               fout.precision(2);    
               double median;     
               if (count%2 == 0)
                           median =  ((list[count/2] + list [(count/2)-1])/2);
               else 
                           median = (list[count/2]);
               fout << "Median:  " << median << endl << endl;
}

void reset_array(double list[], int count)
{
    for(int i = 0; i < count; i++)
    {
         list[i] = 0;
    }
}
             
void close_files (ifstream& fin, ofstream& fout)
{
               fin.close();
               fout.close();
}               
Last edited on
Topic archived. No new replies allowed.