Printing out "*" corresponding to array index size.

I am having trouble getting the stars to output correctly in the printStars function. the final output should look something like this:
 
Enter a number(-1 to stop): 3
Enter a number(-1 to stop): 2
Enter a number(-1 to stop): 4
Enter a number(-1 to stop): -1
3
2
4
0: ***
1: **
2: ****

Here is what I have so far. But for some inputs it skips the output of the last index but for some numbers it doesnt skip... im quite confused.
Any help is greatly appreciated thank you!

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>
using namespace std;

int readArray(int input[], int size);
void printArray(int input[], int count);
void printStars(int input[], int size);

int main(){
	int input[10];
	int count = readArray(input, 10);

	cout << "Printout List:" << endl;
	printArray(input, count);
	//print stars
	printStars(input, count);

	system("pause");
}

int readArray(int input[], int size){
	int x;
	int count = 0;
	do{
		cout << "Enter a number: (-1 to stop): ";
		cin >> x;
		if (x != -1){
			input[count] = x;
			count++;
		}
	} while (x != -1 && count < size);
	return count;
}
void printArray(int input[], int count){
	for (int i = 0; i < count; i++){
		cout << input[i] << endl;
	}
}
void printStars(int input[], int count){
	int counter = 0;
	for (int i = 0; i < input[i]; i++){
		int x = input[i];
		cout << counter++ << ": ";
		for (int n = 0; n < x; n++){
			cout << "*";
		}
		cout << endl;
	}
}
Last edited on
Try count as loop termination in line 40 instead of input[i].
yes line 40 is creating the issue since you are checking i value with content of the array at i'th position so it is skipping for some numbers

as suggested by tcs we cant use count also as we are not sure total array while will be filled or not. i mean user may read 5 entries and press -1 so i suggest following changes.

change line 9

int input[10]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};

and change line 40 as

for (int i = 0; input[i] != -1; i++){
@raju8438: Your suggestion won't work because "we are not sure total array [...] will be filled or not".

So use count. It really will help :-)
Topic archived. No new replies allowed.