CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2004
    Posts
    236

    Is there a way to cast a char to a string?

    Hello everyone, I havn't been doing C++ in awhile but I need to freshen up fast!

    I'm doing the following,

    I'm trying to see if somthing in string2 is in string1,
    for example string2 is "eo"
    String1 = "Hello"
    Hello has
    both an e and an o
    the logic to see if "eo" in "Hello" works but once I find that yes there is an e and an o in "Hello" I want to remove the e and o, so the string remaining is
    hll

    Well the replace function only accepts STRINGS so I was trying to cast the character e when its found to a string, then using the string.repalce(i,1,char1);

    But I keep getting that error any ideas?


    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    
    
    int main()
    {
    
    	string string1 = "Hello";
    	string string2 = "eo";
    	cout << "string1: " << string1 << endl;
    	
    	for(int i = 0; string1[i] != '\0'; i++)
    	{
    		if(string1[i] == string2[0])
    		{
    			cout << "found: " << string2[0] << " in the string: " << string1 << endl;
    			//string1[i] = string1[i+1];
    			string char1 = string2[0];
    			string1.replace(i,1,char1);
    		}
    		else if(string1[i] == string2[1])
    		{
    			cout << "found: " << string2[1] << " in the string: " << string1 <<  endl;
    			//string1[i] = string1[i + 1];
    			string char2 = string2[1];
    			string1.replace(i,1,char2);
    		}
    		else
    			cout << "nothing in string2: " << string2 << "matches in string1: " << string1 << endl;
    	}
    	cout <<"New string: " << string1 << endl;
    
    	return 0;
    }
    The error is:
    1>.\test.cpp(22) : error C2440: 'initializing' : cannot convert from 'char' to 'std::basic_string<_Elem,_Traits,_Ax>'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>,
    1> _Ax=std::allocator<char>
    1> ]
    1> No constructor could take the source type, or constructor overload resolution was ambiguous
    1>.\test.cpp(29) : error C2440: 'initializing' : cannot convert from 'char' to 'std::basic_string<_Elem,_Traits,_Ax>'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>,
    1> _Ax=std::allocator<char>
    1> ]
    1> No constructor could take the source type, or constructor overload resolution was ambiguous
    1>Build log was saved at "file://c:\Documents and Settings\Mr. Coffee\My Documents\Visual Studio 2005\Projects\C program\C program\Debug\BuildLog.htm"
    1>C program - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Computer Science/Engineering
    @ PSU
    Co-oping with IBM's zSeries team! wee
    VS 2005.net
    http://www.personal.psu.edu/css204/

  2. #2
    Join Date
    Dec 2002
    Location
    La Plata, Buenos Aires
    Posts
    615

    Re: Is there a way to cast a char to a string?

    Strings can be constructed from chars. For example, here glGetString returns a char*, so ext is a std::string from a function that returns char*.

    Code:
    string ext ((const char*)glGetString(GL_EXTENSIONS));

  3. #3
    Join Date
    Dec 2002
    Location
    La Plata, Buenos Aires
    Posts
    615

    Re: Is there a way to cast a char to a string?

    Viceversa, c_str method of C++ strings can be used to convert a std::string to a C standard char* type.

  4. #4
    Join Date
    Sep 2004
    Posts
    236

    Re: Is there a way to cast a char to a string?

    It seems I don't need to do that at all but the problem I'm having now is how do I delete the last character of this string without an overflow?

    I'm telling it to delete the o off hello, and its mad because the '\0' is next, but I'm not telling it to delete the '\0' so I don't understand why its upset.

    I get a runtime error if I use this code:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    
    
    int main()
    {
    
    	string string1 = "Hello";
    	string string2 = "eo";
    	cout << "string1: " << string1 << endl;
    	
    	for(int i = 0; string1[i] != '\0'; i++)
    	{
    		if(string1[i] == string2[0])
    		{
    			cout << "found: " << string2[0] << " in the string: " << string1 << endl;
    			//string char1 = string(&string2[0],1);
    			string1.erase(i,1);
    		}
    		else if(string1[i] == string2[1])
    		{
    			cout << "found: " << string2[1] << " in the string: " << string1 <<  endl;
    			//string char2 = string(&string2[1],1);
    			string1.erase(i,1);
    		}
    		else
    			cout << "nothing in string2: " << string2 << "matches in string1: " << string1 << endl;
    	}
    	cout <<"New string: " << string1 << endl;
    
    	return 0;
    }
    If i comment out the last erase it will return
    Hllo
    but i want it to delete the o so its
    Hll


    Also this was what I used to convert a character to a string, but it wasn't needed.
    string char1 = string(&string2[0],1);
    Computer Science/Engineering
    @ PSU
    Co-oping with IBM's zSeries team! wee
    VS 2005.net
    http://www.personal.psu.edu/css204/

  5. #5
    Join Date
    Dec 2007
    Location
    Chennai (India)
    Posts
    25

    Exclamation Re: Is there a way to cast a char to a string?

    Why can't you use CString::Remove()

    Code:
    CString str("hello");
    int n = str.Remove('e');
    n = str.Remove('o');
    - Siva

  6. #6
    Join Date
    Sep 2004
    Posts
    236

    Re: Is there a way to cast a char to a string?

    when you say CString do you mean <string> or is there an actual library called CString?
    Computer Science/Engineering
    @ PSU
    Co-oping with IBM's zSeries team! wee
    VS 2005.net
    http://www.personal.psu.edu/css204/

  7. #7
    Join Date
    Dec 2007
    Location
    Chennai (India)
    Posts
    25

    Re: Is there a way to cast a char to a string?

    Cstring is different from string. string (std::string) is one of the standard template library, whereas CString is a COM pointer to the character string, i.e., the character string is wrapped in a component. For using CString, you should use MFC
    - Siva

  8. #8
    Join Date
    Dec 2007
    Location
    Chennai (India)
    Posts
    25

    Re: Is there a way to cast a char to a string?

    Hi,

    Try this:

    Code:
    string string1 = "Hello";
    string string2 = "eo";
    cout << "string1: " << string1 << endl;
    
    string::iterator iter = string1.begin();
    
    while (iter != string1.end() )
    {
    	char ch = iter[0];	
    		
    	if (ch == string2.at(0) || ch == string2.at(1))
    	{
    		string1.erase(iter);
    		continue;
    	}
    
    	iter++;
    }
    
    cout<<"New string : "<<string1<<endl;
    Last edited by SivakumarT; January 17th, 2008 at 01:12 AM.
    - Siva

  9. #9
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Is there a way to cast a char to a string?

    There's this thing called remove_if (among other useful things). Use it.

    Code:
    #include <algorithm>
    #include <string>
    
    struct Remove
    {
        Remove(const std::string& garbage) : garbage_(garbage)
        {
        }
    
        bool operator()(std::string::value_type symbol) const
        {
            return garbage_.find(symbol) != std::string::npos;
        }
    
        const std::string garbage_;
    };
    
    int main()
    {
        std::string string1 = "Hello";
    
        string1.erase(std::remove_if(string1.begin(), string1.end(), Remove("eo")), string1.end());    
    }
    Last edited by Plasmator; January 17th, 2008 at 01:21 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured