Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

omegasyphon

array question

This topic is 6088 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

when i go to run the following code, nothing is displayed in my program but if i just hard coded the array it works fine. its reading in a file of just integers
    

	GLuint corners[1000];
	for (int x=0;x<1000;x++)
	{
		cin>>corners[x];
		cin>>corners[x+1];
		cin>>corners[x+2];
		cin>>corners[x+3];
	}

    
Edited by - omegasyphon on October 19, 2001 1:08:25 PM

Share this post


Link to post
Share on other sites
Advertisement
do you realize that the piece of code you have there would require you to input 4000 values ?
are you typing in these 4000 values ?
when you do
cin >> corners[x+whatever];
that means you must type in a value from the keyboard (or use command line redirection).


"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T

Share this post


Link to post
Share on other sites
Moreover, you erase previous values. Replace this :

for (int x=0;x<1000;x++)

By this :

for (int x=0;x<1000;x+=4)

But anyway, like said ncsu121978, you have to type these values on the keyboard.

Share this post


Link to post
Share on other sites
Also, when x == 999, and cin>>corners[x+1] executes, you''re writing off of the end of your array and into memory that''s not yours.

"If consquences dictate our course of action, it doesn''t matter what''s right, it''s only wrong if you get caught."
- Tool

Share this post


Link to post
Share on other sites
lastly...nothing is displayed because you''re not displaying anything....(in addition to all the seemingly "foolish" programming practices you''re doing)

Share this post


Link to post
Share on other sites
oops those should be fin not cin

so basically should this work?
im reading in a bunch of integers

for (int x=0;x<1000;x+=4)
{
fin>>corners[x];
fin>>corners[x+1];
fin>>corners[x+2];
fin>>corners[x+3];
}

Edited by - omegasyphon on October 19, 2001 1:09:13 PM

Share this post


Link to post
Share on other sites
quote:
Original post by ncsu121978
do you realize that the piece of code you have there would require you to input 4000 values ?

Um, 1003.


// doing this involves 4 additions
for( int x = 0; x < 1000; x += 4)
{
cin >> corners[x];
cin >> corners[x+1];
cin >> corners[x+2];
cin >> corners[x+3];
}

// Since you want to increase x anyway
for( int x = 0; x < 1000; )
{
cin >> corners[x++];
cin >> corners[x++];
cin >> corners[x++];
cin >> corners[x++];
}
// is exactly the same thing. Which means you've unrolled a loop
// that gives you absolutely no performance gain. Do this
// instead:
for( int x = 0; x < 1000; x++)
cin >> corners[x];

Bonus point: why are they the same?

I think you need to rethink what you're trying to do. And I don't think I've ever heard of fin (I suppose you're trying to read in from a file). Try this:

ifstream file("filename.ext");
for( int x = 0; x < 1000; x++ )
ifstream >> corners[x];


Edited by - Oluseyi on October 19, 2001 1:14:15 PM

Share this post


Link to post
Share on other sites
ok if i have a data file like so

8 9 7 8
3 4 5 6
3 5 6 2

trying to read that file in like this doesnt work

ifstream file("data.txt");
for (int x=0;x<15;x++)
{
ifstream>>ary[x];
}


Share this post


Link to post
Share on other sites
ok if i have a data file like so

8 9 7 8
3 4 5 6
3 5 6 2

trying to read that file in like this doesnt work

ifstream file("data.txt");
for (int x=0;x<12;x++)
{
ifstream>>ary[x];
}

it give a syntax error on reading into the array with the arrows


Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!