for() loop

I am trying to do do the
While( user == gullible )
in every single looping state so that I can understand all of them well. Have fully completed it with the do{}while loop. The for loop is giving me trouble though, this is what I have so far -
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int main()
{
    for(int x=0; x!=5;) {
        cout<<"Enter any number other than 5: ";
        cin>>x;
        cin.ignore();
    if (x==5) {
    cout<<"You werent supposed to enter 5...";
        }
    }
cin.get();
}


The first part of the challenge works fine. To do the first star however I would need another variable to count how many times I press enter and the loop structure doesn't allow me to use more than one variable. Any ideas?
Last edited on
Actually i'm not sure what do want to do. But i'm sure about one thing, you can use more than one variable in a for loop.

1
2
3
4
5
6
int i, j;
for(i = 0, j = 0; i<5 && j<10; ++j)
{
  // TODO
  // ++i;
}
Last edited on
http://cplusplus.com/forum/articles/12974/ - While( user == gullible ) - I am trying to do the 1st star of the problem. well hmmm if you say i can use more than one variable in it then i will continue experimenting.

EDIT: ah... so you can declare the variables outside of the for loop. okay I will try that
Last edited on
lol it worked like a charm. Thanks...it was the tutorial i read that caused the confusion...http://www.cprogramming.com/tutorial/lesson3.html, it exemplifies placing the variables inside the for loop and i thought I must do it like that. Sorry for the simple question.
Nvm... it does not work like a charm...
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()
{
    int x;
    int j;
    for(x=0; x!=5; j=1; j<=10) {
        cout<<"Enter any number other than 5: ";
        cin>>x;
        cin.ignore();
    if (x==5) {
    cout<<"You werent supposed to enter 5...";
        }
j++;
    }
cin.get();
}
The format of a for loop is as follows: for (initialization; condition; variable change) statement;

What you have is: for (initialization, condition; initialization, condition) { code block; } - the variable change is not necessarily required.

See if you can correct your code with this information.
Last edited on
I can't do it. I have tried changing and rearranging the same code over and over but it is not working...

EDIT: im going to re-write it from scratch.


1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
int main()
{
    int x, j;
        for (x=0, j=1; x!=5 &&  j<=10; j++) {
    cout<<"Enter number: ";
    cin>>x;
        }
}


EDIT: this worked...
Last edited on
Topic archived. No new replies allowed.