Partially-filled array

I always have problems with partially-filled arrays. I need to write a program and part of it involves, to

1.) ask user to for a sequence of digits and end when the user enders -1. Assuming each digit entered is 0 through 9.
2.) Store the digits in a partially-filled array
3.) Print the digits back out.

Here is what I have so far, but I can't figure out a way to print out the digits entered:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
int main()
{
    int digits[10], y=0, b=0;
    
    cout << "Enter numbers or -1 to stop: " << endl;
    
	for (int x=0; x<10; x++)
        
	{
		cin >> y;
		if (y!=-1)
		{
		   digits[x]=y;
                   b++;
		}
		else
		      break;
	}
return 0;
}


Can anyone help me out? Thank you.
Last edited on

Assuming each digit entered is 0 through 9.


You have no check to see if the number is 0-9.

You print the numbers out the same way you input them with a for loop.
b++ is the counter.

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
#include <iostream>
using namespace std;
int main()
{
    int digits[10], y=0, b=0;
    
    cout << "Enter numbers or -1 to stop: " << endl;
    
	for (int x=0; x<10; x++)
        
	{
		cin >> y;
		if (y!=-1)
		{
		   digits[x]=y;
           b++;
		}
		else
        break;
	}
        cout << "You typed : " ;
        for (int x=0; b>x; x++)
        {cout << digits[x] << " ";
        }
        cout << endl;
return 0;
}
The process is quite simple. You want to fill the array as long as the user has not entered -1 as input. If the user does this, output all the values in the array.

Observe:
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
#include <iostream>
using namespace std;
int main()
{
    int digits[10], y=0, b=0;
    
    cout << "Enter numbers or -1 to stop: " << endl;
    
	for (int x=0; x<10; x++)
        {
		cin >> y;
		if (y!=-1)
		{
		   digits[x]=y;
                   b++;
		}
		else
		{
		  for ( x = 10-b,  b = 0; x < 10; b++, x++)
		    cout << digits[b] << " ";//and print out all the values in the array
		  break;
		}		
	}
	cout << endl;
return 0;
}


You can make it even fancier by using the isdigits(int) function to check that a valid digit is entered, then print a message to the user if the value entered is not a number.
Last edited on
The code above seems more complicated than it needs to be IMO. You can just let 'b' be equal to 0 until you need to use it to print the numbers and put while(b < x) as condition

My attempt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
  int digits[10], x, input;

  for(x = 0; x < 10; ++x)
  {
    cin >> input;
    if(input != -1)
      digits[x] = input;
    else
      break;
  }

  for(int i = 0; i < x; ++i)
    cout << digits[i] << " ";
@maeriden

I would have done it that way, but in his code, he had b incrementing at the same time as x was. So if I used while(b < x) that would not have worked. I understand what you mean by letting b = 0 until it was needed, but I was trying to stick to his format.
Ah I see. But I suppose we agree that having that 'b' is redundant since it is always equal to 'x' and it's better if we get rid of it
Thank you guys, that helped a lot.
@Smac89 I haven't heard of isdigits(int) function, I'm barely in a intro to programming class. Or you mean creating my own predefined function? As we just covered that on Tuesday as our last lecture.

@maeriden I prefer your code, as it is easier to understand and does what I need but I still have a problem.

Here is what I am using:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    int digits[10], x, input;
    
    cout << "Enter digits or -1 to stop: ";
        
        for(x = 0; x < 10; x++)
        {
            cin >> input;
            if(input != -1)
                digits[x] = input;
            else
                break;
        }
        
        for(int i = 0; i < x; i++)
            cout << "Digits: " << digits[i] << endl;

And my output is:
1
2
3
4
Enter digits or -1 to stop: 9 1 1 -1
Digits: 9
Digits: 1
Digits: 1


How can I make it so it outputs like this:
Digits: 911


And can you help me understand what is going on in the second for loop. Why exactly is the "i < x"?

If that makes any sense. Thank you.
Last edited on
Using for loops for these types of problems always seems a little disingenuous to me. We're looping until x is 10.. except we're not. Why not use a while loop and get the actual loop condition in the loop condition?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using std::cin ;
using std::cout ;

int main()
{
    int digits[10] ;
    const unsigned max_digits = sizeof(digits)/sizeof(digits[0]) ;

    unsigned digits_entered = 0;
    int input ;
    while ( digits_entered < max_digits  &&  (cin >> input)  &&  input != -1 )
        digits[digits_entered++] = input ;

    cout << "Digits: " ;
    for ( unsigned i=0; i<digits_entered; ++i )
        cout << digits[i] ;
    cout << '\n' ;
}
How can I make it so it outputs like this:
Every time the loop executes it prints "Digits: " and the newline again. You want to move "Digits: " outside the loop so that it's printed only once and remove the 'endl'

And can you help me understand what is going on in the second for loop
In the first for, every time a valid number is inserted the loop reaches the end, 'x' is incremented and it restarts. This means that the array has x valid numbers that you want to print. In your example x == 3
The second for uses the variable 'i' to select which element to print. It will stop when 'i' becomes equal or greater than the number of valid elements in 'digits'. In your example the condition translates to i < 3.
So:
first iteration i = 0. i < 3? Yes -> print digits[0]
second i = 1. i < 3? Yes -> print digits[1]
third i = 2. i < 3? Yes -> print digits[2]
fourth i = 3. i < 3? No -> exit the loop. Perfectly executed since we only had 3 numbers in the array
@cire I see what is going on but I haven't dealt with "unsigned" and "sizeof" but with your code I was able to accomplish what I wanted to do. So my final code for this part of my project is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    int digits[10], x, input;
    
    cout << "Enter digits or -1 to stop: ";
        
        for(x = 0; x < 10; x++)
        {
            cin >> input;
            if(input != -1)
                digits[x] = input;
            else
                break;
        }
        cout << "Digits: " ;
            for(int i = 0; i < x; i++)
                cout << digits[i];
                cout << endl;
Topic archived. No new replies allowed.