Sign in to follow this  
mattnenterprise

for loop

Recommended Posts

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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by gapern



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;
}



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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Kambiz
int 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 this post


Link to post
Share on other sites
Quote:
Original post by SamLowry
Quote:
Original post by Kambiz
int 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 this post


Link to post
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 this post


Link to post
Share on other sites
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....)

Share this post


Link to post
Share on other sites
Quote:
Original post by wodinoneeye
Forgot 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 this post


Link to post
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;
}

Share this post


Link to post
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this