for loop

Started by
10 comments, last by theOcelot 15 years, 9 months ago
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.
Advertisement
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?
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
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.
Quote:Original post by gapern


int 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;}
You're right. Sorry about that. I forgot arrays start at [0].
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.
Quote:Original post by Kambiz
int 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.
Quote:Original post by SamLowry
Quote:Original post by Kambiz
int 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.)
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]
Quote:Original post by Puck
Actually, 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....)

--------------------------------------------[size="1"]Ratings are Opinion, not Fact

This topic is closed to new replies.

Advertisement