NinetyNineee
NinetyNineee

Reputation: 15

Palindrome boolean not checking false

For some reason my boolean is not checking false, can anyone help me figure out why? Also how would I go about making this program ignore spaces if it were a phrase (i.e. "never odd or even")

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main() 
{
   string input;
   int i = 0;
   bool flag = true;
   int size = input.size();
   
   cin >> input;
   
   for (i = 0; i < size; i++)
   {
      if (input.at(i) != input.at(size - i - 1))
      {
         flag = false;  
      }   
   }   

   if (flag = true)
   {
      cout << "palindrome: " << input << endl;  
   }
   else
   {
      cout << "not a palindrome: " << input << endl;   
   }   

   return 0;
}

Upvotes: 1

Views: 45

Answers (1)

Will Walsh
Will Walsh

Reputation: 2054

New answer

You are getting the size of the string before the user has entered the string.

Change

int size = input.size();

cin >> input;

to

cin >> input:

int size = input.size();

Previous answer

You are using a single = rather than two in the if statement.

Change

if (flag = true)

to

if (flag == true)

Upvotes: 1

Related Questions