3

Hello I'm new to c++ and can't figure out why my code is doing this. I've scoured across the internet and can't find the solution I need. I appreciate all the help. The line that's giving me the problem is when I'm calling the function. According to visual studios it states that "argument of type 'char*' is incompatible with parameter of type 'char**'". It's referring to newArr.

#include <iostream>
#include <string>
#include <stdio.h>
#include <ctype.h>
#include "stdafx.h"

using namespace std;

bool isPalindrome(char *newArr[], int);

//int i = 0;
//char phrase; 
//char c;
bool palindrome;
bool tOf;
int numb;
char c;

const int length = 80; //const so that it can't be changed
char inarr[length]; //array set to a const length of 80
char newArr[length]; //array that will have no spaces

string str;

int main()
{

    cout << "This program tests if a word/phrase is palindrome.\n\n";
    cout << "Please enter your phrase (just letters and blanks, 
please):\n\n";

    cin.getline(inarr, length);
    //cout << array; //spits out the array
    str = inarr; //turn into string
    numb = str.length();
    //cout << numb << "\n"; //how many characters in array

    for (int i = 0; i < (numb / 2) + 1; i++)
    {

        for (int j = 0; j < (numb / 2) + 1; j++)
        {
            newArr[j] = inarr[i];   //from old array to new array   

            c = newArr[j];
            newArr[j] = toupper(c); //change to all upper case

                                //cout << newArr[j];
            i += 2; //goes to every other index to skip space in string
        }

    }

    tOf = isPalindrome(newArr, numb); //calling of function

    if (tOf == true) //the response to true or false
    {
        cout << "\nYes, the phrase is a palindrome!";
    }
    else
    {
        cout << "\nNo, the phrase is not a palindrome!";
    }

    return 0;

}

bool isPalindrome(char *newArr[], int numb) //function to determine true or 
false
{
    for (int i = 0; i < (numb / 2) + 1; i++) //within the array...
    {
        if (newArr[i] != newArr[(numb / 2) - i]) //if first index != last 
and etc (iterates)
        {
            palindrome = false;
        }
        else
        {
            palindrome = true;
        }
    }
    return palindrome;
}
Maina Aoita
  • 35
  • 1
  • 1
  • 5
  • 1
    Since this is C++ why don’t you just use the string? There’s no need for a char array, you don’t need to send the length separately and you wouldn’t get these errors. In any case fix the function definition. It doesn’t expect a pointer to array, it expects a pointer. – Sami Kuhmonen Oct 19 '17 at 05:22

2 Answers2

3

You're trying to pass newArr (a char *) into isPalindrome() (which takes a char **). This is what "argument of type 'char*' is incompatible with parameter of type 'char**'" means.

To fix this, simply pass in a char **; you can do this by passing in the address of newArr instead of newArr itself:

tOf = isPalindrome(&newArr, numb); //calling of function
frslm
  • 2,969
  • 3
  • 13
  • 26
  • Doing this would cause undefined behavior since the code actually expects a `char*` as the parameter. The function definition is wrong. – Sami Kuhmonen Oct 19 '17 at 05:20
  • 1
    Yep, OP would need to either dereference `newArr` in `isPalindrome()` or change the function definition. – frslm Oct 19 '17 at 05:22
  • 1
    I dereferenced it and it worked on online compilers but visual studios didn't like it. But it works :) Thank you for the suggestion! – Maina Aoita Oct 19 '17 at 05:31
1

Brief Change the function signature (both definition and declaration) of the function to

bool isPalindrome(char* newArr, int numb);

Call it tOf = isPalindrome(newArr, numb);

Detail

If you call isPalindrome(newArr, numb). you are passing address of the first element either &newArr[0] . So you are function defination should be able to pick the address of the element. hence *newArr

Further your function will validate the details by using array arithmetic. which is all right .

Output

$ ./a.out
This program tests if a word/phrase is palindrome.

Please enter your phrase (just letters and blanks, please):

Palindrome

No, the phrase is not a palindrome!
$ ./a.out
This program tests if a word/phrase is palindrome.

Please enter your phrase (just letters and blanks, please):

YeseY

Yes, the phrase is a palindrome!

$

Seshadri VS
  • 550
  • 1
  • 6
  • 24