Click here to Skip to main content
15,896,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Write a program to read the data from this file and display a temperature bar chart. The program should: 
1) Display a short message from the programmer with the programmer's name, describing what is about to display. For example, if your name is Whippersnapper and you are producing a bar chart, begin with the message: Whippersnapper's bar chart: 
2) Attempt to open the file lohiF.txt mentioned above. If the file is not found, handle the situation appropriately with a good error message, including the name of the file not found, then stop. Do not attempt to display a chart if no data is available. An even better error message might include a short description of the expected file format. 
3) If the file opens OK, read the first full line of the file. This line contains a brief description of the data in the file. Above the chart you are about to make, display the brief description you read from the file so the user knows what this chart represents. 
4) Use all the data in the file to display a bar chart showing temperature ranges for 12 months. 
5) Make the program more flexible: read low, high temperature pairs from the file until end-of-file (instead of exactly 12 times). It should work if there are more or less than 12 months of temperature data. Handle temperature values until end of file.

The program should contain test results at the bottom in a comment. This particular bar chart shifts the values to the right, assuming that 32 F, freezing, is at the far left. You can modify this behavior if you like, but you should at least show the range from low to high temps in the file. 
The program can assume: 1) The first line of the file has a string that describes the data: 2) Temperature values are within the range of -999 to +999 degrees. 3)At least 1 month of temperature values are provided. 4)No more than 24 months of temperature data. 5)The temperatures start with month 1, January. 6)Temperature values are provided in pairs: low-temp high-temp with whitespace between temps. 6) It is possible the file may not be not found.
The text in the file as:-
Austin 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

The output should look like this one.
Austin Average Monthly low high Temperatures (Fahrenheit) Years 2010 - 2019
1           (41F)********************(63F)
2              (46F)********************(67F)
3                  (53F)********************(75F)
4                    (59F)********************(81F)
5                        (67F)********************(87F)
6                            (74F)********************(94F)
7                              (76F)********************(98F)
8                              (76F)********************(100F)
9                           (72F)********************(93F)
10                      (61F)********************(83F)
11               (50F)********************(72F)
12           (44F)********************(64F)


What I have tried:

<pre lang="C++"><pre>
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream> // provides both ifstream and ofstream
using namespace std;

int main() {
	
		cout <<"Whippersnapper's bar chart: \n"<< endl;
	string inFileName = "lohiF.txt";
	ifstream inFile{inFileName};
	if (inFile){
		string first_line;
		getline(inFile, first_line);
		cout<< first_line <<endl;
				
		for(int i=1;i<=13; i++){
		    
		    int variable,values ;
			inFile >> variable>>values;
				
			{  for (int star = 0; star < 22; star++)
  			 {    cout <<'*';
  				 }
   				cout << endl;}
			for (int row = 0; row < 13; row+=5)
			{  for (int star = 0; star < 22; star+=5)
  			 {    cout <<"  ";
   }			
            int variable,values ;
			inFile >> variable>>values;
}
			//int variable,values ;
			inFile >> variable>>values;
			cout << i <<setw(5)<<variable << setw(5)<<values<<endl;	
		}
	}
	else{
		cout <<"lohiF.txt "<<"Error on file open\n"<< endl;
	}
	return 0;	
}
Posted
Updated 17-Jun-21 2:05am
v3
Comments
OriginalGriff 17-Jun-21 5:54am    
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?
What help do you need?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
bhushan7 17-Jun-21 6:38am    
I just update the Description, Please check.
Richard MacCutchan 17-Jun-21 6:46am    
Check what? You have still not explained what your problem is.
Richard MacCutchan 17-Jun-21 6:45am    
If you look at the sample results it becomes clear that each degree over 32F represents a character position in the bar chart. So if the lowest value is 41 then start by printing spaces for each degree between 32 and 41. Next print the actual low temperature value in parentheses. Next print an asterisk for every degree between the lowest and highest temperatures. Finally print the high temperature in parentheses.
bhushan7 17-Jun-21 7:36am    
As per your help, I reach the answer, I still need the if-else condition for space between 32F.

Some things to think about:
1. The months will count from 1 to 12 for each year, but you do not know how many months will be in the file. So creating a loop that counts from 1 to 12 may not work. A better idea is a loop that continues until the input file returns an end of file status.

2. Use meaningful names for all variables so your code is readable. And also you can easily understand the purpose of it. In your case "variable" and "values" do not make it clear what they are used for. I used "lowTemp" and "hiTemp" for clarity.

3. Read the temperature values once only for each iteration of the main loop.

4. Your print statements need to print a number of characters based on two known values. So make use of those values to control your loops. For example to print the spaces:
C++
for (int space = 32; space < lowTemp; space++)
{
    cout ' ';
}

For the asterisks:
C++
for (int star = lowTemp; star < hiTemp; star++)
{
    cout '*';
}
 
Share this answer
 
v2
Quote:
Can you please type in the code?

No. While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
bhushan7 17-Jun-21 7:51am    
You are right sir, I need to learn. I am working on that.
I just posted my updated program please check.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900