Creating a bar chart in C++

Creating a bar chart in C++

Page 1 of 1

7 Replies - 20081 Views - Last Post: 11 February 2008 - 12:45 PM Rate Topic: -----

#1 tannerprog12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-October 07

Creating a bar chart in C++

Post icon  Posted 10 February 2008 - 10:20 AM

Hi everyone, I am new to C++, and trying to learn it on my own. I would greatly appreciate any help on this issue that I am having. I would like to know how a bar chart can be created in C++, or if this is even possible to do?

For example: My dad and I sell used cars, and for each year since 2003 we would like to track how many vehicles we sold. We are going to pull our data (how many vehicles are sold) from a floppy disk. For each vehicle that we have sold, we would like to represent it with a # sign.

Below is kinda how we picture the output to look like:

2003 #########
2004 ######
2005 ##########
2006 ########
2007 #######
2008

Again, I don't know if this is even possible to do. However, I am hoping that this is possible.

Thanks in advance for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: Creating a bar chart in C++

#2 schnalf  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 129
  • Joined: 09-February 08

Re: Creating a bar chart in C++

Posted 10 February 2008 - 11:25 AM

in witch form do you have the number of the sold cars? do you have to read it from a file first?
when no, where is the problem? you must only print the # so many times how you sold the cars.

for example you sold 2008 12 cars:

int SoldCars2008 = 12;

std::cout << "2008: ";
for(int i = 0; i < SoldCars2008; i++)
{
		std::cout << "#";
}
std::cout << endl;



or i am missunderstanding you?
Was This Post Helpful? 0
  • +
  • -

#3 tannerprog12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-October 07

Re: Creating a bar chart in C++

Posted 10 February 2008 - 07:29 PM

View Posttannerprog12, on 10 Feb, 2008 - 07:19 PM, said:

View Postschnalf, on 10 Feb, 2008 - 11:25 AM, said:

in witch form do you have the number of the sold cars? do you have to read it from a file first?
when no, where is the problem? you must only print the # so many times how you sold the cars.

for example you sold 2008 12 cars:

int SoldCars2008 = 12;

std::cout << "2008: ";
for(int i = 0; i < SoldCars2008; i++)
{
		std::cout << "#";
}
std::cout << endl;



or i am missunderstanding you?



Sorry about that - pressed "add reply" before I was finished. I will be reading the number of cars sold from my file first.

So, for example we sold 22 cars in 2003, 27 cars in 2004, 19 cars in 2005, etc...
Our output would hopefully look like the following:

2003 ######################
2004 ###########################
2005 ###################

Thanks again for any help!
Was This Post Helpful? 0
  • +
  • -

#4 tannerprog12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-October 07

Re: Creating a bar chart in C++

Posted 11 February 2008 - 01:24 AM

I figured out how to do a bar chart, but I have another problem and that is I do not know how to show the correct amount of vehicles sold for each year. The output that I receive for the code below list 2 pound signs for each year, and I know that is not correct. My data file is set up as follows: 22 27 19 24 33 2 (22 is for 2003, 27 for 2004, 19 for 2005, ... and 2 for 2008 so far).

Any help would be appreciated. Thank you in advance.

Here is my code:


#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
	ifstream in_stream;

	in_stream.open("a:\\vehicles.dat");

	int vehicles;

	in_stream >> vehicles;

	//Display the bar chart.
	cout << endl;
	cout << "Tanner's Used Auto Sales\n";
	cout << "(each # will represent 1 vehicle)\n\n";
	cout << setw(4) << "Year" << setw(30) << "Vehicles Sold\n";
	cout << "----------------------------------------\n\n";

	for (int years = 2003; years <= 2008; years++)
	{
		cout << years << "	 ";

		//print pound signs.
		for (int pound = 1; pound < vehicles; pound  = pound  + 1000)
		{
			cout << "#";
		}
		
		cout << endl << endl; //start a new line of output.
	}
	
	//Close the file.
	in_stream.close();

	//End the program.
	return 0;
}



Was This Post Helpful? 0
  • +
  • -

#5 schnalf  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 129
  • Joined: 09-February 08

Re: Creating a bar chart in C++

Posted 11 February 2008 - 09:27 AM

ok, the problem is easy: you read only one time the number of sold vehicles form the vehicles.dat file out and you use it for each year in your bar chart.
the rest of your code seems to be right.
Was This Post Helpful? 0
  • +
  • -

#6 tannerprog12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-October 07

Re: Creating a bar chart in C++

Posted 11 February 2008 - 10:27 AM

How would I make it read the correct number of times then?
Thanks again for the help!
Was This Post Helpful? 0
  • +
  • -

#7 schnalf  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 129
  • Joined: 09-February 08

Re: Creating a bar chart in C++

Posted 11 February 2008 - 10:39 AM

i don't know in witch way you save your datas in your vehicles.dat. do you save the numbers for each year? or do you done the same mistake and you saved only the information from one year?

if you're not sure, post the code where you create the vehicles.dat
Was This Post Helpful? 0
  • +
  • -

#8 tannerprog12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-October 07

Re: Creating a bar chart in C++

Posted 11 February 2008 - 12:45 PM

ok, thank you for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1