#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
int main(){
ifstream f;
dc:
string loc;
cout<<"Enter file location: ";
getline(cin, loc);
ta:
try{
f.open(loc, ios::in);
if(!f.is_open())
throw runtime_error("File not found×");
else{
cout<<"File found✓"<<endl;
system("pause");
}
}catch(runtime_error e){
cout<<e.what()<<endl;
cout<<"Press 1 to try again\n 2 to change directory\nanyother key to cancel: ";
int x;
cin>>x;
if(x==1)
goto ta;
if(x==2)
goto dc;
}
cout<<"Thank you for trying.."<<endl;
f.close();
}
When i go to label dc
, it is not getting line. Output is given below:
Enter file location: /storage/emulated/0/sample.txt
File not found×
Press 1 to try again
2 to change directory
anyother key to cancel: 2
Enter file location: File not found×
Press 1 to try again
2 to change directory
anyother key to cancel:
It have to get the filename from getline(cin, loc);
again. Please give me solution to solve this issue
I tried cin.ignore();
just before goto dc;
statement. It works fine. But why?
Please explain this