0

Hi everyone.

I'm extremely new to this. It's my first programming course, period. I realize I probably needed a load of pre-reqs, but for some reason, I was advised to take Intro to C++ so...

So far, I've been doing well! However, this latest project has me stumped...

The jist of it:

Read lines from an input file, reverse, compare, find palindrome, and write it back out to the input file.

So far:

#include<fstream>
#include<iostream>
#include<string>
#include<cctype>
usingnamespace std;
#define SIZE 80
char palinCan[SIZE+1];
char reverse[SIZE+1];
int i = 1,a = 0, j ,q = 0,num = 0;
char reversal(char palinCan[SIZE]);
char b;
 
int main()
{
//int palindrome(char[]);
 
ifstream in;
ifstream infile;
infile.open("file.txt.txt", ios::in);
if (infile.fail())
{
cerr << "Couldn't open file..." << endl;
return 0;
}
do{
 
infile.getline(palinCan, SIZE+1);
strcpy_s(reverse, palinCan);
/* if(palindrome(reverse))
cout << storage << reverse << "It's a palindrome" << endl; 
else 
cout << "Nope" << endl; // I am getting "does not evaluate to function taking #of arguments here */
cout << palinCan << reverse << endl;
 
 
}while (!infile.eof());
}
char reversal()
{
for(j = strlen(palinCan); j >= 0 ; j--){
cin >> reverse[SIZE];}
return reverse[SIZE];
}

I don't know how to make a functioning... function to reverse the loop and return the result into the reversal array for comparison. I've tried a few variations, but none of them worked.

Once I have the reverse of the string stored in the reverse array, I should be able to make a comparison function with it, and have it return a simple true or false.

I appreciate any help...

2
Contributors
5
Replies
6
Views
10 Years
Discussion Span
Last Post by Ancient Dragon
0

This is a c++ program, why are you using c-style char arrays. Unless your instructore requires it you should probably be using std::string as input buffer.

You do loop is all wrong. Use a while loop instead for better control.

std::string inbuf;
while( getline(infile,inbuf) )
{
   // blabla
}

now all you have to do is creae another std::string object and copy the caracters backwards from inbuf into it.

Votes + Comments
Good job, Ancient Dragon :) --joeprogrammer
0

This is a c++ program, why are you using c-style char arrays. Unless your instructore requires it you should probably be using std::string as input buffer.

You do loop is all wrong. Use a while loop instead for better control.

std::string inbuf;
while( getline(infile,inbuf) )
{
   // blabla
}

now all you have to do is creae another std::string object and copy the caracters backwards from inbuf into it.

How do I copy the reverse/backwards into it though? That's the primary problem I'm having now... getting the reverse of a string and storing that for comparison.

0

do you have to reverse just the words?
"Hello World" becomes "World Hello"

or reverse all the letters? Reversing the letters is easy -- reversing words becomes a little more complex. As I mentioned in my previous post, you need to create a second string and copy the characters to it in reverse order -- create a loop that starts at the end of the string and decrement instead of increment the loop counter. For example:

for(int i = inbuf.length()-1; i >= 0; --i)
{

}
0

do you have to reverse just the words?
"Hello World" becomes "World Hello"

or reverse all the letters? Reversing the letters is easy -- reversing words becomes a little more complex.

the letters. The letters have to be reversed so that it can be compared to the normal line.

0

the letters. The letters have to be reversed so that it can be compared to the normal line.

Good -- that simplifies the program. See my previous post. The rest you will have to figure out because I don't want to write your program for you.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.