Q:

Why can't i give space as input in following code?

See the below code. If i give a "space" input for variable name, another integer variable is not being stored in the file. Why?
I've posted the outputs too.
    #include<iostream>
#include<fstream>
#include<cstring>
using namespace std;

int main()
{
    int age;
    char name[100];

    ofstream obj,obj10;
    obj.open("lol.dat", ios::trunc);
    cout<<"Enter Name"<<endl;

    cin.getline(name,100);
    cin.ignore();
    obj<<name<<endl;
    cout<<"Enter age"<<endl;

    cin>>age;
    cin.ignore();
    obj10<<age<<endl;
    obj.close();
    //read

    ifstream obj2;
    obj2.open("lol.dat");
    obj2>>name;
    cout<<"Name:"<<name<<endl;
    obj2>>age;
    cout<<"Age:"<<age<<endl;
    obj2.close();
    return 0;
}

My outputs:
 Enter Name
Ankith

Enter age
1234
Name:Ankith
Age:1234

Enter Name
Ankith Prabhas

Enter age
12345
Name:Ankith
Age:0

What do i need to do to take the complete name "Ankith Prabhas" as one input and another integer?

A:

Try to use getline() in strings where you expect spaces.

