Thread: How to use the string() function?

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    10

    How to use the string() function?

    I am working on an assignment that will return the string length in the number of characters. Here is what I've done so far, but I have no clue how to use the string() function. Any help would be appreciated.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	cout << "Groom's Name (last,first): ";
        string groomfirst;
        string groomlast;
        cin >> groomlast >> groomfirst;
        
        
        cout << "Bride's Name (last, first): ";
        string bridefirst;
        string bridelast;
        cin >> bridelast >> bridefirst;
    
    	size_t groom;
    	groom = groomlast.length();
    	size_t bride;
    	bride = bridelast.length();
    
    	cout << "Length of Groom's LAST name: " << groom  << endl;
    	cout << "Length of Bride's LAST name: " << bride << endl;
        
        return 0;
    }
    Last edited by almich; 01-27-2006 at 03:23 PM. Reason: Updated code

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    don't understand your question but
    Code:
    	string groom = groomlast.length();
    	string bride = bridelast.length();
    has to be
    Code:
           unsigned long groom = groomlast.length();
          unsigned long bride = bridelast.length();
    Kurt

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    try looking up the return type of the length function......I think you'll be able to figure out what you are doing wrong from there.

    http://www.msoe.edu/eecs/cese/resources/stl/

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    10
    Quote Originally Posted by ZuK
    don't understand your question but
    Code:
    	string groom = groomlast.length();
    	string bride = bridelast.length();
    has to be
    Code:
           unsigned long groom = groomlast.length();
          unsigned long bride = bridelast.length();
    Kurt
    Thanks. I got it to work!

    But I must move on to the next level where I have to store
    lastname,firstname into one variable and use the substr function to extract the last name and display the length of the last name. I do know it should be something like:

    name.substr(0,?)

    Does anybody know how to locate the comma, or is there an easier way?

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    again

    READ THE INFORMATION HERE!!!!!

    http://www.msoe.edu/eecs/cese/resources/stl/string.htm

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM