Thread: string madness

  1. #1
    irregularly symmetrical n3v's Avatar
    Join Date
    Mar 2006
    Location
    Finland
    Posts
    67

    string madness

    alright, i know this is an extraordinarily n00bish question, but as a noob, i'm having trouble figuring this out.
    so, imagine the input to a program is something like
    "a5 to b4"
    i need to take this from input, and split "a5" and "b4" into two seperate strings. i'm having trouble figuring out how to get it from input, how to seperate it, and how to put it into other strings.
    any help would be great. Thanks.

    by the way, i really don't get cin.getline(). i've only used it once, instead of cin >>, and the enter button had no effect. enlightenment on how cin.getline actually works would also be spectacular.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You may be able to use a stringstream with formatted input to a string, e.g.
    Code:
    std::stringstream ss("a5 to b4");
    std::string str;
    ss >> str; // read "a5"
    std::cout << str << "\n";
    ss >> str; // ignore "to"
    ss >> str; //read "b4"
    std::cout << str << "\n";
    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
    Feb 2006
    Posts
    155
    use the string library functions

    string a,b,c,d;
    a="a5 to b4"
    b= str.substr(0,2);
    c=str.substr(6,8);


    for adding to strings just use the overloaded + concatenation operator

  4. #4
    irregularly symmetrical n3v's Avatar
    Join Date
    Mar 2006
    Location
    Finland
    Posts
    67
    Quote Originally Posted by qqqqxxxx
    for adding to strings just use the overloaded + concatenation operator
    overloaded + concantenation operator? huh?

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    yeah suppose u have two strings

    string a ="aaaa";
    string b="bbbb";
    string c =a+b;
    cout<<c;

    u will get aaaabbbb

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    dont forget this:
    #include <string>

  7. #7
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    this is how I would do it

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string A, B;
    	cin >> A >> B >> B;
    	cout << A << " " << B;
    }

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by n3v
    by the way, i really don't get cin.getline(). i've only used it once, instead of cin >>, and the enter button had no effect. enlightenment on how cin.getline actually works would also be spectacular.
    I'm not clear what you mean by "the enter button had no effect". getline will read a line from cin, provided that cin is in a good state. (if the badbit or failbit are set, then you must reset cin before you can use it again)

    typical syntax for getline (assuming cin is good)
    Code:
    string str;
    getline(cin, str);
    Will read a whole line of input from cin, and store it in str.

    cin.getline() works slightly differently in that you specify a maximum number of characters to read from cin. Personally, I prefer the former method - only use cin.getline() when you explicitly need to specify string length.

    Note that "read from cin" is not the same as "prompt user for input". if cin already contains data from a previous operation, which has not yet been retrieved, then getline will retrieve that data.

    the only time the user is prompted for more input is when the stream is empty. (you can use cin.ignore() to discard any remaining unwanted characters in the stream)

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Note that all the examples and suggestions in the thread assume you are using C++ strings. However, cin.getline works on C style strings. You should switch to C++ strings and getline(cin, str). If you cannot switch, then you'll need a different solution.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    This works:
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int main()
    {
    	char str[100] = {0};
    	
    	cout<<"Please enter some text: ";
    	cin.getline(str, 100);
    
    	cout<<"You entered: "<<str<<endl;
    
    	
    	return 0;
    }
    This doesn't work:
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int main()
    {
    	char str1[50] = {0};
    	
    	cout<<"Please enter a word: ";
    	cin>>str1;
    	
    	cout<<"You entered: "<<str1<<endl;
    
    	
    	char str2[100] = {0};
    	cout<<"Please enter some text: ";
    	cin.getline(str2, 100);
    
    	cout<<"You entered: "<<str2<<endl;
    	
    	return 0;
    }
    Correcting the previous program to make it work:
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int main()
    {
    	char str1[50] = {0};
    	
    	cout<<"Please enter a word: ";
    	cin>>str1;
    	
    	cout<<"You entered: "<<str1<<endl;
    
    	
    	char str2[100] = {0};
    	cout<<"Please enter some text: ";
    
    	cin.ignore();
    	cin.getline(str2, 100);
    
    	cout<<"You entered: "<<str2<<endl;
    	
    	return 0;
    }
    Explanation:

    When you use a line like:

    cin>>str1;

    the program looks for some input to read from cin(cin represents input from the keyboard). Since cin starts off empty, there is no input to read, and the program must wait for the user to enter some input. After the user enters some input, say 'Bob', and hits return, the return causes a newline(\n) to be added to the input, so this is what's in cin:

    Bob\n

    The operation 'cin>>' is defined to stop reading input when it encounters whitespace(spaces, tabs, newlines), so 'Bob' is read from cin, and then cin>> stops reading input, leaving the trailing '\n' in cin. Any unread data in cin remains there and will be read during the next read operation.

    Now, cin.getline() works differently than 'cin>>'. cin.getline() reads in input until after it reads in a \n whereupon it immediately stops. In the 2nd program above, when you subsequently use cin.getline() to read from cin, getline() does not have to wait for more user input since there is already input in cin that it can read, namely the remaining '\n' leftover from the previous user input. So, cin.getline() reads the \n and stops, and execution continues with the next line in the program--making it seem like cin.getline() was skipped.

    Note that if the program used cin>> the second time it read from cin instead of cin.getline(), since cin>> is defined to skip all leading whitespace, cin>> would have skipped over the \n remaining in the input stream. Then, because cin would be unable to find any input it could read after the \n, cin>> would have to wait for the user to enter more input.

    So, the lesson is: when you use both cin>> and cin.getline() in a program, you have to be aware that the operation 'cin>>' will leave a trailing \n remaining in cin, which will mess up a subsequent cin.getline(). You can solve that problem by using cin.ignore() to erase any previous input remaining in cin.
    Last edited by 7stud; 03-17-2006 at 04:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. += operator
    By BKurosawa in forum C++ Programming
    Replies: 8
    Last Post: 08-05-2007, 03:58 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM