| | |
Having problem with array in for loop...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 2
Reputation:
Solved Threads: 0
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!
------------------------------------
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 . . .
C++ Syntax (Toggle Plain Text)
#include <iostream> int main() { using namespace std; int exercise[5], i; cin >> exercise[0]; for (i = 0; i < 5; i++) { cout << exercise[i] << endl; } return 0; }
------------------------------------
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 . . .
Last edited by JoeRoss578; Oct 16th, 2008 at 4:16 pm.
You only fill exercise[0]. Perhaps this would work better for you:
C++ Syntax (Toggle Plain Text)
for (i = 0; i < 5; i++) { cin >> exercise[i]; cout << exercise[i] << endl; }
I'm here to prove you wrong.
#include <iostream>
int main()
{
using namespace std; // out of place
int exercise[5], i;
cin >> exercise[0]; // is only entering one value
for (i = 0; i < 5; i++)
{
cout << exercise[i] << endl;
}
return 0;
}#include <iostream>
using namespace std;
int main()
{
int exercise[5];
for(int i=0; i<5; i++) // C++ arrays start with index 0, so its 0, 1, 2, 3, 4
{
cin >> exercise[i]; // index[0] gets 1, [1] gets 2, ...
cout << exercise[i]; // as you input, 12345, it will also output
}
for(int i=0; i<5; i++)
{
cout << element[i] << endl; // endl is endline (or a new line)
}
return 0;
} I would love to change the world, but they won't give me the source code
>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.
[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.
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.
[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.
I'm here to prove you wrong.
Neat stuff thanks for the feedback. I do appreciate lots of your posts. I learn a lot of neat things. Had no idea it was even legal to use a directive on a block scope.
Last edited by chococrack; Oct 16th, 2008 at 6:02 pm.
I would love to change the world, but they won't give me the source code
•
•
•
•
[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.
100% agreethats why I am writing the whole time
: c++ Syntax (Toggle Plain Text)
std::cout<<......std::endl; std::cin>>...;
![]() |
Similar Threads
- get length of a dynamic array (C++)
- how to print out an array in a message box (Java)
- MERGED: Deleting duplicates in an array (plz help me out!!!!!!!) (C)
- problem creating a for loop for an array (C++)
- twenty integers in an array (Java)
- How do I create a program using an Array ? (C++)
Other Threads in the C++ Forum
- Previous Thread: Inserting a number to an array
- Next Thread: Struct: Having trouble reading into my file
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






