Array help

I have an array initialized to the size 5. The point of this function in my code is to get input from the user to either fill the array(5 inputs) or cut it short when the user enters a character. It will print out the numbers until the character is introduced then it will only print the spot in memory. How do I correct this when my array is (and has to be) initialized to 5. (That for loop in the while loop is probably dead wrong but I just left things exactly as is)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Fill_array (double num1[], int SIZE1)
{

int i = 0;
for (i = 0; i < SIZE1; i++)
{
    cout << "Please enter a double integer: ";
    cin >> num1[i];
    while (num1[i] >= 'a'&& num1[i] <= 'z')
{
    for (i=0; i < SIZE1; i++)
    cout << num1[i]; //this is where I get the value in memory
}
}

}
I am not 100% sure what's being asked here but I think I have enough to offer some help. Let me clarify first.

What you are saying is that you will be inputting integers into this array until the user enters a character. Then if a character is entered, you will automatically enter some previously determined value already in memory to fill in the rest of the array? Correct?
Looks like it was one of those things where you just had to get away from the computer for a bit.
Turns out the array size doesn't have to be set which makes things a lot easier.

The user is prompted to enter 5 double.
1, 2.5, 3, 4.5, 5 (as an example)
If the user enters only two doubles and hits a char
The array should only print out 1, 2.5

Now IF the array was set to 5, I don't think that its possible but would there be any way to get the output of 1, 2.5? Is there somehow to clear the last 3 doubles in the array?
Now IF the array was set to 5, I don't think that its possible but would there be any way to get the output of 1, 2.5? Is there somehow to clear the last 3 doubles in the array?


Keep a count of the number of values entered and only display that number of elements.

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
http://ideone.com/7BWC3b
#include <iostream>
#include <iomanip>

unsigned fill_array(std::istream& is, double arr [], unsigned arr_size)
{
    unsigned count = 0;

    while (count < arr_size && is >> arr[count])
        ++count;

    return count;
}

void show_array(std::ostream& os, const double arr [], unsigned arr_size)
{
    for (unsigned i = 0; i < arr_size; ++i)
        os << arr[i] << '\n';
}

int main() 
{
    const unsigned numbers_size = 5;
    double numbers[numbers_size];

    std::cout << "Enter up to 5 real numbers\n> ";

    unsigned numbers_entered = fill_array(std::cin, numbers, numbers_size);

    std::cout << "You entered " << numbers_entered << " numbers.\n";
    std::cout << "They are:\n";

    show_array(std::cout, numbers, numbers_entered);
}
Topic archived. No new replies allowed.