943,712 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 737
  • C++ RSS
Oct 16th, 2008
0

Having problem with array in for loop...

Expand Post »
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!

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. using namespace std;
  6. int exercise[5], i;
  7.  
  8. cin >> exercise[0];
  9.  
  10. for (i = 0; i < 5; i++)
  11. {
  12. cout << exercise[i] << endl;
  13. }
  14.  
  15. return 0;
  16.  
  17. }

------------------------------------
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JoeRoss578 is offline Offline
2 posts
since Oct 2008
Oct 16th, 2008
1

Re: Having problem with array in for loop...

You only fill exercise[0]. Perhaps this would work better for you:
C++ Syntax (Toggle Plain Text)
  1. for (i = 0; i < 5; i++)
  2. {
  3. cin >> exercise[i];
  4. cout << exercise[i] << endl;
  5. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 16th, 2008
0

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JoeRoss578 is offline Offline
2 posts
since Oct 2008
Oct 16th, 2008
1

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 16th, 2008
0

Re: Having problem with array in for loop...

#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;
}
Reputation Points: 92
Solved Threads: 16
Junior Poster
chococrack is offline Offline
149 posts
since Oct 2008
Oct 16th, 2008
2

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.

[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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 16th, 2008
0

Re: Having problem with array in for loop...

Click to Expand / Collapse  Quote originally posted by Narue ...
>using namespace std; // out of place
Actually, that's a better place than your proposed solution.
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.
Reputation Points: 92
Solved Threads: 16
Junior Poster
chococrack is offline Offline
149 posts
since Oct 2008
Oct 17th, 2008
0

Re: Having problem with array in for loop...

Quote ...
[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.
EXACTLY 100% agree
thats why I am writing the whole time :
c++ Syntax (Toggle Plain Text)
  1. std::cout<<......std::endl;
  2. std::cin>>...;
Reputation Points: 46
Solved Threads: 8
Junior Poster
sidatra79 is offline Offline
114 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Inserting a number to an array
Next Thread in C++ Forum Timeline: Struct: Having trouble reading into my file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC