Public Group

# for loop

This topic is 3829 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am having problems with a for loop.here is some code. int i = 0 for(int n[20];n != 0;++i) { cin >> n; b = b + n; cout << b << endl; } For some reason when I type in 0 the program doesn't quit. Can someone help.

##### Share on other sites
Where did you find this code?
It is terrible!
Of course it's not going to work since you are comparing an address to 0 which is never going to happen!
Why don't you use something simpler?

##### Share on other sites
I'm going to assume you have a main and a b variable defined and just didn't include those in your code snippet for whatever reason. This should work.

int i = 0;int b = 0;int n[20];for(i = 0;i != 21; i++){     cin >> n;     b = b + n     cout << b << endl;}

Your for loop conditions were kind of confusing and i'm not sure what you were trying to do there, so if the code I wrote isn't what you were looking for let me know.

##### Share on other sites
Quote:
 Original post by gapernint i = 0;int b = 0;int n[20];for(i = 0;i != 21; i++){ cin >> n; b = b + n cout << b << endl;}

That's incorrect. The last iteration will index the array out of bounds. Here is the correct code.

int i = 0;int b = 0;int n[20];for(i = 0;i < 20; i++){     cin >> n;     b = b + n     cout << b << endl;}

##### Share on other sites
You're right. Sorry about that. I forgot arrays start at [0].

##### Share on other sites
This should fix the problem:
int i = 0;for(int n[20];n != 0;++i){cin >> n;b = b + n;cout << b << endl;}

although I agree that this code is terrible.

##### Share on other sites
Quote:
 Original post by Kambizint i = 0;for(int n[20];n != 0;++i){cin >> n;b = b + n;cout << b << endl;}

Doesn't that depend on n[0] not being initialized to 0? Since C++ makes no guarantees, the behavior of that code is not 100% predictable as far as I know.

My attempt:
const int SIZE = 20;int n[SIZE];int i = 0;int b = 0:int input;std::cin >> input;while ( i != SIZE && input != 0 ){  b += input;  std::cout << b << std::endl;  n = input;  ++i;  std::cin >> input;}

Still horrible, but it's something.

##### Share on other sites
Quote:
Original post by SamLowry
Quote:
 Original post by Kambizint i = 0;for(int n[20];n != 0;++i){cin >> n;b = b + n;cout << b << endl;}

Doesn't that depend on n[0] not being initialized to 0? Since C++ makes no guarantees, the behavior of that code is not 100% predictable as far as I know.

This is true, it is one of the reasons why that code is terrible. But I think it was the part causing all the confusion... (There is also no index boundary check.)

##### Share on other sites
Actually, I believe this is what's intended:

int i = 0;int n[20];for(cin >> n;(i < 20 && n != 0);cin >> n[++i]){b = b + n;cout << b << endl;}

However, this can also be done without the arbitrary 20 number limit on input:

int n;for (cin >> n; n != 0; cin >> n){b = b + n;cout << b << endl;}

Depending on what was intended, either should work. [smile]

Edit: Fixed first example.

Also, one last thing: Never trust user input. Ever. Expect them to input a string instead of a number. [smile]

##### Share on other sites
Quote:
 Original post by PuckActually, I believe this is what's intended:*** Source Snippet Removed ***However, this can also be done without the arbitrary 20 number limit on input:*** Source Snippet Removed ***Depending on what was intended, either should work. [smile]Edit: Fixed first example.Also, one last thing: Never trust user input. Ever. Expect them to input a string instead of a number. [smile]

Forgot to initialize b to zero in each case???? (or even define it as a var....)

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 17
• 14
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634094
• Total Posts
3015500
×