Format to 3 columns

I have been playing around with my code I have written and I can't seem to be able to get everything to "cout" in three separate side by side columns...any advice or help would be greatly appreciated. Thnx in advanced ~tonyp

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
148
149
150
151
152
153
154
#include<fstream>
#include<iostream>
#include<cstdlib>
#include<string>
#include<cmath>
#include<iomanip>

using namespace std;

const int MAX_HOURS = 24;

string getDate1 (ifstream& fin);
string getDate2 (ifstream& fin);
void tempDay1(ifstream& fin, double day1[], string& tempDate1);
void tempDay2(ifstream& fin, double day2[], string& tempDate2);
void getHours();
double findMean1 (const double day1[]);
double findMean2 (const double day2[]);
double stdDev1 (const double day1[], double mean1);
double stdDev2 (const double day2[], double mean2);

int main()
{
    ifstream fin;
    string tempDate1, tempDate2;
    double day1[MAX_HOURS], day2[MAX_HOURS];
    tempDate1 = getDate1(fin);
    tempDate2 = getDate2(fin);
    tempDay1(fin, day1, tempDate1);
    getHours();
    tempDay2(fin, day2, tempDate2);
    double mean1 = findMean1(day1);
    double mean2 = findMean2(day2);
    double stdv1 = stdDev1(day1, mean1);
    double stdv2 = stdDev2(day2, mean2);
    

}


string getDate1 (ifstream& fin)
{
    fin.open("weatherdata.txt");
    if(fin.fail( ))
    {
        cout << "File failed to open." << endl;
        exit(1);
    }

    string tempDate1;
    fin >> tempDate1;
    return tempDate1;
}

string getDate2 (ifstream& fin)
{
    string tempDate2;
    fin >> tempDate2;
    return tempDate2;
}

void getHours()
{
    cout << left << setw(20) << "Hour of the Day" << "\n"
    << setw(20) << "_______________" << "\n" << "\n" << endl;
    
    for (int i = 1; i <= MAX_HOURS; i++)
    {
        cout << left << setw(20) << i << "\n" << endl; 
    }
}

void tempDay1(ifstream& fin, double day1[], string& tempDate1)
{
    int i = 0;
    double tempFromFile;
    cout << left << setw(25) << tempDate1 << "\n" 
    << setw(25) << "_________" << "\n" << "\n" << endl;
    for (int i = 0; i < MAX_HOURS; i++)
    {
        fin >> tempFromFile;
        day1[i] = tempFromFile;
        cout << left << setw(25) << day1[i] << "\n" << endl;
    }     
    
}

void tempDay2(ifstream& fin, double day2[], string& tempDate2)
{
    int i = 0;
    double tempFromFile;
    cout << left << setw(30) << tempDate2 << "\n" 
    << setw(30) << "_________" << "\n" << "\n" << endl;
    for (int i = 0; i < MAX_HOURS; i++)
    {
        fin >> tempFromFile;
        day2[i] = tempFromFile;
        cout << left << setw(30) << day2[i] << "\n" << endl;
    }     
    fin.close();
}


double findMean1 (const double day1[])
{
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(1);
    double tempTotal = 0;
    for(int i = 0; i < MAX_HOURS; i++)
    {
        tempTotal = tempTotal + day1[i];
    } 
    return tempTotal/MAX_HOURS;

}

double findMean2 (const double day2[])
{
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(1);    
    double totalTemp = 0;
    for(int i = 0; i < MAX_HOURS; i++)
    {
        totalTemp = totalTemp + day2[i];
    }
    return totalTemp/MAX_HOURS;
}

double stdDev1 (const double day1[], double mean1)
{
    double temp = 0;
    double stdv1;
    for (int i = 0; i < MAX_HOURS; i++)
    {
        temp += pow((day1[i] - mean1), 2);
    }
    stdv1 = sqrt(temp / (MAX_HOURS));
    return stdv1;
} 

double stdDev2 (const double day2[], double mean2)
{
    double temp = 0;
    double stdv2;
    for (int i = 0; i < MAX_HOURS; i++)
    {
        temp += pow((day2[i] - mean2), 2);
    }
    stdv2 = sqrt(temp / (MAX_HOURS));
    return stdv2;
} 
Which line is the problem on?
So, you should tell us how your program works, what's in the file you opened. Or maybe comment in your code to make it more easier.

You give me nothing and when I test your code. "What? I have to write a file? But I don't know what will be in the file.So, I leave."
ya sorry i didn't include the .txt file The .txt file has two strings (dates) 6/14/2014
6/15/2014
and then it lists 48 numbers to represent one temp. for each hour of each day.

Here is the weatherdata.txt info:
6/14/2014
6/15/2014
69
69
70
70
70
72
73
74
77
79
80
81
82
85
87
90
89
86
82
79
75
74
72
70
68
68
69
70
71
73
75
77
80
83
85
86
90
91
91
90
87
86
84
82
78
74
71
69

The output looks like this (sorry for the long list-like output) and then I will add the cout << for mean1, mean2, stdv1, and stdv2.
I am unsure of where the problem is, or if it is even possible to turn that output into three side by side columns.

Hour of the Day
_______________


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

6/14/2014
_________


69.0

69.0

70.0

70.0

70.0

72.0

73.0

74.0

77.0

79.0

80.0

81.0

82.0

85.0

87.0

90.0

89.0

86.0

82.0

79.0

75.0

74.0

72.0

70.0

0.0
0.0
6/15/2014
_________


68.0

68.0

69.0

70.0

71.0

73.0

75.0

77.0

80.0

83.0

85.0

86.0

90.0

91.0

91.0

90.0

87.0

86.0

84.0

82.0

78.0

74.0

71.0

69.0
Last edited on
So, do you know why your output comes like this?
You have to design the format of the output before you program it.

In this case I suggest you to keep all data in array, loop using hour and print out every day in that hour in the same line. such as,

cout << setw(15) << "Hour" << setw(25) << tempDate1 << setw(25) << tempDate2 << endl;
loop i = 1 -> 24
cout << setw(15) << 1 << setw(25) << day1[i] << setw(25) << day2[i] << endl;
end loop

something like this in new function.
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
148
149
150
Purpose of program: Diplay hourly temperature fluctuations as well as the daily mean and standard deviation.
***************************************************************************/
#include<fstream>
#include<iostream>
#include<cstdlib>
#include<string>
#include<cmath>
#include<iomanip>

using namespace std;

const int MAX_HOURS = 24;

string getDate1 (ifstream& fin);// used to get the first string date
string getDate2 (ifstream& fin);// used to get the second string date
void tempDay1(ifstream& fin, double day1[], string& tempDate1);//fills day1 array with 24 temperature values
void tempDay2(ifstream& fin, double day2[], string& tempDate2);//fills day2 array with 24 temperature values
double findMean1 (const double day1[]);//calculates the mean temperature for day1 array
double findMean2 (const double day2[]);//calculates the mean temperature for day2 array
double stdDev1 (const double day1[], double mean1);//calculates the standard deviation for day1 array
double stdDev2 (const double day2[], double mean2);//calculates the standard deviation for day2 array
void showData (const double day1[], const double day2[], string tempDate1, string tempDate2);//shows data in desired format

int main()
{
    ifstream fin;
    string tempDate1, tempDate2;
    double day1[MAX_HOURS], day2[MAX_HOURS];
    tempDate1 = getDate1(fin);
    tempDate2 = getDate2(fin);
    double mean1 = findMean1(day1);
    double mean2 = findMean2(day2);
    double stdv1 = stdDev1(day1, mean1);
    double stdv2 = stdDev2(day2, mean2);
    tempDay1(fin, day1, tempDate1);
    tempDay2(fin, day2, tempDate2);
    showData(day1, day2, tempDate1, tempDate2);
    cout << setw(20) << "Daily Mean:" << setw(25) << mean1 << setw(30) << mean2 << endl;
    cout << setw(20) << "Standard Deviation:" << setw(25) << stdv1 << setw(30) << stdv2 << endl;


}


string getDate1 (ifstream& fin)
{
    fin.open("weatherdata.txt");//opens data file
    if(fin.fail( ))
    {
        cout << "File failed to open." << endl;
        exit(1);//closes the program if .txt file fails to open
    }

    string tempDate1;
    fin >> tempDate1;
    return tempDate1;
}

string getDate2 (ifstream& fin)
{
    string tempDate2;
    fin >> tempDate2;
    return tempDate2;
}

void tempDay1(ifstream& fin, double day1[], string& tempDate1)
{
    int i = 0;
    double tempFromFile;
    for (int i = 0; i < MAX_HOURS; i++)
    {
        fin >> tempFromFile;
        day1[i] = tempFromFile;
    }     
}

void tempDay2(ifstream& fin, double day2[], string& tempDate2)
{
    int i = 0;
    double tempFromFile;
    for (int i = 0; i < MAX_HOURS; i++)
    {
        fin >> tempFromFile;
        day2[i] = tempFromFile;
    }     
    fin.close();
}


double findMean1 (const double day1[])
{
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(1);
    double tempTotal = 0;
    for(int i = 0; i < MAX_HOURS; i++)
    {
        tempTotal = tempTotal + day1[i];
    } 
    return tempTotal/MAX_HOURS;

}

double findMean2 (const double day2[])
{
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(1);    
    double totalTemp = 0;
    for(int i = 0; i < MAX_HOURS; i++)
    {
        totalTemp = totalTemp + day2[i];
    }
    return totalTemp/MAX_HOURS;
}

double stdDev1 (const double day1[], double mean1)
{
    double temp = 0;
    double stdv1;
    for (int i = 0; i < MAX_HOURS; i++)
    {
        temp += pow((day1[i] - mean1), 2);
    }
    stdv1 = sqrt(temp / (MAX_HOURS));
    return stdv1;
} 

double stdDev2 (const double day2[], double mean2)
{
    double temp = 0;
    double stdv2;
    for (int i = 0; i < MAX_HOURS; i++)
    {
        temp += pow((day2[i] - mean2), 2);
    }
    stdv2 = sqrt(temp / (MAX_HOURS));
    return stdv2;
} 

void showData (const double day1[], const double day2[], string tempDate1, string tempDate2)
{
    cout << setw(20) << "Hour of the Day" << setw(25) << tempDate1 << setw(30) << tempDate2 << endl;
    cout << setw(20) << "________________" << setw(25) << "__________" << setw(30) << "__________" << "\n" << endl;
    for (int i = 0; i < MAX_HOURS; i++)
    {
        cout << setw(20) << i << setw(25) << day1[i] << setw(30) << day2[i] << endl;
    }
    cout << "_____________________________________________________________________________" << endl;
}   


Here is the output:
Hour of the Day 6/14/2014 6/15/2014
________________ __________ __________

0 69.0 68.0
1 69.0 68.0
2 70.0 69.0
3 70.0 70.0
4 70.0 71.0
5 72.0 73.0
6 73.0 75.0
7 74.0 77.0
8 77.0 80.0
9 79.0 83.0
10 80.0 85.0
11 81.0 86.0
12 82.0 90.0
13 85.0 91.0
14 87.0 91.0
15 90.0 90.0
16 89.0 87.0
17 86.0 86.0
18 82.0 84.0
19 79.0 82.0
20 75.0 78.0
21 74.0 74.0
22 72.0 71.0
23 70.0 69.0
_________________________________________________________________
Daily Mean: 0.0 0.0
Standard Deviation: 0.0 0.0
Last edited on
Did you check my previous reply?

And noticed, "\n" is newline, same as std::endl.
So, you got newline twice.
Last edited on
I did see your previous comment and just implemented a new void function called show data which does the output amazingly...I am so thankful for you taking time to help a noob in distress like myself...i am going to delete my last (long ass) comment and place my new code, because I am having one last issue with the mean and standard deviation values returning the proper numbers...they both return 0.0 now and they worked before...so I will once again ask for you to share your expertise with me if you would be so gracious
Disregard, I fixed it...I needed to move line 35 and 36 to line 31...Isk I want to thank you for all of your help
Is that clearly?

1
2
3
4
double mean1 = findMean1(day1);
double mean2 = findMean2(day2);
double stdv1 = stdDev1(day1, mean1);
double stdv2 = stdDev2(day2, mean2);


I think you find mean before you read the numbers ^^

EDIT Ah~ Okay np :)
Last edited on
Topic archived. No new replies allowed.