Having trouble with nested loop pattern

I'm supposed to be printing out a total of 7 symbols per line when I enter a 7 according to the instructions but I keep getting 8. How can I make sure only 7 symbols are entered in this line of code?

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
  int main ()
{
	int A;
	
	cout << "Eric Arango " << endl;
	cout << "enter a value between 7 and 23 " << endl;
	cin >> A;

	if ( A >= 7 && A <= 23 && A % 2 == 1 )
		cout << "print picture"  << endl;
	else
		return 0;

		for ( A > 7 ; A < 23 ; A++ )
		{
			if ( A % 2 == 1 )
		{
			cout << "&" ;
		}
		}
	
	
	
	system("pause");
	return 0;

}
you are overusing the A variable


make a variable, one for user input and one for condition
I'm kinda confused on what you mean by that. I'm new to this so, pretty confusing.... sorry
You have a for loop. You have there an expression A > 7 that does absolutely nothing.

This code does about the same as your program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using std::cout;

int main ()
{
  int A;
  std::cin >> A;
  if ( 7 <= A && A <= 23 && (A % 2 == 1) )
  {
    while ( A < 23 )
    {
      if ( A % 2 == 1 )
      {
        cout << A << '\n' ;
      }
      ++A;
    }
  }
  return 0;
}

I have to use a for loop. I know it's wrong. I'm just asking for some guidance
You can use the for loop. The question is, what should it do?

You, as user, have given value 7 and the program has stored it in variable A.
Your test on line 9 has confirmed that value 7 is valid (odd and within range [7..23].

Your for loop will continue as long as A is less than 23 and increment the A by one after each iteration. If the A is odd, then you print a symbol.

Note that we know that A is odd at start. If we keep incrementing it by 2 rather than 1, then the A is odd on every iteration and we don't have to test that separately.
1
2
3
4
5
    while ( A < 23 )
    {
      cout << A << '\n' ;
      A += 2;
    }

or
1
2
3
4
    for ( ; A < 23; A += 2 )
    {
      cout << A << '\n' ;
    }

There are eight odd values: {7, 9, 11, 13, 15, 17, 19, 21} (starting from 7 and less than 23).

We get back to the question: why do you have the A > 7 in your for loop, as an init-expression. It does not do anything there.

Why are the 7 and 23 valid inputs? Is the intention that the 23 is valid, but produces 0 symbols?
I put it there since it was giving me errors without it. didn't realize I could just put the ; by itself.

The 7 and 23 are valid outputs. The number the user puts in A produces no symbols. the numbers following after will.
Then the for loop should not start from A given by the user, but from the next odd number.
Topic archived. No new replies allowed.