Loop runs just once

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
int main()
{
    char character;
    int it;  
  if (character == 'b'){
    for( it = 9; it > 1 ; it-- );
        cout << "Variable is " << character << ' ' << it-- << endl;
        cin >> character ;
    }

Why this loop runs just once, i want to run nine times.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
int main()
{
    char character;
    int it;  
  if (character == 'b'){
    for( it = 9; it > 1 ; it-- );
        cout << "Variable is " << character << ' ' << it-- << endl;
        cin >> character ;
    }


Your error is in line 8

In for( it = 9; it > 1 ; it-- ); you have a semi colon after your loop causing it to end after the first iteration. Remove the semi colon and it should run how you want. Also, in line 9 cout << "Variable is " << character << ' ' << it-- << endl; you should remove it-- from your line and just replace it with it if your just trying to display the current iteration of the loop.

Also, just as a convention you should remodel your loop to look something like this
 
for(int i = 0; i < 9; i++)


But regardless, after you remove the semi colon it should run.

1
2
3
4
 for(int i = 0; i < 9; i++){
        cout << "Variable is " << character << '  ';
        cin >> character ;
}
Last edited on
if (character == 'b'){
How often will character == 'b' ?
Solved that problem, thanks.
Now i inserted pointers to store the inputed value from c or a or d to a pointer then from pointer to put in variable "input" when i press b.
Now i want when i press c then when i press b to run the loop how many times i press c and b.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main( )
{
    char character, *input;
    int i, it;
        cin >> character;

    if ( character == 'c' || 'a' || 'd') {
        for( i = 1 ; i <= 9  && character!='b'  ; i++ )
         cin >> character
         input=&character;}

    if (character == 'b'){
    for( it = 9; it > 1 ; it-- )
        cout << "Variable is " << *input << ' ' << it << endl;
    }
return 0;}

my output with this program is for example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
c
c
c
c
Variable is b 9
Variable is b 8
Variable is b 7
Variable is b 6
Variable is b 5
Variable is b 4
Variable is b 3
Variable is b 2
b
process returned 0 <0x0>...


i want to output this ->
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
d*
c**
a***
c****
d*****
b
Variable is d***** 9
b
Variable is c**** 8
b
Variable is a*** 7
b
Variable is c** 6
b
Variable is d* 5
c^
b
Variable is c^ 4 ...
Last edited on
http://www.cplusplus.com/forum/windows/182433/
I answered it here...

And if you want to store "c**" to array you will need a String array not char Array.
Topic archived. No new replies allowed.