Trying to read the file and tokenize each string to produce a bar chart

I have this .txt file. I want to extract each element that is separated by a space. For example, in line one I want to be able to extract "41" "63" as separate tokens to make a bar chart using *. The asterisk would represent the difference between the low to high temperature.

1
2
3
4
5
6
7
8
9
10
11
12
41 63
46 67
53 75
59 81
67 87
74 94
76 98
76 100
72 93
61 83
50 72
44 64


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

int main(){
    string line;
    ifstream temp_file;
    int months=-1;
    
    
    temp_file.open("temperature.txt");
    
    while(getline(temp_file,line)){
        months+=1;
        cout<<months<<right<<setw(30)<<line<<"\n"<<endl;
    }
    
temp_file.close();
}


This is what I have. it reads the file line by line, and have all the months too, but i don't know how to extract each token to make a bar chart.
Last edited on
Hello techielove,

Try 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
#include <iostream>  // <--- You forgot this 1.
#include <iomanip>
#include <string>

#include <fstream>

using namespace std;

int main()
{
    string line;
    ifstream temp_file;
    int months{};
    int lowTemp{},highTemp{};  // <--- Added.


    temp_file.open("temperature.txt");
    // <--- How do you know it is open and ready to use?

    if (!temp_file)  // <---  Added.
    {
        std::cerr << "\n     File \"temperature.txt\" did not open\n";

        return 1;
    }

    while (temp_file >> lowTemp >> highTemp)  // <--- Changed.
    //while (getline(temp_file, line))
    {
        //months++;  // <--- Changed.

        //cout << months << right << setw(30) << line << "\n" << endl;

        cout << setw(2) << months++ << setw(4) << lowTemp << " " << setw(4) << highTemp << " Difference is: " << highTemp - lowTemp << "\n";
    }

    //temp_file.close();  // <--- Not required as the dtor will close the file when the function looses scope.

    return 0;  // <--- Not required, but makes a good break point for testing.
}

This produces this output:

 0  41   63 Difference is: 22
 1  46   67 Difference is: 21
 2  53   75 Difference is: 22
 3  59   81 Difference is: 22
 4  67   87 Difference is: 20
 5  74   94 Difference is: 20
 6  76   98 Difference is: 22
 7  76  100 Difference is: 24
 8  72   93 Difference is: 21
 9  61   83 Difference is: 22
10  50   72 Difference is: 22
11  44   64 Difference is: 20


This should give you some ideas of where to go from hers.

Andy

Edit:
Last edited on
Do you mean like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <fstream>
#include <string>
#include <iomanip>
#include <iostream>

int main() {
	std::ifstream temp_file("temperature.txt");

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

	unsigned month {1};

	for (unsigned low {}, high {}; temp_file >> low >> high; ++month)
		std::cout << std::setw(2) << month << ": " << std::string(high - low, '*') << '\n';
}



 1: **********************
 2: *********************
 3: **********************
 4: **********************
 5: ********************
 6: ********************
 7: **********************
 8: ************************
 9: *********************
10: **********************
11: **********************
12: ********************

Topic archived. No new replies allowed.