creating a simple bar chart using numbers entered from the command line

Hi

I need help creating a program which prints a line of n copies of the character c followed by a new line.and then use this code to print out a simple barchart illustrating the numbers read in from the command line.

I have created the code for the first part (shown below)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include <iostream>
using namespace std;

void line (int n, char c)
{
  // this is the loop for n
  for (int i = 0; i < n; i++)
    cout << c << endl;
}

int main(void)
{
  int Number;
  char Char;

  cout << "Enter a number: " ;
  cin >> Number ;
  cout << "\nEnter a letter: " ;
  cin >> Char ;

  line (Number, Char) ;
}



But i need help so this code can be used to read in numbers from the command line to print a simple barchart in this format:

1
2
3
4
5
6
7
8
9
10
11
> ./a.out 21 1 8 15 10 8
1: *********************
2: *
3: *******
4: ***************
5: **********
6: ********
--------------------------------------------------





Thanks in advance if you can help me.
To print a repeated character, use manipulators:
 
cout << setw(n+1) << setfill('*') << '\n';
( n is the number of times character '*' needs to be printed )
But can you help me implement what you said into my code please? And i am entering numbers from the command line so can you help me out with this?
1
2
3
4
5
6
7
8
#include <iostream>
#include <iomanip>
int main()
{
     int n;
     cin >> n;
     cout << '\n' << setw(n+1) << setfill('*') << '\n';
}
(not so difficult)

If you want to store multiple number entered by the user, take a look at these:
http://www.cplusplus.com/reference/stl/list/push_back.html
http://www.cplusplus.com/reference/stl/list/begin.html
Last edited on
The thing is really i shouldnt be using the <iomanip> as I havn't learned it yet! And the user needs to be able to enter numbers from the command line so that the numbers can be transfered into the * characters in the graph.
In that case, you could use the string constructor:

1
2
3
int n = 10;
string s( n, '*' );        // create a string of n asterisks
cout << s << endl;
But then how to i use this code to help me with my original question so that the user enters a certain number of numbers in the command line and these are output into a graph in this format:

1
2
3
4
5
6
7
8

> ./a.out 21 1 8 15 10 8
1: *********************
2: *
3: *******
4: ***************
5: **********
6: ********
--------------------------------------------------
An help at all would be good.
As seymore said but with a console entered value
1
2
3
4
int n;
cin >> n;
string s( n, '*' );        // create a string of n asterisks
cout << s << endl;
The line function that you posted would work, if you remove "<< endl" from the line that prints the character(s) and add "cout << endl;" on a separate line. For each line, you only want one newline at the end, not a newline between each character.

Anyway, to start the command line input, try:
1
2
3
4
5
6
int main( int argc, char * argv[] )
{
    cout << "argc = " << argc << endl;
    cout << "argv[0] = " << argv[0] << endl;
    return 0;
}


Then you'll just have to loop and do a line at a time...
Last edited on
Here a basic one i threw together not sure if it 100% works. Let me know
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>

std::string InsertLine(int index)
{
	std::cout << "Enter the " << index + 1 << " items size in the bar graph: ";
	int itemSize;
	std::cin >> itemSize;

	std::string myString(itemSize, '*');

	return myString;

}

int main()
{
	std::cout << "How big would you like your bar graph: ";
	int graphSize;
	std::cin >> graphSize;

	std::string *graph = new std::string[graphSize];

	for(int i = 0; i < graphSize; i++)
	{
		graph[i] = InsertLine(i);
	}

	std::cout << "--------------------------------------------------" << std::endl;

	for(int i = 0; i < graphSize; i++)
	{
		std::cout << i+1 << ": " << graph[i] << std::endl;
	}

	std::cout << "--------------------------------------------------" << std::endl;

	delete[] graph;

	return 0;
}
1
2
3
4
5
6
7
int main( int argc, char * argv[] )
{
  for(int i=0;i<argc;i++)
   line ((int)*arg[i], '*');
   
   return 0;
}



I am not sure it running
Topic archived. No new replies allowed.