From the following code when i give input in string with space character like "John Doe" than it bypass/skip the block code(for loop). Please help me how i executed the for loop code if i entered the "John" only in cin input then it work fine and for loop code will be executed acordingly and when i use getline comand it didn't run the loop acordingly.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main ()
{
string nam;
int num;
fstream task2;
task2.open("task2.txt" , ios:: out);
if(!task2)
{
cout<<"File not found..."<<endl;
}
else
{
cout << "Enter the number of students: " ;
cin >> num ;
for (int i=0 ; i <num ; i++)
{
cout << "Student " <<(i+1)<< endl;
getline(cin,nam);
task2 <<nam <<endl;
}
cout<<"\nData read from the file."<<endl;
task2.close();
task2.open("task2.txt",ios::in);
string text;
for(int i=0; i<num; i++)
{
while(getline(task2,text))
{
cout<<text<<endl;
}
}
task2.close();
}
return 0;
}