Thread: Question on string concatenate (strcat)...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    22

    Question on string concatenate (strcat)...

    I have a program with two strings. I want to copy part of the first string, up to the first space character in that string, into a separate string. Here is the section of code i wrote that is supposed to do that. Rawstring & numberstr are character arrays (strings), rawstring is the source and numberstr is the destination, space is an integer which stores the position of the first space character in the array. It should be fairly obvious how it is supposed to work. When i try to compile i get an "invalid conversion from char to const char" in the strcat function. What is wrong? How do i fix it?

    Code:
    for (int x = 0; (x < 19) && (space = 0); x++)
        {
            if (rawstring[x] == ' ')
            {
                space = x;
            }
        }
        for (int x = 0; x < space; x++)
        {
            strcat(numberstr, rawstring[x]);
        }
        cout << "\n" << numberstr << "\n";

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Well, you say strings but you are using arrays and not strings?

    Here it is for strings:
    Code:
    #include<iostream>
    #include <string>
    
    using namespace std;
    
    
    int main()
    {
    
    
    	string original_string = "Hello 123456";
    	string up_to_space;
    
    
    	int location;
    
    	location = original_string.find(' ',0);
    	up_to_space = original_string.substr(0,location);
    
    	cout << up_to_space;
    
    	system("pause");
    
    
    	return 0;
    
    }
    But for the cstring...or arrays.
    Code:
    final_string[index] = original_String[index];
    In your last for loop upto the appropiate value of where index would be where the space occurs.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    22

    reply

    Wow i didn't know you could declare a string as, well, a string. I thought strings had to be declared as character arrays (ie char string[50]). Is there a different include file (other than iostream) needed to use the string variable type or the cool functions like string.find(' ', 0)?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    When i try to compile i get an "invalid conversion from char to const char" in the strcat function. What is wrong? How do i fix it?
    Actually, it should say: "invalid conversion from char to const char*". I don't know if you've studied pointers yet, but strcat() requires that the second argument be a const char*("constant char pointer"), which can be something between double quotes, e.g. "this text". Instead, you are using rawstring[x] which is a char type which is a single character between single quotes. In addition, strcat() requires that the first character array be big enough to hold all the characters in the second character array or string.

    The easiest way to add a character to the end of a string in a character array is like this:
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
    	char ch = 'o';
    	char my_string[10] = "hell";
    
    	my_string[strlen(my_string)] = ch;
    	cout<<my_string<<endl; 
    
    	return 0;	
    }
    Once again, the character array has to have room for the char you are adding. Note: when you are treating char arrays as strings, you don't want to add a char to the last position in the array. Therefore, if your char array has a size of 10, you don't want to be adding a char to index position 9. The last spot has to be a '\0' character when you are treating char arrays as strings.

    I thought strings had to be declared as character arrays (ie char string[50]).
    In C, they do. C++ has its own special string type.

    Is there a different include file (other than iostream) needed to use the string variable type or the cool functions like string.find(' ', 0)?
    |
    |
    |
    V
    Quote Originally Posted by Enahs
    Well, you say strings but you are using arrays and not strings?

    Here it is for strings:
    Code:
    #include<iostream>
    #include <string>
    
    using namespace std;
    
    
    int main()
    {
    
    
    	string original_string = "Hello 123456";
    	string up_to_space;
    
    
    	int location;
    
    	location = original_string.find(' ',0);
    	up_to_space = original_string.substr(0,location);
    
    	cout << up_to_space;
    
    	system("pause");
    
    
    	return 0;
    
    }
    But for the cstring...or arrays.
    Code:
    final_string[index] = original_String[index];
    In your last for loop upto the appropiate value of where index would be where the space occurs.
    Last edited by 7stud; 11-01-2005 at 01:25 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Strcat question
    By Berix in forum C++ Programming
    Replies: 2
    Last Post: 05-05-2005, 01:00 PM
  4. How to concatenate, then widen string literals
    By lonehacker in forum C Programming
    Replies: 13
    Last Post: 09-15-2004, 10:56 AM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM