im making a program where i will displaying the sales of each store using a sales bar chart.

user will input sales for each store.


after the input, a sales chart must be displayed using a for loop, with each '*' = $100 in sales

question: how can i make the * worth $100 so when ie: sales are $400 for the day, 4 * will be displayed? this is the trouble i am having with my for loop!

this is what i have so far.

#include <iostream>
using namespace std;

int main()
{
	double sale1, sale2, sale3, sale4, sale5;
	double total;
	int row;

	cout << "Enter today's sale for store 1: ";
	cin >> sale1;
	cout << "Enter today's sale for store 2: ";
	cin >> sale2;
	cout << "Enter today's sale for store 3: ";
	cin >> sale3;
	cout << "Enter today's sale for store 4: ";
	cin >> sale4;
	cout << "Enter today's sale for store 5: ";
	cin >> sale5;

	cout << endl;
	cout << "SALES BAR CHART" << endl;

	for (row = 0; row < 5; row++)
	{

	cout << '*';	
	cout << endl;

	}




	return 0;
}

thanks in advance!!

So you want to print '*' n times, where n is a number of hundreds of dollars?
$300 - n = 3
$630 - n = 6
$690 - n = 6? (or n = 7?)

If so, divide each sale by n and store it in int (in that way $630 will end up being 6.3, but stored in int: 6!)

Then add another loop inside row-loop that will print cout<<'*' for n times.
Like this pseudocode:

for rows loop
     for n loop
          print *
     end n loop
     print newline
end rows loop

i followed your advice, but i feel like this code has some errors:

cout << "SALES BAR CHART" << endl;

	for (row = 0; row < 5; row++);
	{

	cout << '*';	
	cout << endl;

	}

	for (n = 100)
	{
	
	sale1 /= n;
	sale2 /= n;
	sale3 /= n;
	sale4 /= n;
	sale5 /= n;


	}

btw this is how it should be on the console

store 1: $500
store 2: $600

chart:
(each * = $100)
store 1: *****
store 2: ******

etc.

No, no...
n := sale/100;
And then INSIDE for loop that counts from 0 to n insert cout<<'*';

i followed your advice, but i feel like this code has some errors:

cout << "SALES BAR CHART" << endl;

for (row = 0; row < 5; row++);
    {

    cout << '*';  
    cout << endl;

    }[/B][/ICODE]

    for (n = 100)
    {

    sale1 /= n;
    sale2 /= n;
    sale3 /= n;
    sale4 /= n;
    sale5 /= n;


    }

is the text in bold right??

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Using dynamic memory allocation, you can come up with this:

#include <iostream>
#include <alloc.h>

using namespace std;

int main()
{
  int nstores, *sales;

  cout << "SALES BAR CHART" << endl;
  cout << "---------------" << endl;
  cout << "Enter the number of stores: ";
  cin >> nstores;

  sales = (int *) malloc(sizeof(int)*nstores);

  for(int i=0; i<nstores; i++)
  {
    cout << "Enter today's sales for store number " << i+1 << ": ";
    cin >> sales[i];
    sales[i] /= 100;
  }
  cout << "\n\n";
  for( int i=0; i<nstores; i++)
  {
    cout << "Store " << i+1 << ": ";
    for (int j=0; j<sales[i]; j++)
      cout << '*';
    cout << "\n";
  }
  system("pause>nul");
  return 0;
}

It's 3:18am and I'm tired. PM me if you have any questions. I don't have problems adding you to msn either.

If we want to go dynamic, why not use new instead of malloc()? :)

Didn't I do that?
I'm just a student as well, if you know of a different way to do that dynamically please post it and explain the differences.
afg, do you need help understanding dynamic memory allocation? I'm not a master on the subject but I know the basics ;) I can make a chart to explain it to you just like my teacher did =3

Ok, so this is the type of solution afg was looking for, with no arrays or dynamic allocation.

#include <iostream>

using namespace std;

int main()
{
  int sale1, sale2, sale3, sale4, sale5;

  cout << "Enter today's sale for store 1: ";
  cin >> sale1;
  sale1 /= 100;

  cout << "Enter today's sale for store 2: ";
  cin >> sale2;
  sale2 /= 100;

  cout << "Enter today's sale for store 3: ";
  cin >> sale3;
  sale3 /= 100;

  cout << "Enter today's sale for store 4: ";
  cin >> sale4;
  sale4 /= 100;

  cout << "Enter today's sale for store 5: ";
  cin >> sale5;
  sale5 /= 100;

  cout << endl;
  cout << "SALES BAR CHART" << endl;
  cout << "---------------" << endl;

  cout << "Store 1: ";
  int row;
  for ( row = 0; row < sale1; row++)
    cout << '*';

  cout << "\nStore 2: ";
  for ( row = 0; row < sale2; row++)
    cout << '*';

  cout << "\nStore 3: ";
  for (int row = 0; row < sale3; row++)
    cout << '*';

  cout << "\nStore 4: ";
  for (int row = 0; row < sale4; row++)
    cout << '*';

  cout << "\nStore 5: ";
  for (int row = 0; row < sale5; row++)
    cout << '*';

//cin.get();
//cin.ignore();
  system("pause>nul");
  return 0;
}

You can either use system("pause>nul"); alone to pause the program, or use both cin.get(); cin.ignore(); together to achieve the same result.

thanks!
one more question! (sorry!!!)

if the user inputs like 990 in sales, and you want to round up to 1000 and show 10 * instead of 9, would you use the double instead of int? and what would the same thing apply if the user were to enter a decimal value? (must be able to show dollars and cents)

This article has been dead for over six months. Start a new discussion instead.