problem with initilazation

Hello, I am working on some beginer exercises that were listed here on the forums. But i am get an error during compile at an for statment. When the compiler gets to the braces at the start of the statment, it says 'i undeclared identifier'.

Here is the code that i have. I have tested the for loop by itself and it works, but here no dice.

// user_is _gullable.cpp : Defines the entry point for the console application.
//
// The third in a series of exersizes.
// http://www.cplusplus.com/forum/articles/12974/2/

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
int chNumber = 0;

for (int i=1; i<10; i++)
{

while (chNumber != 5)
{
cout << "\nEnter a number other than 5.\n";
cin >> chNumber;
}

if (chNumber == 5)
cout << "\nHEY!! You weren't suppose to enter 5.\n";
}

cout << "\nNow the exercise is over.\n";

system("PAUSE");
return 0;
}
I can't see anything that is (syntactically) wrong there...

PS: Use [ code ]Your syntax highlighted code, like int and double and stuff [ /code ] tags.
Last edited on
But why are you using a for loop? The program should work without it.
I suppose he's trying to get the user to enter 5 9 times (at least that's what he's doing right now)? I don't know, if I did run that program I would probably just hit ctrl+c cause I'd assume it is broken.
for (int i=0; i<10; i++)
it is after this entering the block, that i get an error message in my compiler. It says 'i' undeclared identifier.
Im at a loss, because the syntax is right.
Last edited on
You didn't actually write that, did you?
closed account (zb0S216C)
Firstly, this code works perfectly fine. Secondly, when the user enters the value of 5, you should break from the loop by using the break keyword.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main()
{
int chNumber = 0;

for (int i=0; i<10; ++i)
{
cout << "\nEnter a number other than 5.\n";
cin >> chNumber;
    if (chNumber == 5){
cout << "\nHEY!! You weren't suppose to enter 5.\n";
break;
    }
}

cout << "\nNow the exercise is over.\n";

return 0;
}


I modified your program a little. This version should work. You didn't need the while loop after all...
hansr99;
yes i wrote that, i am working through exersices that are listed on this site. http://www.cplusplus.com/forum/articles/12974/
I am just starting to learn c++ and this is one of my first problems to solve.

Last edited on
No i mean, you wrote
for int(int i=0... before.
Topic archived. No new replies allowed.