I need some serious help as I am on a deadline for tomorrow afternoon. I will give the instructions for the program followed by the code that I have... 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. You may use the Windows Platform SDK (Win32), the Active Template Library (ATL), and the Standard Template Library (STL). You may use a managed or unmanaged project.

#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;
}

Several worring things here , but I am going to pick on one detail.
Your wrote this:

while(input[i]==input[j] && i<=j)
{
  i++;
  j--;
}

Now what happens with the simple word "a".
Let us see: First i is set to zero and so is j.
The while statements says that both input[0]==input[0] :) and that i<=j since both are zero.

Next i is increase and j is decreased:
Now what happens, now you are searching for input[-1]. That is converted into the operator[] for string, which takes an unsigned integer. Note that -1 is going to be a very very big number. At that point you are accessing memory that doesn't exist and you are hoping that nothing goes wrong -- but it can.

Fix the loop...

p.s. does your program pick up the last word in the file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.