C++ string problem

This is a discussion on C++ string problem within the C++ Programming forums, part of the General Programming Boards category; Hi I have a problem with this program. Code: #include <iostream> #include <string> using namespace std; void welcomeMessage(); string user_information ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    2

    C++ string problem

    Hi I have a problem with this program.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    void welcomeMessage();
    string user_information ( string name1, string name2);
    void exitMessage ( string name1, string name2);
    
    int main()
    {
    	string firstname, lastname;
    	welcomeMessage();
    	user_information ( firstname, lastname );
    	exitMessage ( firstname, lastname );
    	system("PAUSE");
    	return 0;
    }
    
    void welcomeMessage()
    {
    	cout << "Welcome to my C++ program \n";
    }
    
    string user_information ( string name1, string name2)
    {
    	
    	cout << "Please give me your first name ";
    	cin >> name1;
    	cout << "Please give me your last name ";
    	cin >> name2;
    	return name1 ;
    }
    
    void exitMessage ( string name1, string name2)
    {
    	cout << "Thank you ";
    	cout << name1;
    	cout << " ";
    	cout << name2 << endl;
    	cout << "for using my project " << endl;
    }
    I am not sure what to do. Do I need to convert them to a char array and then use one as input and the other as out put. Any input on this would be great I am stuck with this one.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    When you pass a variable to a function, you are actually passing a copy of that variable. Obviously, therefore, any changes to the copies won't be reflected in the originals.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,245
    user_information() needs to accept arguments that are non-const references (or pointers) to std::string. That allows you to change the string passed.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    39
    I would try the following- change...

    Code:
    string user_information ( string name1, string name2)
    {
    	
    	cout << "Please give me your first name ";
    	cin >> name1;
    	cout << "Please give me your last name ";
    	cin >> name2;
    	return name1 ;
    }
    To:

    Code:
    void userInformation(const string& s)
    {
       cout << "Please give me your first and last name";
      getline(cin, name);
      
     }
    Last edited by ninety3gd; 06-07-2009 at 10:57 AM. Reason: Added code tags

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    I would try the following- change...
    That's wrong in a couple ways. First of all, you pass a variable named "s", and use a variable named "name". Second, you can't pass a const reference to getline().

    The correct way to use that approach would be:
    Code:
    void userInformation(string& name)
    {
        cout << "Please give me your first and last name: ";
        getline(cin, name);
    }

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    39
    Got it bithub...I just what look right from the texts on the matter

    Forgot to find the parameter problem..and the caveat about getline...

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    2
    thanks now it works correctly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09: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, 02:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 01:11 AM

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