Read character array as string using cin in C++

Here, we are implementing a program in which we are reading a character array using cin in C++.
Submitted by IncludeHelp, on November 02, 2018 [Last updated : March 01, 2023]

How to read character array as string using cin?

We have to character array known as string in C++ using "cin".

In this example, we reading firstName and secondName of a person and printing them.

C++ program to read character array as string using cin

#include <iostream>
using namespace std;

int main()
{
    char firstName[30], secondName[30];

    //input values
    cout << "What is your first name? ";
    cin >> firstName;
    cout << "What is your second name? ";
    cin >> secondName;

    //printing
    cout << "Hi " << firstName << " " << secondName << endl;

    return 0;
}

Output

What is your first name? Amit
What is your second name? Shukla
Hi Amit Shukla





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.