charting using for loops??

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 55
Reputation: afg_91320 is an unknown quantity at this point 
Solved Threads: 0
afg_91320 afg_91320 is offline Offline
Junior Poster in Training

charting using for loops??

 
0
  #1
Oct 11th, 2008
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.

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. double sale1, sale2, sale3, sale4, sale5;
  7. double total;
  8. int row;
  9.  
  10. cout << "Enter today's sale for store 1: ";
  11. cin >> sale1;
  12. cout << "Enter today's sale for store 2: ";
  13. cin >> sale2;
  14. cout << "Enter today's sale for store 3: ";
  15. cin >> sale3;
  16. cout << "Enter today's sale for store 4: ";
  17. cin >> sale4;
  18. cout << "Enter today's sale for store 5: ";
  19. cin >> sale5;
  20.  
  21. cout << endl;
  22. cout << "SALES BAR CHART" << endl;
  23.  
  24. for (row = 0; row < 5; row++)
  25. {
  26.  
  27. cout << '*';
  28. cout << endl;
  29.  
  30. }
  31.  
  32.  
  33.  
  34.  
  35. return 0;
  36. }

thanks in advance!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: charting using for loops??

 
0
  #2
Oct 11th, 2008
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:
  1. for rows loop
  2. for n loop
  3. print *
  4. end n loop
  5. print newline
  6. end rows loop
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 55
Reputation: afg_91320 is an unknown quantity at this point 
Solved Threads: 0
afg_91320 afg_91320 is offline Offline
Junior Poster in Training

Re: charting using for loops??

 
0
  #3
Oct 11th, 2008
thanks

so n should be = 100, making the statement true???
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 55
Reputation: afg_91320 is an unknown quantity at this point 
Solved Threads: 0
afg_91320 afg_91320 is offline Offline
Junior Poster in Training

Re: charting using for loops??

 
0
  #4
Oct 11th, 2008
i followed your advice, but i feel like this code has some errors:

  1. cout << "SALES BAR CHART" << endl;
  2.  
  3. for (row = 0; row < 5; row++);
  4. {
  5.  
  6. cout << '*';
  7. cout << endl;
  8.  
  9. }
  10.  
  11. for (n = 100)
  12. {
  13.  
  14. sale1 /= n;
  15. sale2 /= n;
  16. sale3 /= n;
  17. sale4 /= n;
  18. sale5 /= n;
  19.  
  20.  
  21. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 55
Reputation: afg_91320 is an unknown quantity at this point 
Solved Threads: 0
afg_91320 afg_91320 is offline Offline
Junior Poster in Training

Re: charting using for loops??

 
0
  #5
Oct 11th, 2008
btw this is how it should be on the console

store 1: $500
store 2: $600

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

etc.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: charting using for loops??

 
0
  #6
Oct 12th, 2008
No, no...
n := sale/100;
And then INSIDE for loop that counts from 0 to n insert cout<<'*';
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 55
Reputation: afg_91320 is an unknown quantity at this point 
Solved Threads: 0
afg_91320 afg_91320 is offline Offline
Junior Poster in Training

Re: charting using for loops??

 
0
  #7
Oct 12th, 2008
how do i do that??
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 55
Reputation: afg_91320 is an unknown quantity at this point 
Solved Threads: 0
afg_91320 afg_91320 is offline Offline
Junior Poster in Training

Re: charting using for loops??

 
0
  #8
Oct 12th, 2008
Originally Posted by afg_91320 View Post
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; }
is the text in bold right??
Last edited by afg_91320; Oct 12th, 2008 at 9:56 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 32
Reputation: emotionalone is an unknown quantity at this point 
Solved Threads: 4
emotionalone emotionalone is offline Offline
Light Poster

Re: charting using for loops??

 
0
  #9
Oct 13th, 2008
Using dynamic memory allocation, you can come up with this:
  1. #include <iostream>
  2. #include <alloc.h>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int nstores, *sales;
  9.  
  10. cout << "SALES BAR CHART" << endl;
  11. cout << "---------------" << endl;
  12. cout << "Enter the number of stores: ";
  13. cin >> nstores;
  14.  
  15. sales = (int *) malloc(sizeof(int)*nstores);
  16.  
  17. for(int i=0; i<nstores; i++)
  18. {
  19. cout << "Enter today's sales for store number " << i+1 << ": ";
  20. cin >> sales[i];
  21. sales[i] /= 100;
  22. }
  23. cout << "\n\n";
  24. for( int i=0; i<nstores; i++)
  25. {
  26. cout << "Store " << i+1 << ": ";
  27. for (int j=0; j<sales[i]; j++)
  28. cout << '*';
  29. cout << "\n";
  30. }
  31. system("pause>nul");
  32. return 0;
  33. }

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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: charting using for loops??

 
0
  #10
Oct 13th, 2008
If we want to go dynamic, why not use new instead of malloc()?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC