Array with 5 odd numbers, can't figure out how to display

Dec 5, 2021 at 5:40pm
Hello guys. As the title says, I'm having a difficult time with Arrays. I need to create an array that stores 5 numbers (odd numbers only) If the user enters and even number, display an error message and tell them to enter only odd numbers.

I've done all of this, but everytime I try to display the list, it doesn't work. So i guess my real question is, could someone show me how to display the odd numbers stored into my array? I will post what I have below.

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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;



int main()
{	
    int array[5]; //array created for storing odd numbers only.

    for (int i = 0; i < 5;)
    {
        cout << "Please enter 5 odd numbers: "; //user enters 5 odd numbers
        cin >> array[i];
        if (array[i] % 2 != 0) //if the number is odd then store it, if it is even:
        {
            i++;
        }
        else
        {
            cout << "Invalid input. Please only enter odd numbers!" << endl; //let the user know to enter only odd numbers. 
        }

    }
    return 0;
}
Dec 5, 2021 at 6:08pm
loop just like you already did and print each value, with a space or end of line or whatever between the numbers.
Dec 5, 2021 at 6:13pm
I tried to do that but I can't quite seem to figure out how to do it.
Dec 5, 2021 at 6:22pm
Take your pick:

One line per
1
2
  for (int i=0; i<5; i++)
      cout << array[i] << endl;


Space separated list
1
2
3
  for (int i=0; i<5; i++)
      cout << array[i] << " ";
  cout << endl;
Dec 5, 2021 at 6:23pm
@jonnin

I used this, but it is an infinite loop and I don't know how to stop the loop. I'm new to arrays and loops.

1
2
3
4
   for (int i = 0; i < 5;)
    {
        cout << "The five odd numbers are: " << array[i]; //displays the 5 numbers stored in the array
    }  
Dec 5, 2021 at 6:24pm
In line 15 you read directly into the array before you check it.
On line 22 you print an error message but don't remove it from the array.
You need to read it into a temporary variable and only if it is odd store it.
Dec 5, 2021 at 6:30pm
@AbstractionAnon, thanks for that. I see what I was missing now.

@thmm what do you mean by check it? and I'm new to arrays so i'm not sure how to remove and even number input from the array or any of that. I'm reading some material about arrays and functions currently but I'm literally like a 2 day newbie. But thank you for input.
Dec 5, 2021 at 7:33pm
Maybe like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for (int i = 0; i < 5;)
{
        cout << "Please enter 5 odd numbers: "; //user enters 5 odd numbers
        int tmp;
        cin >> tmp;
        if (tmp % 2 != 0) //if the number is odd then store it, else print error msg
        {
            array[i] = tmp; 
            i++;
        }
        else
        {
            cout << "Invalid input. Please only enter odd numbers!" << endl; //let the user know to enter only odd numbers. 
        }
}
Dec 5, 2021 at 7:43pm
@thmm - Granted it's poor practice to input directly into the array before checking, but if it's not odd, i does not get incremented and the entry gets overwritten the next time through the loop.
Last edited on Dec 5, 2021 at 7:43pm
Dec 5, 2021 at 11:13pm
1
2
3
4
5

 for (int i = 0; i < 5; i++) //HERE
    {
        cout << "The five odd numbers are: " << array[i]; //displays the 5 numbers stored in the array
    }  


Dec 6, 2021 at 8:05am
@AbstractionAnon,
why would it be overridden? Once an element is inserted i gets incremented.
If it doesn't get inserted there is no need to increment the next index.
Dec 6, 2021 at 10:48am
for ( int e : array ) cout << e << ' ';
Dec 6, 2021 at 1:25pm
thmm wrote:
Once an element is inserted i gets incremented.

No it doesn't in the OP's original code. See lines 16-19.
i only gets incremented if the element is odd.
The next time through the loop, the same element is used.
Dec 6, 2021 at 2:50pm
sorry for the late post. You guys helped me a ton. Turns out I'm doing this wrong anyway. It gives me the required output needed, but apparently I'm supposed to not have any of this under int main () I'm supposed to have functions. So now I am off to figure out to make functions.
Dec 6, 2021 at 3:54pm
Maybe like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

const int N = 5;

void read_input(int arr[N])
{
}

void print_array(int arr[N])
{
       
}

int main()
{	
    int array[N]; //array created for storing odd numbers only.
    
    read_input(array);
    
    print_array(array);
    
}

Just fill in your code into the functions.
Topic archived. No new replies allowed.