c++ Temperature bar chart

Pages: 12
Can anyone help me out with this problem

This is what I have so far:
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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>

using namespace std;

int main()
{
//Variable declarations
    int count;
    float temperature;
float stars;
    string name;
    cout << "Enter You First Name Please: " << endl;
    cin >> name;
    cout << name << "'s bar chart: " << endl;
    
      ifstream inputFile ("temperatures.txt");
      if(inputFile.fail())
          cout<<"error";
      inputFile.close();
      return 0;

  }







How do I make this code print out the low and high and make it proportionate to the stars?
 

There is 2 different outputs if you could help me with both:








NAME's 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)


Last edited on
what do you want, exactly?
you may want to widen the line number so 1, 10, 100, and 1000 are all the same width.
but I have no idea what you want with the stars. you can do that to the temps too, so 0, -1, 113, and -42 are all the same width. are the number of stars between each the same? If not, are you trying to control the # of stars? The stars seem to come from the file.
The indentations aren't working on the output but the line is proportionate to the temperature. It is shifted to the right based on the high temperature


You say this, but your code doesn't do anything.
 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)
yea I thought about that, but if you do that, the high-low should relate to # of stars, but it doesn't seem to.
@jonnin, the number of stars in each case reflects the difference between high and low temperatures. (It was high-low+1 in the code I used to draw that.) I positioned the first star to reflect the low temperature on the same scale, but I've no idea if that is what the OP wanted.

I think the OP should start by proving that he/she can read from file, before they see stars before their eyes.

As @TheIdeasMan points out, the OP's code doesn't actually DO anything.
Last edited on
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
#include <iostream>
#include <string>
#include <iomanip>

#include <fstream>

using namespace std;

int main()
{
    int month{0}, low_temp{0}, high_temp{0}, range{0};
    
    ifstream inputFile ("temperatures.txt");
    
    if(!inputFile)
    {
        cout << "Error\n";
        return -99;
    }
    else
    {
        while(inputFile >> month >> low_temp >> high_temp)
        {
            range = high_temp - low_temp;
            string str_range (range, '*');
            
            string str_start(low_temp, ' ');
            
            cout
            << str_start
            << '(' << low_temp << " F) "
            << str_range
            << " (" << high_temp << " F) "
            << '\n';
        }
    }
    
    inputFile.close();
    
    return 0;
    
}



                                         (41 F) ********************** (63 F) 
                                              (46 F) ********************* (67 F) 
                                                     (53 F) ********************** (75 F) 
                                                           (59 F) ********************** (81 F) 
                                                                   (67 F) ******************** (87 F) 
                                                                          (74 F) ******************** (94 F) 
                                                                            (76 F) ********************** (98 F) 
                                                                            (76 F) ************************ (100 F) 
                                                                        (72 F) ********************* (93 F) 
                                                             (61 F) ********************** (83 F) 
                                                  (50 F) ********************** (72 F) 
                                            (44 F) ******************** (64 F) 
Program ended with exit code: 0



1 41 63
2 46 67
3 53 75
4 59 81
5 67 87
6 74 94
7 76 98
8 76 100
9 72 93
10 61 83
11 50 72
12 44 64
Thanks so much guys
closed account (9G8M4G1T)
how would you output this?


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
New York 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   **
  40 F
 TempF    1   2   3   4   5   6   7   8   9  10  11  12 month



if text file is

41 63
46 67
53 75
59 81
67 87
74 94
76 98
76 100
72 93
61 83
50 72
44 64
Last edited on
KENnIG1591,
Apart from retrospectively changing your original post, all you have done is repeat two of the outputs that you have been given.

Let's see your code - you can start by proving that you can read a file.
If it's like this then even though it's a bit fiddly it's pretty easy with strings and stuff. First step is read the file. Get that right and the rest follows. You don't have to but I used a struct with a range method.

 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
Program ended with exit code: 0

Edit: Oops, forgot the axis labels, stole them from the sample - just a simple std::cout, another easy bit.
Last edited on
closed account (9G8M4G1T)
@againtry sorry I don't think I'm understanding could you show me the code if you have it
closed account (9G8M4G1T)
Im using Xcode to execute this but it won't open up my text file. can anyone help me out with this please?

This is due tomorrow for me

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

using namespace std;

int main()
{
    string name;
    cout << "Enter You First Name Please: " << endl;
        cin >> name;
        cout << name << "'s bar chart: " << endl;
    
    int month{0}, low_temp{0}, high_temp{0}, range{0};
    
    ifstream inputFile ("temperatures.txt");
    
    if(!inputFile)
    {
        cout << "Error\n";
        return -99;
    }
    else
    {
        while(inputFile >> month >> low_temp >> high_temp)
        {
            range = high_temp - low_temp;
            string str_range (range, '*');
            
            string str_start(low_temp, ' ');
            
            cout
            << str_start
            << '(' << low_temp << " F) "
            << str_range
            << " (" << high_temp << " F) "
            << '\n';
        }
    }
    
    inputFile.close();
    
    return 0;
    
}


//desired output
 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


Of course I have the code, but I think it is your responsibility to show us what you have done.
My code is simple and with some careful consideration can be based on the horizontal example, albeit tedious.
Each item is constructed as a fixed length string, stored in a vector of strings, blank characters are significant, then print the strings. It’s similar to displaying a 2D array.
@againtry can you help me out with figuring out how you are getting the code to open up the text file? I'm using the code you provided to try and get the output but its only giving me the error message. I've tried using the pathname but its still not working so I can't even see the current output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
this is what my text file has:
New York's Average Monthly low high Temperatures (Fahrenheit) Years 2010 - 2019
41 63
46 67
53 75
59 81
67 87
74 94
76 98
76 100
72 93
61 83
50 72
44 64 
Last edited on
You probably need to set the working directory in Xcode
See http://www.cplusplus.com/forum/beginner/279941/#msg1210090
Ahh ok. I tried those options and still just got the error message. ..
What error message?
You code does the horizontal bar chart. Quite successfully without errors, and using Xcode.
So this is the only thing that it'll output:
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
/* test output:
Enter You First Name Please: 
test
test's bar chart: 
Program ended with exit code: 0
*/

with this code:

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

using namespace std;

int main()
{
    string name;
        cout << "Enter You First Name Please: " << endl;
        cin >> name;
        cout << name << "'s bar chart: " << endl;
    
    int month{0}, low_temp{0}, high_temp{0}, range{0};
        
        ifstream inputFile ("temperatures.txt");
        if(!inputFile)
        {
            cout << "Error. Your file could not be found.\n";
            return -99;
        }
        else
        {
            while(inputFile >> month >> low_temp >> high_temp)
            {
                range = high_temp - low_temp;
                string str_range (range, '*');
                string str_start(low_temp, ' ');
                cout
                << str_start
                << '(' << low_temp << " F) "
                << str_range
                << " (" << high_temp << " F) "
                << '\n';
            }
        }
        inputFile.close();
        return 0;
    }
Last edited on
Pages: 12