Here are the directions, I sort of answered a couple out of order, my code won't run though? I am not receiving any error, just blankness. If someone could aid me in tidying up the code, that would be awesome. First, prompt the user for the names of the two files (2 points). Then open each file (2 points). Remember to check for failure (2 points). Keep reading a line from each of them. Increment a line number counter (2 points). If both inputs fail, print a message indicating that both files are identical (2 points). If one of the inputs fails and the other does not, print a message indicating which file is shorter, then exit (2 points). If the two input lines are different, print the line number counter and both input lines, then exit (2 points). If the two input lines are identical, keep on reading (2 points). Plus 4 points for overall structure and style.
Oops, didn't mean to add that, there I fixed it. Now I am getting 49:28: error: ‘strcmp’ was not declared in this scope
Can someone please help me fix my code up to the directions, I've done a fair amount of work learning how to do this, then implementing, and am dying to get this done.
You need to reset the file pointer for Mary1, just like you did for Mary2.
I added some basic string-file operation to your code, you should be able to figure out the rest. the only part that was tricky is making sure you use c-style constant strings when you're opening the files with a string type.
So basically, I've got a small part of the problem correct. Your code above is cleaner, I am just unsure of how to do the rest of the problem. My partner is also very confused on the issue as well, again my points of interests are:
If both inputs fail, print a message indicating that both files are identical (2 points).
If one of the inputs fails and the other does not, print a message indicating which file is shorter, then exit (2 points).
If the two input lines are different, print the line number counter and both input lines, then exit (2 points).
If the two input lines are identical, keep on reading (2 points)..
I am unsure on what it means 'if one of the inputs fails and the other does not, print a message indicating which file is shorter, then exit. And actually the rest of the above instructions, HELP PLEASE??
nahla,
I'm going to add these hints for you, but please try to take the time to research on file operations and string comparisons. your code is not far away from completion.
I've taken out your code and just put in the parts that you need:
plus put my comments on where your point tally is coming from.
I'm following the instructions to heart eventhough it doesnt make complete sense to me.
#include <iostream>
#include <fstream>
#include <string.h>
#include <ostream>
#include <stdio.h>
#include <string>
usingnamespace std;
int main()
{
//Open the two files//
string file1, file2;
ifstream Mary1, Mary2;
// file1="Mary1.txt" ;
// file2="Mary2.txt" ;
//First, prompt the user for the names of the two files (2 points).
cout << "Enter the name for 1st file:" << endl;
cin >> file1;
cout << "Enter the name for 2nd file:" << endl;
cin >> file2;
//Then open each file (2 points)
Mary1.open( file1.c_str(), ios::binary ); //c_str() returns C-style string pointer
Mary2.open( file2.c_str(), ios::binary );
//Remember to check for failure (2 points)
if (Mary1.fail())
{
cout << "Couldn't open the file " << file1 <<endl;
}
if (Mary2.fail())
{
cout << "Couldn't open the file " << file2 << endl;
}
if (file1 == file2)
{
cout << "files are identical" << endl;
return 0;
}
//If both inputs fail, print a message indicating that both files are identical (2 points). this is REALLY what bothers me.
if (Mary2.fail() && (Mary1.fail()) )
{
cout << "both files are identical" << endl;
return -1;
}
//If one of the inputs fails and the other does not, print a message indicating which file is shorter, then exit (2 points). This makes some sense, but not really.
if (Mary1.fail() and !Mary2.fail() )
{
cout << file1 << " is shorter." << endl;
return -1;
}
if (Mary2.fail() and !Mary1.fail() )
{
cout <<file2 << " is shorter." << endl;
return -1;
}
/* YOUR CODE HERE*/
/*Keep reading a line from each of them. Increment a line number counter (2 points).
Keep reading a line from each of them. Increment a line number counter (2 points).
If the two input lines are different, print the line number counter and both input lines,
then exit (2 points). If the two input lines are identical, keep on reading (2 points).
*/
return 0;
}