# for loop

## 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[i]; b = b + n[i]; 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[i];     b = b + n[i]     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[i]; b = b + n[i] 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[i];     b = b + n[i]     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[i] != 0;++i){cin >> n[i];b = b + n[i];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[i] != 0;++i){cin >> n[i];b = b + n[i];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[i] = 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[i] != 0;++i){cin >> n[i];b = b + n[i];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];(i < 20 && n[i] != 0);cin >> n[++i]){b = b + n[i];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....)

##### Share on other sites
Quote:
 Original post by wodinoneeyeForgot to initialize b to zero in each case???? (or even define it as a var....)

That was intentional. The original poster didn't include a declaration for b, so I'm assuming it's declared elsewhere in unposted code, but thanks for mentioning it. [smile]

(And as a note for mattnenterprise: if b is in fact NOT declared elsewhere then yes, there should be an "int b = 0;" in there.)

##### Share on other sites
Is it so hard?
int i=0; //why is this outside the loop anyway?for(int n[20]; i<20;i++){    cin >> n[i];    if(n[i] == 0)        break;    b += n[i];    cout << b << endl;}

or

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

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
628336
• Total Posts
2982157

• 9
• 24
• 9
• 9
• 13