c++ Temperature bar chart

Pages: 12
So why have you changed the name of the file? Originally you call it temperatures.txt and now its lohif.txt

Also if you set the working directory properly you don't need the full path, thus reducing the possibility of naming errors.
I renamed my file to try and fix the issues but that did not help.

I have changed it back but the only problem I'm having is that the code will not open my text file. When I set the working directory properly how I've been told it says "The working directory "temperatures.txt" for scheme "lab" doesn't exist."
Finally got the code working. Can anyone help me with the last thing which is to print the numbers 1-12 to the left of my output i don't know what to change so its not just 1's?



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

using namespace std;

int main()
{
    int
    low_temp,
    high_temp;
    string title_line;
    ifstream temp_file{"lohiF.txt"};
    if (temp_file){
        getline(temp_file, title_line);
        cout << "TEST's bar chart: " << endl;
        cout <<title_line<<endl;

        while(temp_file>>low_temp>>high_temp){
            int input = 1;
                while (input <= 1) {
                    std::cout << input;
                    input++;
            int space_num = (low_temp - 32);
            int stars = (high_temp - low_temp);
            
            cout <<string (space_num,' ')
                 << "("<<low_temp<<"F)"
                 << string (stars,'*')
                 << "(" <<high_temp<< "F)"<<endl;
            if(temp_file.eof())
                break;
                }}
    }
    else{
        cout << "Error. Your file could not be found.\n";
        exit(-99);
    }
}


My output:
TEST's bar chart: 
Austin Average Monthly low high Temperatures (Fahrenheit) Years 2010 - 2019
1         (41F)**********************(63F)
1              (46F)*********************(67F)
1                     (53F)**********************(75F)
1                           (59F)**********************(81F)
1                                   (67F)********************(87F)
1                                          (74F)********************(94F)
1                                            (76F)**********************(98F)
1                                            (76F)************************(100F)
1                                        (72F)*********************(93F)
1                             (61F)**********************(83F)
1                  (50F)**********************(72F)
1            (44F)********************(64F)
Program ended with exit code: 0 
Last edited on
line 21 is in the wrong place. That's why it will always print out 1.
is the code correct and I just need to move it?
try it
1
2
3
4
int input = 1; // THIS IS IN THE WRONG SPOT
                while (input <= 1) { // WHAT IS THIS LINE AND CORRESPONDING } FOR?
                    std::cout << input;
                    input++;

is this a better way to print the list?
I might be looking at the wrong way to do this because if there's less data in the text file it should only number that amount

The only thing is that this prints all 12 numbers every single time and I don't know how to make it so it only prints every number once

1
2
3
            std::list<int> listOfNumbers = {1, 2, 3, 4,5,6,7,8,9,10,11,12};
            for (int item : listOfNumbers)
              std::cout << item << "\n";
Last edited on
Why not just print the current value of 'input' ? No need for a list and for loop at all. Let the machine do the counting.

Or better still use the first column from the data file!
Been looking up different solutions and can't figure it out or what to look up for help.
Just gonna submit it now thanks for your help
1
2
3
4
5
6
7
8
cout << "TEST's bar chart: " << endl;
        cout <<title_line<<endl;
    int input = 1;
        while(temp_file>>low_temp>>high_temp){
            
                while (input <= 1) { // and corresponding }
                    std::cout << std::setw(2) << input;
                    input++;
For a horizontal chart, perhaps:

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

constexpr const char* fname {"lohiF.txt"};

int main() {
	std::ifstream temp_file {fname};
	if (!temp_file)
		return (std::cout << "Cannot open file\n"), 1;

	std::string title_line;

	std::getline(temp_file, title_line);
	std::cout << fname << " bar chart:\n" << title_line << '\n';

	for (int low_temp {}, high_temp, row {1}; temp_file >> low_temp >> high_temp; ++row) {
		const auto space_num {low_temp - 32};
		const auto stars {high_temp - low_temp};

		std::cout << row << std::string(space_num, ' ')
			<< "(" << low_temp << " F)"
			<< std::string(stars, '*')
			<< "(" << high_temp << " F)\n";
	}
}



lohiF.txt bar chart:
New York's Average Monthly low high Temperatures (Fahrenheit) Years 2010 - 2019
1         (41 F)**********************(63 F)
2              (46 F)*********************(67 F)
3                     (53 F)**********************(75 F)
4                           (59 F)**********************(81 F)
5                                   (67 F)********************(87 F)
6                                          (74 F)********************(94 F)
7                                            (76 F)**********************(98 F)
8                                            (76 F)************************(100 F)
9                                        (72 F)*********************(93 F)
10                             (61 F)**********************(83 F)
11                  (50 F)**********************(72 F)
12            (44 F)********************(64 F)

For a vertical chart, perhaps:

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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <vector>

constexpr const char* fname {"lohiF.txt"};

struct Temp {
	int mint {};
	int maxt {};
};

int main() {
	std::ifstream temp_file {fname};

	if (!temp_file)
		return (std::cout << "Cannot open file\n"), 1;

	std::string title_line;

	std::getline(temp_file, title_line);
	std::cout << fname << " bar chart:\n" << title_line << '\n';

	std::vector<Temp> temps {};
	int mint {200}, maxt {-200};

	for (int mt {}, mxt {}; temp_file >> mt >> mxt; temps.emplace_back(mt, mxt)) {
		if (mt < mint)
			mint = mt;

		if (mxt > maxt)
			maxt = mxt;
	}

	for (int r = maxt; r >= mint; --r) {
		std::cout << std::setw(3) << r << " F  ";

		for (const auto& [tmn, tmx] : temps)
			std::cout << (r <= tmx && r >= tmn ? "**" : "  ") << "  ";

		std::cout << '\n';
	}

	std::cout << "TempF";

	for (int m = 1; m <= temps.size(); ++m)
		std::cout << std::setw(4) << m;

	std::cout << " month\n";
}



lohiF.txt bar chart:
New York's Average Monthly low high Temperatures (Fahrenheit) Years 2010 - 2019
100 F                              **
 99 F                              **
 98 F                          **  **
 97 F                          **  **
 96 F                          **  **
 95 F                          **  **
 94 F                      **  **  **
 93 F                      **  **  **  **
 92 F                      **  **  **  **
 91 F                      **  **  **  **
 90 F                      **  **  **  **
 89 F                      **  **  **  **
 88 F                      **  **  **  **
 87 F                  **  **  **  **  **
 86 F                  **  **  **  **  **
 85 F                  **  **  **  **  **
 84 F                  **  **  **  **  **
 83 F                  **  **  **  **  **  **
 82 F                  **  **  **  **  **  **
 81 F              **  **  **  **  **  **  **
 80 F              **  **  **  **  **  **  **
 79 F              **  **  **  **  **  **  **
 78 F              **  **  **  **  **  **  **
 77 F              **  **  **  **  **  **  **
 76 F              **  **  **  **  **  **  **
 75 F          **  **  **  **          **  **
 74 F          **  **  **  **          **  **
 73 F          **  **  **              **  **
 72 F          **  **  **              **  **  **
 71 F          **  **  **                  **  **
 70 F          **  **  **                  **  **
 69 F          **  **  **                  **  **
 68 F          **  **  **                  **  **
 67 F      **  **  **  **                  **  **
 66 F      **  **  **                      **  **
 65 F      **  **  **                      **  **
 64 F      **  **  **                      **  **  **
 63 F  **  **  **  **                      **  **  **
 62 F  **  **  **  **                      **  **  **
 61 F  **  **  **  **                      **  **  **
 60 F  **  **  **  **                          **  **
 59 F  **  **  **  **                          **  **
 58 F  **  **  **                              **  **
 57 F  **  **  **                              **  **
 56 F  **  **  **                              **  **
 55 F  **  **  **                              **  **
 54 F  **  **  **                              **  **
 53 F  **  **  **                              **  **
 52 F  **  **                                  **  **
 51 F  **  **                                  **  **
 50 F  **  **                                  **  **
 49 F  **  **                                      **
 48 F  **  **                                      **
 47 F  **  **                                      **
 46 F  **  **                                      **
 45 F  **                                          **
 44 F  **                                          **
 43 F  **
 42 F  **
 41 F  **
TempF   1   2   3   4   5   6   7   8   9  10  11  12 month

Topic archived. No new replies allowed.
Pages: 12