Thread: problems with string finding a char in da middleinitial!!

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    3

    problems with string finding a char in da middleinitial!!

    hi everybody!!
    iam taking this class c++ in summer and is really giving me a lot of problems
    here is tha hw question
    write a program that reads a person's name int he folowing formaty:first name , then middle name or initial, and then last name. , ur program should work the same and place a period after the middle initial even if the input did not contais a period. your program shold allow for users who give no middle name. inthat case the output, of course, contains no middle name or initial.do one with string and the other one with c-strings.
    and this is what i have so far
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    
    int main()
    {
      
      // Variable declarations
      //
      string first,second,third,record_name;
       // strings read in
      int ch;                     // character in input after second string
      char middleInitial;
    
    
      //
      // Get the first two strings read in (first and middle names or
      // first and last names)
      cout << endl;
      cout << "Please enter a Full name : ";
      cin>> first >> second >> third;
     record_name = third +"," + first + " " + second[0] + ".";
     cout << record_name <<endl;
    
    char letter;
    cout <<"enter letter";
    cin >> letter;
    
      return 0;
    }
    program compiles ok when there is a name , last and initial but when there is no middle initial, doesn't , how can i get the second string to do this, sould i use the function substr or the function find with a if statement??
    really confused now.....hehe any input will be apreciated!!
    thanks
    macedonio

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    The program should compile regardless of input.
    Anyway, if a user enters:
    Code:
    David Goffredo
    and then pushes enter, the calls to cin.operator>>() will get the first two and then wait for the third. So, you need a way to figure out first how many words you're dealing with.

    So read it all in first.
    Last edited by CodeMonkey; 07-22-2007 at 02:27 AM. Reason: tags
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    3

    read all them first?

    i am not sure how to do that, sorry i feel like this should be easy to what it seems like,mmmm the only thing i can think is probably count the '\n'??? to see how many words are entered?? something like if there is just one '\n' treat it as a name and last and if there is two '\n' treat it as a name middleinitial and last??
    thanks

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    You can read an entire line of input using the getline function
    Code:
      string full_name;
      getline( cin, full_name );
    Once you've done that, the character you'll be looking for is a ' ' (a space character). you won't find '\n' (the new-line character) anywhere, because getline will filter that out for you.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    3
    hi that makes sense, so can i use the fucntions substr to find the two ' ' char front the beggining of the string to the end???

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can use find to find the spaces, and substr to separate the words. You can also use a stringstream, which works like cin. The difference would be that with a stringstream you can easily determine if the user typed in less than three names.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM