String

This is a discussion on String within the C++ Programming forums, part of the General Programming Boards category; How can I capture the string in C++? For example, the input is: Mary Average User and I can capture ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    101

    String

    How can I capture the string in C++?
    For example, the input is: Mary Average User
    and I can capture the those three words into three variables?

    Thx!~

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Something like this would work -

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    int main()
    {
    
    	string a,b,c;
    
    	cin >> a >> b >> c;
    
    	cout << a << endl << b << endl << c << endl;
    	return 0;
    }
    zen

  3. #3
    Registered User Null Shinji's Avatar
    Join Date
    Oct 2001
    Posts
    80

    Re: String

    Originally posted by DramaKing
    How can I capture the string in C++?
    For example, the input is: Mary Average User
    and I can capture the those three words into three variables?

    Thx!~
    you wanna capture into different variables or into a single one???
    look for a single variable you could use:

    Code:
    char s[200];
    gets(s);
    
    //if youre reading from a file then use fgets
    what zen said is right...

    Mike
    Null Shinji The Sorcerer is here
    Evangelion Quotes:
    "If youre gonna do it, dont waste time. Otherwise, leave", Gendo
    "Release the final safety lock, Evangelion Unit One, Lift Off!!", Misato
    "Syncrograph has reversed, pulses are flowing back!!!", Maya
    streamload id= nullshinji icq= 12944337; E-M@IL= nullshinji@gmx.net; aim= mayeba
    msn= mayeba86@hotmail.com

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    Thx!! The program can put three words in three different variables, how about if I key in two words, the first word will put in varaible "a" and the second word will put in varible c?

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Use cin.getline() to get the input as a string then use strtok() or strchr() to split the string up into its constituent words.A search of these boards should throw some light on this.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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