C++ String Problem

This is a discussion on C++ String Problem within the C++ Programming forums, part of the General Programming Boards category; Part 1: Write a program that reads a person's name in the format: first name, then middle name or initial, ...

  1. #1
    Unregistered
    Guest

    C++ String Problem

    Part 1:
    Write a program that reads a person's name in the format: first name, then middle name or initial, and then last name. The program then outputs the name in the format:
    Last_Name, First_Name, Middle_Initial.

    For example, the input:
    Mary Average User

    should proguce the output:
    User, Mary A.

    The input
    Mary A. User

    should also produce the output:
    User, Mary A.

    Part 2:
    Your program should allow for users who give no middle name or middle initial. In that case, the output, of course, contains no middle name or initial.

    For example, the input:
    Mary User

    should produce the output:
    User, Mary

    I just know how to do Part 1... and what I wrote is:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    int main()
    {
    
      string a,b,c;
    
      cout<<"Please type in your first name, middle name or initial, and then last name:"<<endl;
      cin >> a >> b >> c;
      cout <<"Your name is:"<<endl<< c <<", " << a <<" " << b[0] <<"." << endl;
    return 0;
    }
    Can anyone help me for Part 2? thanks!!

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    8
    haven't learned cin and cout yet, but I know the gist of what it does.

    Try this. If c was not entered, assume b is c, and reformat the output accordingly.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    Originally posted by Pureghetto
    haven't learned cin and cout yet, but I know the gist of what it does.

    Try this. If c was not entered, assume b is c, and reformat the output accordingly.
    Ya... I know what u mean. However, I failed doing that... I have no ideas what should I do to assume b is c if c was not entered because it'll keep asking u to enter the third string before next step. anyway, thx for ur reply!!
    Last edited by DramaKing; 10-27-2001 at 12:03 AM.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    8
    obviously you need to remove that function (or lines). Just assume that if c does not exist you change b to c and then make b a blank.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    Originally posted by Pureghetto
    obviously you need to remove that function (or lines). Just assume that if c does not exist you change b to c and then make b a blank.
    Oh!! I'm new to C++, so don't know what to do now, but I'll try to do. Thx for ur comment!! If I still have programs with that, I wish that u don't mind me asking u again....

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    61
    What you should do is get the input as one big string using the function cin.getline();. Then split that bigger string into smaller strings and display the strings.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    Originally posted by ArseMan
    What you should do is get the input as one big string using the function cin.getline();. Then split that bigger string into smaller strings and display the strings.
    Thx!! However, I have no idea what cin.getline(); is... How can I put them into a big string and split it into some pieces?? Can u tell me how to do? thx!!~

  8. #8
    Unregistered
    Guest
    cin.getline(char * buffer, int maxCharToReadIn, char terminatingChar)

    terminating char defaults to newline which is probably what you want.

    count number of spaces in buffer
    if spaces is 3 then you have 3 parts to name
    if spaces is 2 then you have 2 parts to name
    declare an array of 3 strings and assign empty strings to them

    parse buffer by:
    option 1 : use strtok() with space as delimiter or similar function for STL strings
    option 2 : read buffer char by char. assign chars in buffer to first string. When space found place null terminator in first string and bring up second string. repeat until all char read in
    option 3 : wait for some one else to answer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM

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