Fras Irfan
Fras Irfan

Reputation: 37

Why my program isn't accepting the second input?

I don't know why it doesn't take the second input. Help me solve it.

This is the code:

#include <iostream>

using namespace std;


int main()
{
    char fn,ln;
    cout<<"Enter your First Name\n"<<endl;
    cin>>fn;
    cout<<"Enter your Last Name"<<endl;
    cin>>ln;

    return 0;
}

Upvotes: 0

Views: 638

Answers (1)

Taimoor Zaeem
Taimoor Zaeem

Reputation: 312

Since char can only hold a single character, you may use std::string for storing names.

Example:

#include <iostream>
#include <string>


int main( )
{
    std::cout << "Enter your first name\n";
    std::string firstName;
    std::getline( std::cin, firstName );

    std::cout << "Enter your last name\n";
    std::string lastName;
    std::getline( std::cin, lastName );

    std::cout << "\nHi " << firstName << ' ' << lastName << '\n';
}

Sample input/output:

Enter your first name
John
Enter your last name
Connor

Hi John Connor

Upvotes: 1

Related Questions