-
November 22nd, 2009, 07:27 PM
#1
Bar Charts in C++?
Can somebody help me with making a bar chart in C++ ?
I have to make a bar chart where each * represents 100. It should display like this after the user has inputted the cin for store sales.
Store 1: *******
Store 2: *******
Store 3: *******
Store 4: *******
Store 5: *******
Last edited by Sabensohn70; November 22nd, 2009 at 07:34 PM.
-
November 23rd, 2009, 03:24 AM
#2
Re: Bar Charts in C++?
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
November 23rd, 2009, 03:35 AM
#3
Re: Bar Charts in C++?
Although you could directly use a loop, it may be simpler to just create (and immediately print) a string of asterisks with the desired length, since std::string has a suitable constructor.
-
November 23rd, 2009, 07:17 PM
#4
Re: Bar Charts in C++?
how would i use the for loop for this?
-
November 23rd, 2009, 07:51 PM
#5
Re: Bar Charts in C++?
consider
Code:
for( unsigned int I = 0; I < 10; ++I )
{
cout << "*";
}
http://msdn.microsoft.com/en-us/library/b80153d8.aspx
this is very basic and could be easily found by google. please do some research
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
November 23rd, 2009, 07:57 PM
#6
Re: Bar Charts in C++?
yeah so how would i make it so the users input each represent 100 in the chart?
-
November 23rd, 2009, 08:06 PM
#7
Re: Bar Charts in C++?
i suppose you start with some math. maybe int points = total / 100; ?
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
November 23rd, 2009, 08:11 PM
#8
Re: Bar Charts in C++?
can you please just help me out? I've never learned this material before. Im suppose to make a bar chart where store1=1000, store2= 1200, store3=1800, store4= 800, store5 =1900. and each asterisk will represent 100.
-
November 23rd, 2009, 08:15 PM
#9
Re: Bar Charts in C++?
so far this is what i got..
Code:
#include<iomanip>
using namespace std;
int main()
{
int store1, store2, store3, store4, store5;
cout << "Enter today's sales for store 1.\n";
cout << "Enter a value between 0 and 4000: ";
cin >> store1;
while ( store1 % 100 != 0 || store1 < 0 || store1 > 4000 )
{
cout << "Invalid sales amount: " << store1 << endl;
cout << "Sales must be multiple of 100 in range 0-4000, inclusive.\n";
cout << "Enter today's sales for store 1.";
cin >> store1;
}
cout << "Enter the sales for store 2.";
cin >> store2;
while ( store2 % 100 != 0 || store2 < 0 || store2 > 4000 )
{
cout << "Invalid sales amount: " << store2 << endl;
cout << "Sales must be multiple of 100 in range 0-4000, inclusive.\n";
cout << "Enter today's sales for store 2.";
cin >> store2;
}
cout << "Enter the sales for store 3.";
cin >> store3;
while ( store3 % 100 != 0 || store3 < 0 || store3 > 4000 )
{
cout << "Invalid sales amount: " << store3 << endl;
cout << "Sales must be multiple of 100 in range 0-4000, inclusive.\n";
cout << "Enter today's sales for store 3.";
cin >> store3;
}
cout << "Enter the sales for store 4.";
cin >> store4;
while ( store4 % 100 != 0 || store4 < 0 || store4 > 4000 )
{
cout << "Invalid sales amount: " << store4 << endl;
cout << "Sales must be multiple of 100 in range 0-4000, inclusive.\n";
cout << "Enter today's sales for store 4.";
cin >> store4;
}
cout << "Enter the sales for store 5.";
cin >> store5;
while ( store5 % 100 != 0 || store5 < 0 || store5 > 4000 )
{
cout << "Invalid sales amount: " << store5 << endl;
cout << "Sales must be multiple of 100 in range 0-4000, inclusive.\n";
cout << "Enter today's sales for store 5.";
cin >> store5;
}
i just dont know how to do the bar chart part..
-
November 23rd, 2009, 08:22 PM
#10
Re: Bar Charts in C++?
can you please just help me out?
i have. if i write too much more, i would be handing your assignment in too.
Code:
int points = store4 / 100;
for( unsigned int I = 0; I < 10; ++I )
{
cout << "*";
}
I highlighted the 10... for a reason
so far this is what i got..
that code is redundant
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
November 23rd, 2009, 08:41 PM
#11
Re: Bar Charts in C++?
im thinking for ( store1 = 0; store1 / 100; ++store1 ) ? this doesnt work
-
November 23rd, 2009, 10:37 PM
#12
Re: Bar Charts in C++?
for ( store1 = 0; store1 / 100; ++store1 ) ?
let's think here for a moment. did store1 hold the value for the store 1? if so, you effectively destroyed its value. store1 / 100 isn't right. the first test would equal false since 0 / 100 is 0. ++store1... well would count up if it ran, but when would it stop?
loop up how to use the for statement.....................
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
November 24th, 2009, 01:29 AM
#13
Re: Bar Charts in C++?
..dude i am reading my textbook and it doesnt say anything about making bar charts with the for loop. if you are not going to try to help please do not answer at all okay? believe it or not there are beginners in this world
-
November 24th, 2009, 01:52 AM
#14
Re: Bar Charts in C++?
Originally Posted by Sabensohn70
im thinking for ( store1 = 0; store1 / 100; ++store1 ) ? this doesnt work
Yes, but that is because you used store1 as the loop counter. You should use another variable. Also, note that the truncation means that if the number is less than 100, no asterisk will be displayed. Is this intended?
EDIT:
Originally Posted by Sabensohn70
..dude i am reading my textbook and it doesnt say anything about making bar charts with the for loop. if you are not going to try to help please do not answer at all okay? believe it or not there are beginners in this world
Read Joeman's explanation of why your loop did not work. If you refuse to listen to anything other than a direct solution, you will not gain in understanding, and thus remain a beginner forever.
Last edited by laserlight; November 24th, 2009 at 01:54 AM.
-
November 24th, 2009, 02:33 AM
#15
Re: Bar Charts in C++?
Originally Posted by Sabensohn70
..dude i am reading my textbook and it doesnt say anything about making bar charts with the for loop.
Programming books do not work that way, where you see a solution for all the problems. If books were written that way, a programming book would be millions of pages long. You must use your mind and apply what concepts you've learned in solving the problem.
You have a number, and you must loop n times per 100. How many times will the loop perform? The answer is n/100 (you may need to add 1 if the number is < 100). So now you loop n/100 times. Inside that loop, you print 1 star. Lo and behold, you have your output.
In your code, you took your hard-earned data, and destroyed it when you used it as a loop counter. Just create a brand new variable and use that to count the loop. All you need is a counter, it makes no sense destroying your data and using that as the counter.
if you are not going to try to help please do not answer at all okay?
You were being helped, you just didn't know it because the answer wasn't given to you. Learning to program doesn't work that way, where you are given the answer. At some point, you have to engage your mind in thinking in logical, discrete, well-defined steps. If you can't do that, then this field of study is not for you. And believe me, it has nothing to do with intelligence -- some people can think with a "programming" brain, while others can't, regardless of how intelligent they may be in other topics.
believe it or not there are beginners in this world
Yes, and many of them, probably sitting in your class now, could finish this assignment with no problems, or at the very least, understood immediately what Joeman and laserlight are talking about.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; November 24th, 2009 at 05:12 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|