This forum is in archive mode. You will not be able to post new content.

Author Topic: [C++]String declaration from Chars variables!  (Read 2081 times)

0 Members and 1 Guest are viewing this topic.

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
[C++]String declaration from Chars variables!
« on: 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:

Code: [Select]
#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.
« Last Edit: April 13, 2011, 05:51:40 AM by gh0st »

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.