943,782 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1208
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 11th, 2008
0

charting using for loops??

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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!!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
afg_91320 is offline Offline
55 posts
since Feb 2008
Oct 11th, 2008
0

Re: charting using for loops??

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:
C++ Syntax (Toggle Plain Text)
  1. for rows loop
  2. for n loop
  3. print *
  4. end n loop
  5. print newline
  6. end rows loop
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 11th, 2008
0

Re: charting using for loops??

thanks

so n should be = 100, making the statement true???
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
afg_91320 is offline Offline
55 posts
since Feb 2008
Oct 11th, 2008
0

Re: charting using for loops??

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

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
afg_91320 is offline Offline
55 posts
since Feb 2008
Oct 11th, 2008
0

Re: charting using for loops??

btw this is how it should be on the console

store 1: $500
store 2: $600

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

etc.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
afg_91320 is offline Offline
55 posts
since Feb 2008
Oct 12th, 2008
0

Re: charting using for loops??

No, no...
n := sale/100;
And then INSIDE for loop that counts from 0 to n insert cout<<'*';
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 12th, 2008
0

Re: charting using for loops??

how do i do that??
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
afg_91320 is offline Offline
55 posts
since Feb 2008
Oct 12th, 2008
0

Re: charting using for loops??

Click to Expand / Collapse  Quote originally posted by afg_91320 ...
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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
afg_91320 is offline Offline
55 posts
since Feb 2008
Oct 13th, 2008
0

Re: charting using for loops??

Using dynamic memory allocation, you can come up with this:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 4
Light Poster
emotionalone is offline Offline
33 posts
since Oct 2008
Oct 13th, 2008
0

Re: charting using for loops??

If we want to go dynamic, why not use new instead of malloc()?
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Jiberish Visual C++ Errors Un-Understandable
Next Thread in C++ Forum Timeline: Template problem - error status -1073741819





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC