• Advertisement
Sign in to follow this  

for loop

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

If you intended to correct an error in the post then please contact us.

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


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

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


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

Share this post


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


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

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 != 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 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 < 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 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;
if(n == 0)
break;
b += n;
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
Sign in to follow this  

  • Advertisement