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:
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.
#include <iostream>
usingnamespace 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.
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;
elsebreak;
}
for(int i = 0; i < x; ++i)
cout << digits[i] << " ";
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.
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;
elsebreak;
}
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"?
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] ;
constunsigned 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' ;
}
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;
elsebreak;
}
cout << "Digits: " ;
for(int i = 0; i < x; i++)
cout << digits[i];
cout << endl;