EvilZone

Programming and Scripting => C - C++ => : gh0st April 11, 2011, 12:50:30 AM

: [C++]String declaration from Chars variables!
: gh0st April 11, 2011, 12:50:30 AM
k guys here we have:

3. Write a program that asks the user to enter his or her first name and then last name, and
that then constructs, stores, and displays a third string, consisting of the user’s last name
followed by a comma, a space, and first name. Use char arrays and functions from the
cstring header file. A sample run could look like this:

Enter your first name: Flip
Enter your last name: Fleming

Here’s the information in a single strin: Fleming, Flip

we just need to know how to declare a string value from a char value so the answer would be like this:

:
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    char firstname[20];
    char lastname[20];
    cout << "Enter your first name: ";
    cin.getline(firstname,20);
    cout << endl;
    cout << "Enter your last name: ";
    cin.getline(lastname,20);
    cout << endl;
    string str1,str2;
    str1 = firstname;
    str2 = lastname;
    cout << firstname << "," << lastname;
    cout << endl;
    system("pause");
    return 0;
}

I did it fast cause it wasnt too hard.