C++ strings error

Status
Not open for further replies.
I am getting a error that states Cannot convert from String to char...

any help?



Code:
/*
Jonathan Thomas 
ITSE 2421 intro to object C++
 begin sudo code:
      function   takes in string variable called "verb"
      function uses string  function verb.length to determine length of string assigns value to size
      compares last value in the string whether it is a consanant or an E 
      function convert string to array
      converts back to string
      if last letter  is 'e' the letter will be dropped and replaced with 'ing' using verb.replace(size,3,ing)
      

*/

#include<iostream>
#include<string>
using namespace std;
void createParticiple(string);
bool isVowel(char);
int main()
{
    string verb;
   
           cout<<"this prgram conjugates verbs into the past participle"<<endl;
           cout<<"please enter a verb in lower case letters NO CAPITOLS!!!"<<endl;
           getline(cin, verb);
           
           createParticiple(verb);
           
           system("pause");
}//end of line
void createParticiple(string verb)
{ 
     int size= verb.length();
   string s2;
    string LL;
    string NTL;
    char *lastLetter[1];
    char *nextToLast[1];
     
        LL= verb.substr( size-1, 1);
        NTL= verb.substr(size-2,1);
       
       
      lastLetter=&LL[0];
      nextToLast=&NTL[0];
        
        
      
       if (*lastLetter== "e" && isVowel==false )
       {verb.append(size-1, "ing");};
       
       if (isVowel==false && *lastLetter!="e" )
       {verb.append(size-2, nextToLast);
         verb.append(size, "ing") ;}
       
        
               
     
            
}
bool isVowel(char nextToLast)
{ 
     if (nextToLast=='a' || nextToLast=='e' || nextToLast=='i' || nextToLast=='o' || nextToLast=='u')
      { return true;}
      else {return false;}
}
 

stickboy

Ars Tribunus Angusticlavius
6,218
Subscriptor
It would help if you gave the exact error message (including the line number).

I see two immediate problems:

Code:
    string LL;
    string NTL;
 
    char *lastLetter[1];
    char *nextToLast[1];
    ...
    lastLetter=&LL[0];
    nextToLast=&NTL[0];

Why are lastLetter and nextToLast 1-element arrays of pointer-to-char? &LL[0] will yield a pointer-to-char, so there's a type mismatch.

If you want to get the last letter and the next to last letter, you can do:

Code:
    char lastLetter = verb[verb.size() - 1];
    char nextToLast = verb[verb.size() - 2];

(after verifying that verb.size() >= 2, of course.)

Code:
    if (*lastLetter== "e" && isVowel==false )
    ...
    if (isVowel==false && *lastLetter!="e" )
Unless you meant to declare a local bool variable named isVowel but forgot, isVowel is a function.
 
Here are some compiler warnings that will help you once you get the errors sorted out:
Code:
tmp.cpp:50:20: warning: result of comparison against a string literal is unspecified (use strcmp instead)
    if (*lastLetter== "e" && isVowel==false )
                   ^~ ~~~
tmp.cpp:53:38: warning: result of comparison against a string literal is unspecified (use strcmp instead)
    if (isVowel==false && *lastLetter!="e" )
                                     ^~~~~
You probably want to compare characters instead of strings:
Code:
if(lastLetter == 'e')
...
But you'll have to fix the types up for that to work.
 

Leaf

Ars Scholae Palatinae
939
Subscriptor
I'm not sure if you were taught this way, or if you're just coding C++ with some C-style habits.

Code:
   string LL;
   string NTL;
   char *lastLetter[1];
   char *nextToLast[1];

   LL= verb.substr( size-1, 1);
   NTL= verb.substr(size-2,1);

Variables don't need to be declared at the top of a function like in C. With C++ you should declare variables when they are first used, provided that they are within scope of everything that will use said variable. For example:

Code:
   string LL = verb.substr( size-1, 1 );
   string NTL = verb.substr( size-2, 1 );

Also, declaring a pointer array with only a single element doesn't make much sense to me, it could just be a char*. Are you required to use a character pointer for this assignment? As your code is right now, I don't see why you wouldn't simply use a char for lastLetter and nextToLast.

Code:
if (*lastLetter== "e" && isVowel==false )
   {verb.append(size-1, "ing");};
As was stated before, don't compare chars with strings, so use single quotes around the 'e'. IsVowel is a function, so you need to pass it the required parameters. Also, you need to make sure you're using string's append function right. If you look at the function definitions, there is only one case where an integer can be the first parameter, and that case requires that a character (not a string), be the 2nd parameter. This is used for appending a repeated instance of the character n times, which is not what you're trying to do in the above usage of append.

Also, there's little point IMO to use the curly braces in an if-block if you are just going to condense them the same line as the body of the if-block. Give them their separate lines.
 
Status
Not open for further replies.