Hello kuushie118,
The first six lines are OK except for line 4 which is best not to use.
Then it falls apart in "main".
You define two files as an "fstream", but then you try to open three files. The third file is not even defined and all three open statements are wrong.
When you define a file stream as an "fstream" it can be used either for input or output, so in the open statements you need to tell it if it is for input or output. As in:
1 2 3
|
eastfile.open("eastcoast.dat", std::ios::in);
westfile.open("westcoast.dat", std::ios::in);
mergedfile.open("mergedfile.dat", std::ios::out);
|
If my guess is correct.
Now when you include "#include <fstream>" it will include the header files "ifstream" for input and "ofstream" for output, so what you could have done is:
1 2 3
|
ifstream eastfile;
ifstream westfile;
ofstream mergefile;
|
Since they are already defined for input and output all the open statement needs is the file name.
And do not forget to define the "mergefile" before you open it.
When you open a file for input you should check to make sure it is open before you try to use it. The simplest way is:
1 2 3 4 5 6 7
|
if (!eastfile)
{
std::cout << "\nyour error message\n"<<std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3)); // Requires header files "chrono" and "thread"
return 1; // <--- Leave the program becaust there is no reason to continue until you fix the
// problem. The "1" or any number greater than zero means there is a problem.
}
|
You will need to do this for both files. Change the if condition for the other file.
The test for the output file is at you choice. Its not really needed because if the file name does not exist it will create it, so there is not anything I have seen yet that will cause an open for output to fail.
Lines 20 - 50 is a function and should not be in main, but either above "main" or below"main". If below "main" you will need a proto type above main.
When I moved the function "getReady()" out side of "main" I notice that it contains three other functions. This is not right. Each function needs to be a stand alone function.
I also noticed that the "getReady function tries to open files that are already open. You need to either define and open file streams where they are needed or pass the file streams to the function.
Let me back up a bit. Lines 13, 15,17 and 18 are almost correct. You qualify them as being "const" and have a variable name, but you missed the type for the variable. Also when defining a "const" it is best to make the variable name all caps to remind you that it is defined as a "const".
When I was going over the function "checkEnd" I notices two things:
Your code:
1 2 3 4 5 6 7 8
|
checkEnd()
{
if(eastName == END_NAME)
if(westName == END_NAME)
{
areBothAtEnd = "Y"
}
}
|
This may work, but it is more often written as:
1 2 3 4 5
|
checkEnd()
{
if (eastName == END_NAME && (westName == END_NAME)
areBothAtEnd = "Y"
}
|
For a one line statement following the if statement the {}s are not need unless you plan an adding something else later.
The big problem here is that "areBothAtEnd" is not available to the function because it is defined back in "main" and being defined as a "const" it can not be changed by the program.
in the functions "readEast" and "readWest" you are taking input from the keyboard did you mean to get this information from a file?
Also both the "readEast" and "readWest" functions are trying to give two variables a new value, but neither function has access to those variables because they are defined back in "main".
And please do not make your variable global just to solve the problem there are better ways to do this.
You said that you started with pseudocode to write the program I am curious to know what it looks like if you could post it please and any file used for input or a decent sample of the file so I know what you have to work with. Reading an input file all depends on what the file looks like.
You have some good pieces and for the most part a good start, but the code you posted need major rework to make it a usable program.
I will work on it for a little while tonight and have something tomorrow. I do not know how much you know about C++, so if you want to take some time tonight you can start at:
http://www.cplusplus.com/doc/tutorial/
You should have a look at:
Variables and types
Constants
Almost at the bottom of the page is "Typed constant expressions"
Basic Input/Output
Control Structures
Functions
|
That should keep you busy for awhile and give me a chance to work on the program.
Hope that helps,
Andy