array question

Started by
13 comments, last by omegasyphon 22 years, 6 months ago
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
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
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.
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

"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
lastly...nothing is displayed because you''re not displaying anything....(in addition to all the seemingly "foolish" programming practices you''re doing)
"Success is not the result of spontaneous combustion; you must set yourself on fire."
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
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 additionsfor( 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 anywayfor( 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
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];
}


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


How is your ary defined?

ifstream supports a get() function for reading multiple characters. Look it up.

This topic is closed to new replies.

Advertisement