Histogram help

Can Someone help me with a histogram that gets its values from an array in an input file.

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
42
43
44
45
46
47
48
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int ARRAY_SIZE = 1000;

int main()
{
	string filename;
	system("cls");
	system("pause");
	
	ifstream ifile;

	while (!ifile.is_open())
	{
		system("cls");
		cout << endl << endl;
		cout << endl << endl;
		cout << "		Please enter the name of the input file" << endl
			<< "		in which the list of numbers is found." << endl;
		cout << "		";
		getline(cin, filename);
		ifile.open(filename += ".txt");
		if (!ifile.is_open())
		{
		cout << endl << endl;
			cout << "		";
			cout << filename << " did not open." << endl;
			cout << endl << endl;
			system("pause");
		}
		
	}
	
	int array1[ARRAY_SIZE];
	int number_of_values = get_array_values(array1, ARRAY_SIZE, ifile);

	for (int i = 0; i < number_of_values; ++i)
	{
		for (int m = 0; m < array1[m]; m++)
		{
			cout << "*";
		}
		cout << endl;
	}


The current output looks like getting the amount values correct but the amount of * for each value only comes from the first value in the array

*****
*****
*****
*****
*****


But I want it to look like

*
**
***
****
*****
Line 43. How many times does the loop iterate?
it should loop the amount of times of the value in the array. if i have an input file with the values of 5,2,1,4,3 it outputs

*****
*****
*****
*****
*****


instead of

*****
**
*
****
***
In other words, for the first line,
* the m is 0 initially
* 0 < array1[0] (==5), so one char prints and m increments
* 1 < array1[1] (==2), so one char prints and m increments
* 2 is not < array[2] (==1), so the loop should end

There is something fishy in the data, for your stated data should print only two * for each row.

Why for each row? Because you do compare the loop counter to array1[m]. Should you not use the ith value for the entire row i?
Topic archived. No new replies allowed.