+ 1

What's wrong here? (Says b was not declared in this scope)

#include <iostream> using namespace std; int main() { for (int a = 0; a < 10; a++) { cout << a << endl; while(a < 10) int b; cin >> b >>endl; cout<<b << "not enough"<<endl; } return 0; }

1st Jun 2017, 1:13 AM
Justme
Justme - avatar
22 Respostas
+ 3
@Justme following people -helps you keep track of other users -to easily access their profile codes posts I did it to win challenges, helps keep track on who the stronger challengers are, for each language. Like @Peter (C#) and the #1 Nikolay (most of them?)
1st Jun 2017, 3:58 AM
Manual
Manual - avatar
+ 10
First thing The while loop never ends, and so the for loop never increments(infinite loop) Second thing You can declare a variable only once. Since you have placed int b; in the while loop it is declared every time. This also causes an error. Hope this helps.
1st Jun 2017, 1:31 AM
Dragon Slayer Xavier
Dragon Slayer Xavier - avatar
+ 9
@Justme, every programmer begins with an error.
1st Jun 2017, 1:37 AM
Dragon Slayer Xavier
Dragon Slayer Xavier - avatar
+ 8
And remove int b; from the while loop. #include <iostream> using namespace std; int main() { int b; for (int a = 0; a < 10; a++) { cout << a << endl; while(a < 10) cin >> b >>endl; cout<<b << "not enough"<<endl; } return 0; } //This is still an infinite loop.
1st Jun 2017, 1:34 AM
Dragon Slayer Xavier
Dragon Slayer Xavier - avatar
+ 6
b is declared in the scope of the while loop. You forgot brackets in the while loop so it's only reading the one statement: "int b;". I ASSUME this is what you are trying to do: for (int a = 0; a < 10; a++) { cout << a << endl; while(a < 10) { int b; cin >> b >>endl; cout<<b << "not enough"<<endl; } } Note* this is an infinite loop.
1st Jun 2017, 1:15 AM
Rrestoring faith
Rrestoring faith - avatar
+ 3
I have not nest loops like that before. u oh it needs another } at the end before return 0.
1st Jun 2017, 1:29 AM
Manual
Manual - avatar
+ 2
True I was trying to make an infinite loop thank's btw.
1st Jun 2017, 1:32 AM
Justme
Justme - avatar
+ 2
Amrit thank's didn't knew that actually if I put int b; indide while loop it was declared continiously :/ (my mistake). šŸ‘Œ
1st Jun 2017, 1:35 AM
Justme
Justme - avatar
+ 2
@manual That would prevent it from being infinite, yes. However, the for loop wouldn't continue when the while loop ends.
1st Jun 2017, 1:53 AM
Rrestoring faith
Rrestoring faith - avatar
+ 2
yes becouse if you put ++a you are adding a value (+1) before the action so a actually reach 10 (i think)
1st Jun 2017, 1:54 AM
Justme
Justme - avatar
+ 2
@manual Yeah, I don't think endl does anything with cin, so you could actually be right about that. Also, I recommend never actually changing the variable that was created by a for loop, inside the loop. That can cause huge un-needed confusion in real projects. It's considered good practice to never do it. That's what the incrementation of the for loop is for. @Justme Following people lets you see what that person does on the "activity" feed. I guess so you can see more posts or codes they have done.
1st Jun 2017, 2:02 AM
Rrestoring faith
Rrestoring faith - avatar
+ 2
@Justme The code you wrote was fine if you follow what @amrit said about declaring things outside loops (maybe also remove the endl with the cin). I'm talking about when manual asked about incrementing a. Ex/ for(int i = 0; i < 100; i++) { ++i; // don't change this variable in the loop // it will get confusingly unnecessary } I could have just done: for(int i = 0; i < 100; i+=2) { }
1st Jun 2017, 2:09 AM
Rrestoring faith
Rrestoring faith - avatar
+ 1
does endline do anything for cin? what does b do in this code?
1st Jun 2017, 1:53 AM
Manual
Manual - avatar
+ 1
actually don't know gonna check
1st Jun 2017, 1:55 AM
Justme
Justme - avatar
+ 1
I put it to dont have the text all together
1st Jun 2017, 1:56 AM
Justme
Justme - avatar
+ 1
You're welcome
1st Jun 2017, 1:59 AM
Justme
Justme - avatar
+ 1
What is the use of following people?
1st Jun 2017, 2:00 AM
Justme
Justme - avatar
+ 1
@ Rrestoring faith so should I use instead while...do loop? (just learn that)
1st Jun 2017, 2:06 AM
Justme
Justme - avatar
+ 1
aaa got it thank's
1st Jun 2017, 2:11 AM
Justme
Justme - avatar
0
Would adding ++a inside the loop end the while loop?
1st Jun 2017, 1:51 AM
Manual
Manual - avatar