![]() |
| ||
| Having problem with array in for loop... I did fine on this in C, not sure what I'm doing wrong here. Probably something silly on my end. I'm receiving odd numbers in my program below. I have included the output below the program. Any assistance would be quite appreciated. Thanks! #include <iostream> ------------------------------------ I enter the numbers 1, 2, 3, 4, and 5 in the beginning. I am expecting the Output to be: 1 2 3 4 5 1 2 3 4 5 Press any key to continue... but instead, the output is: 1 2 3 4 5 1 4313739 4313508 4313739 4268019 Press any key to continue . . . |
| ||
| Re: Having problem with array in for loop... You only fill exercise[0]. Perhaps this would work better for you: for (i = 0; i < 5; i++) |
| ||
| Re: Having problem with array in for loop... Ah, I understand now. I thought it might have something to do with that, but my professor didn't explain it very well in class last night. Thanks! |
| ||
| Re: Having problem with array in for loop... >but my professor didn't explain it very well in class last night How about you try to explain it to me as you understand it, then I'll fill in the blanks for you. |
| ||
| Re: Having problem with array in for loop...
#include <iostream> |
| ||
| Re: Having problem with array in for loop... >using namespace std; // out of place Actually, that's a better place than your proposed solution. The effect of a using directive is limited by scope. When you restrict the directive to a block scope, you minimize the chances of a name collision and effectively remove the biggest reason for avoiding the using directive[1]. >// C++ arrays start with index 0, so its 0, 1, 2, 3, 4 I think it's safe to say that the OP already understands this. :icon_rolleyes: [1] The reason: a using directive at global scope defeats the purpose of namespaces. The only reason you should have a using directive at global scope is when porting pre-standard C++ quickly. In new code, it's a bad practice. |
| ||
| Re: Having problem with array in for loop... Quote:
|
| ||
| Re: Having problem with array in for loop... Quote:
thats why I am writing the whole time :twisted: : std::cout<<......std::endl; |
| All times are GMT -4. The time now is 11:32 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC