+ 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; }
22 odpowiedzi
+ 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?)
+ 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.
+ 9
@Justme, every programmer begins with an error.
+ 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.
+ 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.
+ 3
I have not nest loops like that before.
u
oh it needs another } at the end before return 0.
+ 2
True I was trying to make an infinite loop thank's btw.
+ 2
Amrit thank's didn't knew that actually if I put int b; indide while loop it was declared continiously :/ (my mistake). 👌
+ 2
@manual That would prevent it from being infinite, yes. However, the for loop wouldn't continue when the while loop ends.
+ 2
yes becouse if you put ++a you are adding a value (+1) before the action so a actually reach 10 (i think)
+ 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.
+ 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)
{
}
+ 1
does endline do anything for cin?
what does b do in this code?
+ 1
actually don't know gonna check
+ 1
I put it to dont have the text all together
+ 1
You're welcome
+ 1
What is the use of following people?
+ 1
@ Rrestoring faith so should I use instead while...do loop? (just learn that)
+ 1
aaa got it thank's
0
Would adding ++a inside the loop end the while loop?