Hey guys, I have a really quick and hopefully easy question for you all.

I'm trying to read in and store some floating point numbers from a file into an array. The lines look like:

boom.txt:

v 0.000034 0.44556 0.34444
v 4.000 1.0 1.0
v 4.00056 1.2003030 4.000

And the code I currently have (kind of a hybrid of C++ and C, I'm trying to write an application using OpenGL but that's another story)

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;

int main() {
	
    float vert[3][3];
    float v1, v2, v3;
	char str[1024];
	int loopcounter = 0;
	FILE *myFile;
	myFile = fopen("boom.txt", "r");
	

	while(EOF != fscanf(myFile, "%s %f %f %f", str, &v1, 
                                &v2,&v3)) {
          vert[loopcounter][0] = v1; 
          vert[loopcounter][1] = v2;
          vert[loopcounter][2] = v3;
          loopcounter++;



		cout << vert[loopcounter][0] << " " << vert[loopcounter][1] << " " <<  vert[loopcounter][2] << endl;
	
	loopcounter++;
	
    }
    
	fclose(myFile);
	
    system("pause");

}

This is producing some weird results:

3.94739e+033 0 0
4.42113e-039 4.62428e-044 2.8026e-045
5.88389-039 1.4013e-045 7.26043e-039

Can anyone see the problem here, why would it be producing this result? Any help would be greatly appreciated.

Thanks

Edit: I think it has something to do with the way I am assigning vert[loopcounter][0] = v1, etc. If I print out the values of v1, v2, and v3 they are correct. Are you unable to assign values to arrays like that in C?

When you output the values, you've already incremented loopcounter past the values you just loaded... You're outputting garbage.

Ah, you're totally right. After staring at that code for over 30 minutes, I guess my eyes just couldn't see that. Thank you very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.