Hello ejulien18,
First it really helps to post a full program that can be compiled and run.
Second if the program uses an input file include that with your post. Or at least a good sample including any part of the file that is a problem. It helps everyone to be able to use the same information and not have to guess at it.
My first question is the character pointer and array that you create required for the assignment? Or is this just something that you have used before?
You have used "new" to create an array, but you did not use "delete" to free the memory when you are finished. This leaves you with a memory leak.
Although it works putting the progam in an if/else statement it really is bad form and might limit what you can do.
When dealing with an input file I like to use this:
1 2 3 4 5 6 7 8 9 10 11
|
const std::string inFileName{ "" }; // <--- Put File name here.
std::ifstream inFile(inFileName);
if (!inFile)
{
std::cout << "\n File " << std::quoted(inFileName) << " did not open." << std::endl;
//std::cout << "\n File \"" << inFileName << "\" did not open." << std::endl;
return 1;
}
|
This way if the stream is not open and usable you leave the program to fix the problem. Also if the file does not open there is no reason to continue with the program. This will also eliminate the need for the if/else statements.
The while loop on line 26 does not work the way that you are thinking it does. By the time the while condition becomes false you will have processed that you could not read.
On line 38 you need to use 256 as the "getline" will take up to 255 or (256 - 1) leaving the last element of the array for the (\0) to terminate the string. Here you are cutting your-self 1 short of what you could store in the array.
Something does not look right with line 40, but I am not sure what that is yet.
Andy