overflow in implicit constant conversion and recursive functions

My program is suppose to take user input and determine if it is a palindrome (using recursive functions).
First: I'm getting an error warning:
in function int main()
overflow in implicit constant conversion
(it is related to the int last = string::npos; (declaration)

Secondly I need to ask the user if they want to check if the string is a palindrome or a reverse string.
1. I need to create a reverse_string() recursive function to take that same user input and print it out in reverse.
2.output should look like this as examples:

"do you want to reverse a string(1), check if it is a palindrome(2) and anything else quit?": 1 (example input)
Enter your string: Hello World (example input)
reversed string is: dlroW olleH

"do you want to reverse a string(1), check if it is a palindrome(2) and anything else quit?": 2
Enter your string: madam
madam is a palindrome

"do you want to reverse a string(1), check if it is a palindrome(2) and anything else quit?": 2
Enter your string: able
able is not a palindrome.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<iostream>
#include<string>

using namespace std;

string remove_char(string);
bool is_palindrome(string&, int, int);
string reverse_string(string); //need to create a recursive function to also reverse the string and printout

int main()
{
    string userInput;
    string sentence2;
    int first = 0
    int last = string ::npos;

   cout<< " Please enter a word or sentence, I will determine if it is a palindrome or not:" <<endl;
   getline(cin, userInput);

   sentence2 = remove_char(userInput);

   cout<< (is_palindrome(sentence2, first, last) ? " is a palindrome." : " is not a palindrome.") << endl;
   cin.getline();

   return 0;
}
string remove_char(string userInput)
{
    string sentence 2;

    for (unsigned int x = 0; x < userInput.length(); x++)
    {
      if (isalnum(userInput[x]))
      {
        sentence2 += userInput[x];
      }
    }
    return sentence2;
}
bool is_palindrome(string& sentence2, int first = 0, int last = string::npos)
{
   if(last == string::npos)
   {
       last = (sentence2.length()-1);
   }
   if (sentence2[first] == sentence2[last])
   {
     if ((first-lst)== 0)
     {
       return true;
     }
     else if (first ==(last-1))
     { 
         return true;
     }
     else
     {
         return is_palindrome(sentence2, first+1, last-1);
     }
   }
 else
   {
     return false;
   }
}
overflow in implicit constant conversion
(it is related to the int last = string::npos;

the type of std::string::npos is std::string::size_type, typically an alias to std::size_t. It is not int and cannot fit in an int (usually), hence overflow.
one of my classmates was able to code it as
int last = string::npos; with no problems

I guess I don't understand why I am having issues with this...?
I guess I don't understand why I am having issues with this...?

Are you using the same version of the compiler?

GCC doesn't complain about sign conversion by default when compiling C++ (you need to enable this using -Wsign-conversion), so I'm guessing you're using the 64-bit version of the compiler. In that case, it's not just the case that string::size_type (or size_t) is unsigned while int is signed, but that size_type is 64-bit whereas int is still 32-bit.

Incidentally, I like this warning; so I add -Wsign-conversion to my compile options (-Wall -pedantic -Wextra "alone" is not enough to include this option.)

Andy

PS int is still 32-bit on (most?) 64-bit Linux as they use the LP64 data model.

Data model
http://www.viva64.com/en/t/0012/

Last edited on
okay thank you,

so should i code it as

long double last = string::npos;

or what is your suggestion.
yes I am using linux and putty is the compiler.

Thanks!
No, you need an integer type, not any kind of double.

string::size_type last = string::npos;

or

size_t last = string::npos;

(by default, string::size_type is a typedef of size_t. std::string uses the same type as the memory allocator it's using; the standard allocator uses size_t.)

Andy

PS putty is not a compiler:
http://en.wikipedia.org/wiki/PuTTY
PuTTY is a free and open-source terminal emulator, serial console and network file transfer application.
I would typed this for the variable declaration?

because that is where the error is int main()
where I declared int last = string::npos;

I will try the size_t last = string::npos; as the variable declaration in main()
It worked!! Thank you.

so now I need to work on the second part which is:
I need to ask the user if they want to check if the string is a palindrome or a reverse string.
1. I need to create a reverse_string() recursive function to take that same user input and print it out in reverse.
2.output should look like this as examples:

"do you want to reverse a string(1), check if it is a palindrome(2) and anything else quit?": 1 (example input)
Enter your string: Hello World (example input)
reversed string is: dlroW olleH

"do you want to reverse a string(1), check if it is a palindrome(2) and anything else quit?": 2
Enter your string: madam
madam is a palindrome

"do you want to reverse a string(1), check if it is a palindrome(2) and anything else quit?": 2
Enter your string: able
able is not a palindrome.

Here is what I ave done so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include<iostream>
 #include<string>

 using namespace std;

 string remove_char(string);
 bool is_palindrome(string&, int, int);
string reverse();
 int main()
 {
         string userInput;
         string sentence2;
         int first =0;
         size_t  last = string::npos;
         string  answer = ‘yes’;
         int userchoice;

while (answer == ‘yes’)
         cout<< "Do you want to reverse a string(1), determine if it is a palindrome(2) or quit:"<<endl;
	cin>>userchoice<< endl;
        if ((userchoice !=- “1”) && ( userchoice != “2”))
        {	
    	cout>>“Game over!”>>endl;
	answer = “no”;
	return 0;
      }
        Cout<< “Please enter your text:”<< endl;
        getline (cin, userInput);
	if (userchoice =”1”)
	{
              /*call reverse function here*/
	}
	Else
	{
	/*Palindrome function here */
	}
	Cout<< “Would you like to try again, yes or no?” <<endl;
	Cin>> answer;
} 
         sentence2 = remove_char(userInput);

         cout<< (is_palindrome(sentence2, first, last) ? " is a palindrome.":" is not a palindrome.") <<endl;

         cin.get();

         return 0;
 }
 string remove_char(string userInput)
 {
         string sentence2;

         for (unsigned int x=0; x < userInput.length(); x++)
         {
                 if (isalnum(userInput[x]))
                 {
                         sentence2 += userInput[x];

                 }
        }
         return sentence2;
 }
 bool is_palindrome(string& sentence2, int first = 0, int last = string::npos)
 {
     if (last == string::npos)
         {
                 last = (sentence2.length()-1);
         }
         if (sentence2[first]== sentence2[last])
         {
                 if ((first-last)==0)
                 {
                    return true;
                 }
                 else if (first ==(last-1))
                {
                         return true;
                 }
                 else
                 {
                         return is_palindrome(sentence2, first+1, last-1);
                }
         }
         else
         {
                 return false;
         }
 }
String reverse()
{
   //need to figure this part out
}


Edit: there are a coupleof errors ie Cout instead of cout. I am aware of those.

I need to use the toupper() or tolower() functions to change characters to whichever case you want. Somehow I have to incorporate these to ingore the case sensitive problem.
Last edited on
Topic archived. No new replies allowed.