need help writing to outfile

hello every one i am writing a code for a class and i have it almost completely written i just need to have the output written to a txtfile and i'm not sure how to do it if i can get some help id greatly appreciate it her is what i have written. it is a multifile program. thank you in advance for any help you can give..


this is the driver file-
#include "Functions.h"
using namespace std;

int main()
{
WriteHeader();
string fileName;
string Date[500];
string Location[500];
string PollenName[500];
double ReadingValue[500];
string dataFile = "CABQPollenHistory.csv";
int count;

cout << "Please enter filename: ";
cin >> fileName;

if (GetDataFromFile(fileName, Date, Location, PollenName, ReadingValue, count))
{
int largestindex = LargestVal(ReadingValue, count);
cout << "Largest " << Date
[largestindex] << "-" << Location
[largestindex] << "-" << PollenName
[largestindex] << ": " << ReadingValue
[largestindex] << endl;

int minindex = MinVal(ReadingValue, count);
cout << "Minimum " << Date
[minindex] << "-" << Location
[minindex] << "-" << PollenName
[minindex] << ": " << ReadingValue
[minindex] << endl;


int avenindex = AveVal(ReadingValue, count);
cout << "Average " << Date
[avenindex] << "-" << Location
[avenindex] << "-" << PollenName
[avenindex] << ": " << ReadingValue
[avenindex] << endl;

}
return 0;


this is the fiunctions.h file-

#ifndef _FUNCTIONS_H
#define _FUNCTIONS_H

#include <iostream>
#include <string>
#include <fstream>

using namespace std;


void WriteHeader();
bool GetDataFromFile(string fileName, string Date[], string Location[], string PollenName[], double ReadingValue[], int& count);
int LargestVal(double ReadingValue[], int count);
int MinVal(double ReadingValue[], int count);
int AveVal(double ReadingValue[], int count);

#endif // !_FUNCTIONS_H

and this is my functions.cpp file

bool GetDataFromFile(string fileName,string Date[], string Location[], string PollenName[], double ReadingValue[], int& count)
{
ifstream file;
file.open(fileName);
if (!file)
{
cout << "could not open file" << endl;
return false;
}

string element;
getline(file, element);
count = 0;
while (getline(file, element,'\t'))
{
Date[count] = element;
getline(file, element, '\t');
Location[count] = element;
getline(file, element, '\t');
PollenName[count] = element;
getline(file, element);
ReadingValue[count] = stod(element);
//getline(file, element);
count++;
}

return true;
}

int LargestVal(double ReadingValue[], int count)
{
int largestindex = 0;
for (int i = 0; i < count; ++i)
{
if (ReadingValue[i] > ReadingValue[largestindex])
{
largestindex = i;
}
}
return largestindex;
}

int MinVal(double ReadingValue[], int count)
{
int minindex = 0;
for (int i = 0; i < count; ++i)
{
if (ReadingValue[i] < ReadingValue[minindex])
{
minindex = i;
}
}
return minindex;
}

int AveVal(double ReadingValue[], int count)
{
int largestindex{800};
int minindex{1};

int avenindex = 0;
for (int i = 0; i < count; ++i)
{
if (ReadingValue[i] < ReadingValue[avenindex])
{
avenindex = ReadingValue[largestindex] + ReadingValue[minindex];
}
}
int average = avenindex/count;
return avenindex;

}




Please reformat your code using code tags and post it again.
http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
im sorry im really new to this site and not sure how to do that. can i just send it to you in a zip file?
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
#include "Functions.h"
using namespace std;

int main()
{
WriteHeader();
string fileName;
string Date[500];
string Location[500];
string PollenName[500];
double ReadingValue[500];
string dataFile = "CABQPollenHistory.csv";
int count;

cout << "Please enter filename: ";
cin >> fileName;

if (GetDataFromFile(fileName, Date, Location, PollenName, ReadingValue, count))
{
int largestindex = LargestVal(ReadingValue, count);
cout << "Largest " << Date
[largestindex] << "-" << Location
[largestindex] << "-" << PollenName
[largestindex] << ": " << ReadingValue
[largestindex] << endl;

int minindex = MinVal(ReadingValue, count);
cout << "Minimum " << Date
[minindex] << "-" << Location
[minindex] << "-" << PollenName
[minindex] << ": " << ReadingValue
[minindex] << endl;


int avenindex = AveVal(ReadingValue, count);
cout << "Average " << Date
[avenindex] << "-" << Location
[avenindex] << "-" << PollenName
[avenindex] << ": " << ReadingValue
[avenindex] << endl;

}
return 0;


this is the fiunctions.h file-

#ifndef _FUNCTIONS_H
#define _FUNCTIONS_H

#include <iostream>
#include <string>
#include <fstream>

using namespace std;


void WriteHeader();
bool GetDataFromFile(string fileName, string Date[], string Location[], string PollenName[], double ReadingValue[], int& count);
int LargestVal(double ReadingValue[], int count);
int MinVal(double ReadingValue[], int count);
int AveVal(double ReadingValue[], int count);

#endif // !_FUNCTIONS_H

and this is my functions.cpp file

bool GetDataFromFile(string fileName,string Date[], string Location[], string PollenName[], double ReadingValue[], int& count)
{
ifstream file;
file.open(fileName);
if (!file)
{
cout << "could not open file" << endl;
return false;
}

string element;
getline(file, element);
count = 0;
while (getline(file, element,'\t'))
{
Date[count] = element;
getline(file, element, '\t');
Location[count] = element;
getline(file, element, '\t');
PollenName[count] = element;
getline(file, element);
ReadingValue[count] = stod(element);
//getline(file, element);
count++;
}

return true;
}

int LargestVal(double ReadingValue[], int count)
{
int largestindex = 0;
for (int i = 0; i < count; ++i)
{
if (ReadingValue[i] > ReadingValue[largestindex])
{
largestindex = i;
}
}
return largestindex;
}

int MinVal(double ReadingValue[], int count)
{
int minindex = 0;
for (int i = 0; i < count; ++i)
{
if (ReadingValue[i] < ReadingValue[minindex])
{
minindex = i;
}
}
return minindex;
}

int AveVal(double ReadingValue[], int count)
{
int largestindex{800};
int minindex{1};

int avenindex = 0;
for (int i = 0; i < count; ++i)
{
if (ReadingValue[i] < ReadingValue[avenindex])
{
avenindex = ReadingValue[largestindex] + ReadingValue[minindex];
}
}
int average = avenindex/count;
return avenindex;

}
Writing to cout is actually the right thing to do. If your program is called myProg and you want to send the output to output.txt, then just do this on the command line:
myProg > output.txt

Topic archived. No new replies allowed.