while for looping back to text file for palindrome problem

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 3439 Views - Last Post: 12 October 2012 - 12:11 AM Rate Topic: -----

#1 amyceres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 11-October 12

while for looping back to text file for palindrome problem

Posted 11 October 2012 - 09:09 AM

I been playing around with this code for a couple of days. I still don't know where I went wrong with it. Have no problem with user input but when I throw in a text file to read off of it screw up the program.



Current Output :
5 Level 5 --A palindrome
Madam --A palindrome
ABBA --A palindrome
Able was I ere I saw elba --A palindrome
A Man, A Plan A Panama Canal --A palindrome
Palindrome--A palindrome
a&% $#()+@ V$)(* A --A palindrome



Right Output:
5 Level 5 --A palindrome
Madam --A palindrome
oppo --A palindrome
Able was I ere I saw elba --A palindrome
BABE --A palindrome
Palindrome--Not a palindrome
a&% $#()+@ V$)(* A --A palindrome




#include<iostream>
#include<fstream>
using namespace std;


int main()
{
char str[80];
int len = 0;


bool isPalindrome=true;
fstream myfile;
myfile.open("C:\\George.txt");


    
while(!	myfile.eof())
{
myfile.getline(str,80);
for(int i = 0; i <= len; i++) 
     {
   if (str[i] != str[len- i]) 
	{  
     isPalindrome=false;
     break; 
	}
     if(isPalindrome) 
	cout<<str<< "--A palindrome.\n"; 
	else cout <<str<< "--Not a palindrome.\n"; 
	  i++;   //This doesn't work...
}
}
system("Pause");



return 0;


 }

This post has been edited by jimblumberg: 11 October 2012 - 09:15 AM
Reason for edit:: Fixed Code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: while for looping back to text file for palindrome problem

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,439
  • Joined: 25-December 09

Re: while for looping back to text file for palindrome problem

Posted 11 October 2012 - 09:16 AM

Please post a copy of your input file that is causing problems.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 86
  • View blog
  • Posts: 189
  • Joined: 23-September 12

Re: while for looping back to text file for palindrome problem

Posted 11 October 2012 - 09:21 AM

You don't appear to be setting 'len' anywhere, hence everything is a palindrome. Also: you should only need to check to half way through the string--otherwise, you'll be doing each comparison twice. Finally, if this program is to be whitespace, case, and punctuation independent, you'll need to pre-process the strings (convert to uppercase, strip whitespace and punctuation) so it can accept things like:

Go hang a salami, I'm a lasagna hog! :)

Hope this helps. Good luck!
Was This Post Helpful? 1
  • +
  • -

#4 amyceres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 11-October 12

Re: while for looping back to text file for palindrome problem

Posted 11 October 2012 - 09:33 AM

View Postjimblumberg, on 11 October 2012 - 09:16 AM, said:

Please post a copy of your input file that is causing problems.

Jim


My input file is just a text file with the words.

5 Level 5
Madam
ABBA Able was I ere I saw elba
A Man, A Plan A Panama Canal
Palindrome
a&% $#()+@ V$)(* A
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,439
  • Joined: 25-December 09

Re: while for looping back to text file for palindrome problem

Posted 11 October 2012 - 10:20 AM

A couple of things that I noticed are that you are incrementing i twice, once in the for loop another in the loop body. And as already noted you are not properly implementing your for loop, len is zero so the for loop never executes. Also using eof() to control your entry loop will probably cause problems.

Quote

Have no problem with user input but when I throw in a text file to read off of it screw up the program.

I suggest that you post the code where you are using user input that you state is working. Maybe we can show you how to safely modify that program to work with your file.

Jim
Was This Post Helpful? 1
  • +
  • -

#6 amyceres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 11-October 12

Re: while for looping back to text file for palindrome problem

Posted 11 October 2012 - 11:02 AM

Thanks for taking your time for this.
Here is the working one...
#include<isotrean>
#include<fstream>
Using namespace std;
 
int main()
{ 
Char str[80];
Cout<<”enter a string!”;
Cin.getline(str.80);
Int len;
For (len=0; string[len] ! =’\0’; len++);
            Int i; 
            Int j; 
            bool isPalindrome=True;
For (i=0, j=0; i<len/2; i++, j--)
{
If(str[i] !=str[j])
{
isPalindrome=False;
Break;
}
}
If (isPalindrome)
Cout<<str<<"-- It’s a Palindrome.” <<endl;
Else
Cout<<str<<"--Not a Palindrome.” <<
 System (“pause”);
Return 0;
}


View Postjimblumberg, on 11 October 2012 - 10:20 AM, said:

A couple of things that I noticed are that you are incrementing i twice, once in the for loop another in the loop body. And as already noted you are not properly implementing your for loop, len is zero so the for loop never executes. Also using eof() to control your entry loop will probably cause problems.

Quote

Have no problem with user input but when I throw in a text file to read off of it screw up the program.

I suggest that you post the code where you are using user input that you state is working. Maybe we can show you how to safely modify that program to work with your file.

Jim


il]For (len=0; string[len] ! =’\0’; len++);[/il]
This is suppose to be

For (len=0; str[len] ! =’\0’; len++);[

This post has been edited by jimblumberg: 11 October 2012 - 11:04 AM
Reason for edit:: Added missing Code Tags, Please learn to use them.

Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,439
  • Joined: 25-December 09

Re: while for looping back to text file for palindrome problem

Posted 11 October 2012 - 11:08 AM

Please repost the working code in code tags not Abbreviated Line tags. Also note I stress the working code. The code you posted above is not working code. C/C++ is case sensitive and the above code seems to have the first character replaced with an upper case character.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 amyceres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 11-October 12

Re: while for looping back to text file for palindrome problem

Posted 11 October 2012 - 11:39 AM

View Postjimblumberg, on 11 October 2012 - 11:08 AM, said:

Please repost the working code in code tags not Abbreviated Line tags. Also note I stress the working code. The code you posted above is not working code. C/C++ is case sensitive and the above code seems to have the first character replaced with an upper case character.

Jim


sorry...keep on forgetting to put it into a code format
#include<isotrean>  
#include<fstream>  
using namespace std;  

int main()  

{   

char str[80];  

cout<<”Enter a string!”;  

cin.getline(str.80);  

int len;  
for(len=0; str[len] ! =’\0’; len++);  
int i;   
int j;   

bool isPalindrome=true;  

for(i=0, j=0; i<len/2; i++, j--)  

{  
if(str[i] !=str[j])  

{  
isPalindrome=false;  
Break;  

}  
}  

if (isPalindrome)  

cout<<str<<"-- It’s a Palindrome.” <<endl;  

else  
cout<<str<<"--Not a Palindrome.” <<  
system (“pause”);  

return 0;  

29 } 



29 at the end was a mistype

and the system("pause"); should be "Pause"
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,663
  • Joined: 23-August 08

Re: while for looping back to text file for palindrome problem

Posted 11 October 2012 - 11:44 AM

What are you using to type your code? It looks like a word processor, and you SHOULD NOT use a word processor to type code.
Was This Post Helpful? 0
  • +
  • -

#10 amyceres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 11-October 12

Re: while for looping back to text file for palindrome problem

Posted 11 October 2012 - 11:51 AM

View PostJackOfAllTrades, on 11 October 2012 - 11:44 AM, said:

What are you using to type your code? It looks like a word processor, and you SHOULD NOT use a word processor to type code.

Not trying to work with a processor...I just copy and paste so that I keep a record of it for later references...I know that it does automatically convert lowercase to uppercase.
Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,663
  • Joined: 23-August 08

Re: while for looping back to text file for palindrome problem

Posted 11 October 2012 - 11:53 AM

It's also adding funny quotations, which will cause problems if you try to compile with those in the text.
Was This Post Helpful? 0
  • +
  • -

#12 amyceres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 11-October 12

Re: while for looping back to text file for palindrome problem

Posted 11 October 2012 - 12:14 PM

#include<isotrean>    

#include<fstream>    

using namespace std;    
   
int main()       
{     

   
char str[80];    
cout<<"Enter a string!";    
cin.getline(str.80);    
   

int len;    

for(len=0; str[len] ! ='\0' len++);    

        int i;     

        int j;     
  
        bool isPalindrome=true;    
 
for(i=0, j=0; i<len/2; i++, j--)    
   
{  
      if(str[i] !=str[j])    
      {    
       isPalindrome=false;    
       break;    
      }    
}    
   

if (isPalindrome)   
cout<<str<<"-- It’s a Palindrome." <<endl;    
else cout<<str<<"--Not a Palindrome." <<    

system ("Pause");    

   
 return 0;    

} 

:stupid: :oops:
Was This Post Helpful? 0
  • +
  • -

#13 amyceres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 11-October 12

Re: while for looping back to text file for palindrome problem

Posted 11 October 2012 - 12:22 PM

View PostNathanMullenax, on 11 October 2012 - 09:21 AM, said:

You don't appear to be setting 'len' anywhere, hence everything is a palindrome. Also: you should only need to check to half way through the string--otherwise, you'll be doing each comparison twice. Finally, if this program is to be whitespace, case, and punctuation independent, you'll need to pre-process the strings (convert to uppercase, strip whitespace and punctuation) so it can accept things like:

Go hang a salami, I'm a lasagna hog! :)

Hope this helps. Good luck!

what do you mean I am not settig 'len' anywhere...I writing it as a variable..int len=0 unless you mean to set it to int len=str.len(); confuse here
Checking halfway through the string I thought I was doing that with the[ for if statement. ]
Was This Post Helpful? 0
  • +
  • -

#14 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,663
  • Joined: 23-August 08

Re: while for looping back to text file for palindrome problem

Posted 11 October 2012 - 01:49 PM

for(i=0, j=0; i<len/2; i++, j--) 


Think carefully where you want j to point when you start this loop.
Was This Post Helpful? 0
  • +
  • -

#15 amyceres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 11-October 12

Re: while for looping back to text file for palindrome problem

Posted 11 October 2012 - 02:21 PM

Don't I want it the"j" to go backward?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2