Thread: cstring

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    8

    cstring

    i have a string that has various sized spaces between the words, (e.g the big brown fox jumps over the fence.)
    i am trying to assign each of the words to an array and then rejoin the array of words without the spaces. is this easy?
    i have also been advised to use strcmp. but i am not sure how this would work.

    help would be great.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Pretty easy using string streams. You would not even need an array as you can join the words back by concatenating to the result string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    is this easy?
    That's up to you to decide:
    Code:
    const char* cstr = "hello    world goodbye  world";
    
    string str(cstr);  //cstring-->c++ string
    istringstream input(str); //creates a container called 'input', which 
    	                   //you can read from like a file
    
    string result="";
    string current = "";
    
    while(input>>current)  //read from input as if it were a file
    {                       //the >> operator skips leading whitespace until it finds a character, 
                             //then it stops reading as soon as it encounters trailing whitespace
    
    	result += current;
    }
    
    cout<<result<<endl;  //helloworldgoodbyeworld
    Last edited by 7stud; 01-23-2007 at 06:14 AM.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    ok. that makes sense. here is my code so far. i am missing something pretty basic.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using std::cout;
    using std::endl;
    using std::cerr;
    using std::string;
    using std::getline;
    
    int main ()
    {
      string myStringErrors;
    
      std::ifstream file("Lab1.txt");
      
      if (!file)
        {
        cerr<<"Can't open file"<<endl;
        return EXIT_FAILURE;
        }
     //read file one line at a time
      while (std::getline(file,myStringErrors))
        {
        //do stuff with line
        cout<<myStringErrors<<endl;
    
    	const char* myStringNoSpaces = myStringErrors;
    
    	string str(myStringNoSpaces);		//cstring-->c++ string
    	istringstream input(str);			//creates a container called 'input', which
    										//you can read from like a file
    	string result="";
    	string current = "";
    
    	while(input>>current)				//read from input as if it were a file
    	{									//the >> operator skips leading whitespace
    	result += current;
    	}
    
    	cout<<result<<endl;
    	}
       cout<<endl;
    }
    when i compile i always get an error message saying : cannot convert from 'std::string' to 'const char *'. how do i change my std::string? can i change my std::string to suit?

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Whenever you ask a question about an error, ALWAYS post a message on the line that has the error, like this:
    Code:
    int n = 10.5;  //***ERROR***
    Or, post the error message along with the line of code:
    error message saying : cannot convert from 'std::string' to 'const char *'.
    which is this line of code:

    int n = 10.5;
    You can't assign a string type to a variable that is of type const char*. This code makes no sense:
    Code:
    string myStringErrors;
    ...
    const char* myStringNoSpaces = myStringErrors;
    string str(myStringNoSpaces)
    You try to convert myStringErrors from a string type to a const char*. Then you convert the result back to a string type. Hello? You had a string type to start with.
    Last edited by 7stud; 01-23-2007 at 06:39 AM.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    as i am sure you can tell i am new to programming and it is definately not my strong point. i was told that to eliminate spaces i should use 'strcmp' (not sure how that is going to help) or to assign my string to an array. the string 'myStringErrors' is from a file that was given to me. this i managed to open after hours of trying. but once opend i was told to assign each individual word to an array and then rejoin the array without the spaces. i have been sitting for hours doing this. no luck. also thought that maybe 'strcpy' with a loop using string::replace (' ', ' ') will any of these work or am i wasting my time?

    thank you for the tips on error message posting. much appreciated

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Well suppose your code did this:

    int num = 10;
    int irrelevant = num;
    num = irrelevant;

    What would you do to make that code more efficient?

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    not sure. sorry. do you need both variables? if not: int num = 10;.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Ok, now apply that same logic to this code:
    Code:
    string myStringErrors;
    ...
    std::getline(file,myStringErrors)
    ...
    const char* myStringNoSpaces = myStringErrors;
    string str(myStringNoSpaces);
    istringstream input(str);

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    sorry. get what you mean now. will have a look. this is frustrationg me now. need a break i think.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Writing CObjects with Serialization to CArchive
    By ruben_gerad in forum C++ Programming
    Replies: 0
    Last Post: 11-09-2006, 08:25 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Help with CString class and operator+ overloading
    By skanxalot in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2003, 10:23 AM
  5. Operater overloading - (const char + CString)
    By auth in forum C++ Programming
    Replies: 14
    Last Post: 10-24-2002, 09:22 AM