I need to take my last C++ class over one more time and just trying to get a head start on the programs we had been assigned. I will provide the instructions and was wanting to get any feedback on what I should get rid of my current program and will probably look for additional help picking apart this program. The first thing I was wanting to know is if memory-mapped file input/output is set up correctly. Here are the instructions...
Create a utility that transfers words that are palindromes in the input file into the output file. Place a space between each palindrome written to the output file.
The input path and file name and the output path and file name must be gathered at runtime from the user. This can be accomplished via command line inputs (see argv and argc parameters in the main() function).
The program must use memory-mapped file input/output.
The contents of the input file must not be changed by your program (read only).
The definition of a palindrome, for purposes of this programming assignment, is a word which reads the same backward or forward. Examples of palindromes are mom, pop, kayak, noon, radar, and racecar.
While there are a number of methods available to test to see if a word is a palindrome, the most common method is to use a stack. Character[0] of the original string can be compared with character[n-1]. The next comparison would address character[1] with character[n-2]. Comparison continues until all characters are compared (the stack is empty) or there is an inequality (the word is not a palindrome).
The output file should contain only those words that are palindromes from the input file. Each word in the output file should be separated by a space.
Since the length of the output file is determined at runtime, and there is no way to predict how many palindromes will be present in the input file (without actually performing the test), there is no means to accurately predict the output file size required. Set the output file size to the input file size.
Here is my code...
#include<iostream>
#include <fstream>
#include<string>
using namespace std;
//FUNCTIONS
bool isPalindrome(string);
//begin int main
int main(int argc, char * argv[])
{
//Declare variables
ifstream in;
ofstream out;
bool palin;
string data;
//open the file
in.open(argv[1]);
//Statement if the file does not open
if(in.fail())
{
//Cout statement to show the error
cout<<"File not opening!" << endl;
//Make a pause
system("pause");
//Return a value
return 1;
}
//Open the file
out.open(argv[2]);
in>>data;
//Perform while statement
while(in)
{
palin=isPalindrome(data);
if(palin)
out<<data<<" ";
in>>data;
}
//Close the in
in.close();
//Close the out
out.close();
}
//Function to check Palindrome
bool isPalindrome(string input)
{
//Declare variables
int i=0,j;
bool y=false;
j=input.length()-1;
//While statment to check i and j
while(input[i]==input[j]&&i<=j)
{
i++;
j--;
}
//If statement to check if i > j
if(i>j)
//Show that y is true
y=true;
//Return y
return y;
}