for loop

closed account (j1AR92yv)
Hello! I've made this simple code, but am not sure where to go from here.

int main()
{
int i;
cin >> i;
for (int i = 1; i < 5; i += 2)
std::cout << i << std::endl;
}

I need to create a program that lets the user input five numbers, and only displays the odd numbers and skips the number 9, and if the number 9 is entered, then the program does not display the number 9 when displaying the odd numbers.
Should I be adding more variables?
I would start with this.
1
2
3
4
5
6
// I need to create a program that lets the user input five numbers
for ( int i = 0 ; i < 5 ; i++ ) {
    int number;
    cin >> number;
    cout << "You entered " << number << endl;
}


Then I would edit it to this.
1
2
3
4
5
6
7
// I need to create a program that lets the user input five numbers
for ( int i = 0 ; i < 5 ; i++ ) {
    int number;
    cin >> number;
    // and only displays the odd numbers
    cout << "You entered " << number << endl;
}

Now you look at the code, in particular the most recently added comment, and you think about a line of code that determines whether number is odd.

You keep adding one specific step of your requirements until the program is done.



closed account (j1AR92yv)
I've done this,
@salem c
int main()
{
for (int i = 0; i < 5; i++) {
int number;
cin >> number;
{
if (number % 2 != 0)
cout << "You entered " << number << endl;
else cout << "Doesn't count. \n";
{
if (i == 9) continue;
}
}
}

}



I feel like I have too many scopes...
I can't get the program to skip the number 9 as well if entered from the keyboard, I feel like I am close though. Is there something I'm missing?
Last edited on
Well you're missing code tags, your code looks a mess.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
  for (int i = 0; i < 5; i++) {
    int number;
    cin >> number;
    {
      if (number % 2 != 0)
        cout << "You entered " << number << endl;
      else
        cout << "Doesn't count. \n";
      {
        if (i == 9)
          continue;
      }
    }
  }
}


> if (i == 9)
well i is your loop variable, not the number you typed in.
Also, i is constrained to be from 0 to 5, so it's never going to be 9 anyway.
Topic archived. No new replies allowed.