Hello fabstr1,
Some things not mentioned yet:
The C header file "<stdio.h>" should be "<cstdio>" for a C++ program. Even with that change you do not need it. The "<iostream>" covers everything that you need and it is possible that "iostream" may include "cstdio".
Your function defines:
double Bx[10000];
, but you say that there 6 lines of 10000+ columns. Not quite enough for a 1D array. Your description would suggest a 2D array. Next problem you may find is 10000 * 6 * 8 = 480000 bytes. You may run out of memory just trying to define the array. NO sure, but something to think about.
Next is the array is defined in the function, so when the function ends so does that array when it is destroyed along with any other variable(s) you define.
You define:
short loop = 0;
, but do you know what the largest number a short can hold. When I check it on my computer it says that a short is 2 bytes and a signed short can store a number between ( -32,768 to 32,767). That is a long way from 10000+.
if (myfile.is_open())
. Checks if the file is open, but it may be open and not usable. What you should do is something like this:
1 2 3 4 5 6
|
if (!myfile)
{
std::cerr << "\n File \"" << "Test.dat" << "\" did not open.";
return 1;
}
|
This way you can continue your program with out the if statement.
Your while loop will does not work the way that you are thinking. You are checking for "eof" before you read the file which might set the "eof" bit, then you will try to store a number at element 10001 which does not exist in your array.
Line 21 reads an entire line of the file and stores it as a "string".
Line 22 is trying to store a "std::string" into 1 element of the array that is defined as a double. This will not work.
And even if you would get to line 23 you will print an element of the array that contains a garbage value because the array was never initialized or receiver a proper value.
Loop will eventually roll over to a negative number when it exceeds (32,767) and you can not use a negative number to access an array. Even an unsigned short would go to ( 65,535).
Running most of "main" on an if/else may work, but it is not the best way to write a program. The if statement I showed you would come after line 17 and if it is true you would leave the program to deal with the problem. This removes access to the rest of the program that you do not need to continue with.
system("PAUSE");
works and I believe that lower case letters are what you need, but it may not be case sensitive.
I like using this in its place:
1 2 3 4 5 6 7
|
// A fair C++ replacement for "system("pause")". Or a way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0; // <--- Not required, but makes a good break point.
|
This is what I see for now.
Andy