Thread: Trouble trying to input only certain fragments of a sentence

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    5

    Trouble trying to input only certain fragments of a sentence

    say i'm reading from a text file and it says:
    "Michelle Johnson , Dennis Doe , Clifford Robinson"

    let's say i got 3 strings and i want to store each name into a string, for ex, string 1 should have "Michelle Johnson", string2 should have "Dennis Doe", etc. Each name is seperated with a space and before and after the comma, can someone explain how i can do this?

    because if i use "getline", it'll jus read the whole sentence- any help would be great thanks

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The string extraction operator (>>) for ifstream will parse on space, newline, and tab characters. You then just need to get rid of any unwanted comma characters from your strings.
    Here's a simple example using cin:
    Code:
    string a, b, c;
    cin >> a >> b >> c;
    cout << a << endl << b << endl << c << endl;
    gg

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    5
    maybe i used a bad example because i'm actually using this for multiple lines in the text file, and some of the things in the text might be just one word or two words (it's random), for example if the text contained:

    "Brooke, Pamela , Carmen Elektra"
    and the next line contained
    "Michelle Johnson , Dennis Doe , Clifford Robinson"

    so if i used

    ifstream infile;
    infile >> a >> b >> c;

    it wouldn't work because i want to be able to get the name (whether it's just one name or first and last) and store it into a string, and do the same for the rest...so doing something like:

    infile >> firstname1 >> lastname 2 >> comma >> firstname2 >> lastname 2, etc..

    wouldn't work =[ any help would help me a lot

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    You've got figure this out. It isn't too hard to do it taking Codeplug's example and adjusting it for your purposes. I assume this is a homework assignment, and part of the assignment is to figure out how to use the ifstream to read in the data.

    Here is a stronger hint: Because your file has each name separated by a space, then a comma, then a space, when you read in the file one word at a time using ">>", you will either get part of a name or a comma. You need to figure out how to know when you reach a comma and what to do when it happens.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Brooke, Pamela, Carmen Elektra
    Michelle Johnson, Dennis Doe, Clifford Robinson

    Code:
    #include<cstring>
    
    ...
    
    char name1[32];
    char name2[32];
    int len;
    
    ...
    
    //in a loop
    ifstream.getline(name1,16,' ');
    
    len=strlen(name1);
    
    if(name1[len]=',')  //if this was the only name
      name1[len-1]='\0';  //get rid of that last char (and comply with c-string definition)
    else  //if there's another word comming
    {
       ifstream.getline(name2,32,' '); // take it in
       name2[strlen(name2)-1]='\0'; //remove the comma and put null-termiator in
    }
    
    //now you have the name
    
    ...
    
    //end loop
    
    ...
    this is assuming your file follows this:

    [fname],
    -or-
    [fname] [lname],

    with a space between each name and after each comma... no spaces before the comma... if your file doesn't follow these rules, this won't work... but you should definately get your input to follow some kinda ruleset...

    __________________________________________________ __
    EDIT: just read your OP again... just follow what codeplug said...mod it to do what you need

    Code:
    infile>>name1>>name2;
    
    cout<<name1;
    if(strcmp(name2,",\0")) //if name2 is not a comma
         cout<<name2;
    cout<<endl;
    of course this assumes you're using C-strings... which nobody uses anymore
    Last edited by major_small; 04-01-2004 at 09:11 PM.
    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

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You can use the extrator operator and process the file one line at a time if you want.
    Take each line that you read in and put it in an istringstream.

    gg

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by major_small
    of course this assumes you're using C-strings... which nobody uses anymore
    Hmm, so most of the experts on this board are nobodys. You've just made a lot of friends....
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  2. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM
  3. Trouble storing file input in array
    By difficult.name in forum C Programming
    Replies: 1
    Last Post: 10-10-2004, 11:54 PM
  4. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM
  5. Trouble with receiving input
    By BellosX in forum C Programming
    Replies: 4
    Last Post: 09-20-2001, 11:58 PM