Edit a character string with an assigned variable

This is a discussion on Edit a character string with an assigned variable within the C++ Programming forums, part of the General Programming Boards category; Hi I am a little new to programming and am a little frustrated. I am trying to figure out how ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    9

    Edit a character string with an assigned variable

    Hi I am a little new to programming and am a little frustrated. I am trying to figure out how to edit a character string that has been assigned a variable name. For example:

    line = Sam, I am.

    I think I have to use something like the code below, but I want to use the variable line or find a way to get the string into the function with quotes.

    string str("Sam, I am.");
    cout << str << endl;
    str.erase(remove_if(str.begin(),str.end(),ispunct) ,str.end());
    cout << str << endl;
    return 0;

    Is there a way to pass the string this way?

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I don't completely understand your problem. Do you mean something like this?
    Code:
    #include<string>
    #include<iostream>
    
    int main()
    {
    
        char line&#091;&#093;="Sam, I am.";
        std::string str(line);
        std::cout << str << std::endl;
        str.erase(remove_if(str.begin(),str.end(),ispunct) ,str.end());
        std::cout << str << std::endl;
        
        std::cin.get();
        return 0;
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    If you want to include quotes in a literal string you have to escape them.
    Code:
    std::string s = "\"Sam, I am.\"";
    std::cout << s << std::endl;
    this outputs "Sam, I am." with the quotes and everything. I am afraid I don't really understand the question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  2. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. character string as a variable?
    By Guideon72 in forum C Programming
    Replies: 4
    Last Post: 10-19-2001, 07:59 AM

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