Triggering an output statement once at the bottom of a loop

Just looking at my main function and my output_warning function. Basically if the temp is greater than 50 or the windSpeed is greater than 0 and less than or equal to 3.0 I want the loop to trigger an output statement to display once under all the data.

I believe my current output_warning function doesn't work because the values are all being looped and since it's outside the loop it's not getting access to them.

The data of from the in_stream will always trigger this. If I put output_warning in the loop it will display each time these values are met which I don't want. Is there anyway to compensate for this?

This is basically what I'm going for to help clear things up. (This is if I just take out the if statement in output_warning and just display it every time)
https://gyazo.com/9038a48cd63e0f06f42edae6b649aa4a

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <cstdlib>

using namespace std;

void open_files(ifstream& in_stream, ofstream& out_stream);
double wind_chill(double temp, double windSpeed);
double cloud_base(double temp, double dewPoint);
void output_header(ofstream& out_stream);
void output_data(ofstream& out_stream, double temp, double windSpeed, double dewPoint, double wc, double cb);
void output_warning(ofstream& out_stream, double temp, double windSpeed);

int main()
{
    ifstream in_stream;
    ofstream out_stream;

    double temp, windSpeed, dewPoint, wc, cb;

    open_files(in_stream, out_stream);

    output_header(out_stream);

    while (in_stream)
    {
        in_stream >> temp >> windSpeed >> dewPoint;

        wc = wind_chill(temp, windSpeed);

        cb = cloud_base(temp, dewPoint);

        output_data(out_stream, temp, windSpeed, dewPoint, wc, cb);

    }

    output_warning(out_stream, temp, windSpeed);

    in_stream.close();
    out_stream.close();

    return 0;
}

void output_warning(ofstream& out_stream, double temp, double windSpeed)
{
    if (temp > 50 || (windSpeed < 3 && windSpeed > 0))
    {
        cout <<"\n*** There is no wind chill factor at this temperature or wind speed. ***" << endl;
        cout << "\n    *** Temperature must be 50 degrees  or less, and wind speed" << endl;
        cout << "               must be > 3 mph to compute  wind chill." << endl;

        out_stream <<"\n*** There is no wind chill factor at this temperature or wind speed. ***" << endl;
        out_stream << "\n    *** Temperature must be 50 degrees  or less, and wind speed" << endl;
        out_stream << "               must be > 3 mph to compute  wind chill." << endl;
    }
}
Last edited on
Check the condition inside the loop and use a variable to keep track of if it turns out true for at least one of the inputs. Check the variable after the loop to decide if you should show the warning or not.
Very good idea! I added a few lines. A bool variable in main. An if statement after the loop with output_warning. Finally setting the bool variable to true in the condition statement I want to trigger the output_warning in the output_data function.

I gave it a shot and it does work but I'm getting 2 warnings.

First I'm getting warning: using the result of an assignment as a condition without parentheses [-Wparentheses]

On line 39:
if (warn = true)

Second is unused variable 'warn' line 63:
bool warn = true;

Here's the full code to see what I did. I'm new and I tried to replicate what you said so it's probably not fullproof.

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <cstdlib>

using namespace std;

void open_files(ifstream& in_stream, ofstream& out_stream);
double wind_chill(double temp, double windSpeed);
double cloud_base(double temp, double dewPoint);
void output_header(ofstream& out_stream);
void output_data(ofstream& out_stream, double temp, double windSpeed, double dewPoint, double wc, double cb);
void output_warning(ofstream& out_stream, double temp, double windSpeed);

int main()
{
    ifstream in_stream;
    ofstream out_stream;

    double temp, windSpeed, dewPoint, wc, cb;
    bool warn;
    open_files(in_stream, out_stream);

    output_header(out_stream);

    while (in_stream)
    {
        in_stream >> temp >> windSpeed >> dewPoint;

        wc = wind_chill(temp, windSpeed);

        cb = cloud_base(temp, dewPoint);

        output_data(out_stream, temp, windSpeed, dewPoint, wc, cb);

    }

    if (warn = true)
    {
        output_warning(out_stream, temp, windSpeed);
    }

    in_stream.close();
    out_stream.close();

    return 0;
}
void output_data(ofstream& out_stream, double temp, double windSpeed, double dewPoint, double wc, double cb)
{
    cout << fixed << setprecision(1);
    out_stream << fixed << setprecision(1);

    if ((temp > 0.0 && temp < 50.0) && windSpeed > 3.0)
    {
        cout << setw(7) << temp << " dF "<< setw(10) << windSpeed << " mph " << setw(12) << dewPoint << " dF " << setw(13) << wc << " dF " << setw(13) <<cb << " ft " << endl;

        out_stream << setw(7) << temp << " dF "<< setw(10) << windSpeed << " mph " << setw(12) << dewPoint << " dF " << setw(13) << wc << " dF " << setw(13) <<cb << " ft " << endl;
    }

    else if (temp > 50.0 || (windSpeed <=3 && windSpeed > 0))
    {
        bool warn = true;
        wc = wind_chill(temp, windSpeed);
        cb = cloud_base(temp, dewPoint);

        cout << setw(7) << temp << " dF "<< setw(10) << windSpeed << " mph " << setw(12) << dewPoint << " dF " << setw(13) << "***" << setw(17) <<cb << " ft " << endl;

        out_stream << setw(7) << temp << " dF "<< setw(10) << windSpeed << " mph " << setw(12) << dewPoint << " dF " << setw(13) << "***" << setw(17) <<cb << " ft " << endl;
    }
}

void output_warning(ofstream& out_stream, double temp, double windSpeed)
{

        cout <<"\n*** There is no wind chill factor at this temperature or wind speed. ***" << endl;
        cout << "\n    *** Temperature must be 50 degrees  or less, and wind speed" << endl;
        cout << "               must be > 3 mph to compute  wind chill." << endl;

        out_stream <<"\n*** There is no wind chill factor at this temperature or wind speed. ***" << endl;
        out_stream << "\n    *** Temperature must be 50 degrees  or less, and wind speed" << endl;
        out_stream << "               must be > 3 mph to compute  wind chill." << endl;

}
Last edited on
Line 22: You've forgot to initialize warn.
Line 39: To compare you need to use the == operator.
Line 63: You are declaring a local variable named warn which is not the same warn in main.
Made the changes and compiling with no errors or warnings. Thank you very much!
Topic archived. No new replies allowed.