How can i fix these errors

This is a discussion on How can i fix these errors within the C++ Programming forums, part of the General Programming Boards category; Hi all I wrote this code to convert the word to pig latin and i got these errors Code: #include ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    10

    How can i fix these errors

    Hi all

    I wrote this code to convert the word to pig latin and i got these errors

    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    int main()
    {
    	char first[20] , last[20] , first1[20] , last1[20];
    
    	cout <<"Enter your name\n";
    	cin >> first >> last;
    	for(int i=0 ; i<strlen(first);i++)
    		first[i] = tolower(first[i]);
    	for ( i=0 ; i<strlen(last) ; i++)
    		last[i] = tolower(last[i]);
    	if(first[0]=='a' || first[0] == 'e' || first[0]=='i' || first[0] == 'o' || first[0]=='u')
    		strcat(first , "way");
    	else
    	{
    		int j=0;
    		for(i=1 ; i <= strlen(first) ; i++)
    		{
    			first [j] = first[i];
    			j++;
    		}
    		strcat (first1 , first[0]);
    		strcat (first1 , "ay");
    	}
    		if(last[0]=='a' || last[0] == 'e' || last[0]=='i' || last[0] == 'o' || last[0]=='u')
    			strcat(last , "way");
    		else
    		{
    					int j=0;
    		for(i=1 ; i <= strlen(last) ; i++)
    		{
    			last [j] = last[i];
    			j++;
    		}
    		strcat (last1 , last[0]);
    		strcat (last1 , "ay");
    	}
    
    	first[0]=toupper(first[0]);
    	return 0;
    }
    The errors here

    Code:
    First error :(error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast)
    second errors:: error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    
    same first error
    how can i fix it.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    The problem is that you are attempting to concatenate a single character to the end of your string with the strcat function, but that function only takes another (C-style) string, not a character.

    The best solution is to stop using C style strings in your C++ program. Use the C++ string class defined in <string>. Then you can use += instead of strcat to append strings or characters.

    If you have to use C style strings, then you can "append" the character by assigning the character to the position after the last valid character in the target string and manually setting the terminating null character to the next position.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Or just use the & operator. (address of)

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by robwhit View Post
    Or just use the & operator. (address of)
    To take the address of a single character? But strcat appends everything up to '\0'.

    strncat might help for that, though.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by anon View Post
    To take the address of a single character? But strcat appends everything up to '\0'.

    strncat might help for that, though.
    Where did the OP attempt to strcat a char that was not part of an array?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Where did the OP attempt to strcat a char that was not part of an array?

    At the lines with the errors:
    Code:
    strcat (first1 , first[0]);
    If you use the & option (which in this case would be the same as just using first), then the entire string would be appended. But that is not what is wanted. Only the first character of the first string should be appended.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Actually I wouldn't be sure at all what the OP wants to do. It seems that they are trying to append the whole array (first) to another array, which appears to be uninitialized. Basically that strcat should be strcpy.

    What they definately should do is turning the algorithm into a function, so they wouldn't have to reduplicate the code for both strings, and watch the array bounds.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I think there is a typo in there. In the loop inside the else it should be first1 [j] = first[i];. Better variable names might help avoid these problems.

    I'm pretty sure that only one character is trying to be appended there, because that's how pig latin works. The first character of the original string is added to the end of the translated string and then "ay" is added after that.

    Orfay exampleway, isway entencesay isway ittenwray inway igpay atinlay.

    And I want to reiterate my previous suggestion. This would be easier/safer/better with C++ strings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 11:50 AM
  2. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 05:54 PM
  3. strange errors?
    By egomaster69 in forum C Programming
    Replies: 6
    Last Post: 12-21-2004, 06:13 PM
  4. Help me with these errors... :-(
    By major_small in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2003, 09:18 PM
  5. Errors when including winsock2.h
    By skiingwiz in forum Windows Programming
    Replies: 2
    Last Post: 12-27-2002, 07:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21