I'm working on this code which is asking user to input data (student data).
I made a constructor which is taking input of all fields on first run which goes fine on the first run. When I continue to go again via loop its skipping the Name input every time.
Code
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class person{
private:
string sName;
string fName;
string sAddress;
public:
person(){
cout << "Enter Details\n";
cout << "Name :";
getline(cin,sName);
cout << "Father's Name :";
getline(cin,fName);
cout << "Address :";
getline(cin,sAddress);
cout << sName << fName << sAddress;
ofstream myfiles("person.txt",ios::app);
myfiles<<sName<<endl;
myfiles<<fName<<endl;
myfiles<<sAddress<<endl;
myfiles.close();
checkLoop();
}
void checkLoop(){
int choice{};
cout << "\nDo you want to continue?\n1)Continue\tx)Anything else for exit!"<<endl;
cin >> choice;
switch(choice){
case 1:
person();
//break;
default:
break;
}
}
};
int main()
{
person p1;
return 0;
}
Output:
Do you want to continue?
1)Continue x)Anything else for exit!
1
Enter Details
Name :Father's Name :
When I choose the first option Continue
, it's skipping the "Name" part.