0
#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

Raj Arv
  • 1
  • 3
  • 1
    You can eliminate the `goto`s by hoisting this code into a function. In the function goes a `while` loop that repeats forever. When the file successfully opens, it returns. If it does not ask for a retry and loop around. – user4581301 Nov 27 '20 at 19:17
  • 1
    Do not use try/catch as a replacement for an `if`, especially if you already have the `if`. Instead of throwing the exception and catching it later, move the code from the catch block into the body of the `if` that throws the exception. – user4581301 Nov 27 '20 at 19:19
  • @user4581301, can you explain why you are advising to use if instead of throw and catch. Then, when to use throw and catch And please explain about that `while` instead if goto. It's better if you give me the codes instead.. – Raj Arv Nov 28 '20 at 04:06
  • 1
    Exceptions are for exceptional events, stuff that you really aren't expecting to happen and don't want to clutter the code with cases handling it. Here you've already detected it and you might as well handle it immediately instead of adding more clutter and incurring the extra overhead of throwing and catching an exception. – user4581301 Nov 28 '20 at 04:49
  • 1
    Without fixing anything else, this is what I mean about simplifying by removing the exception: https://ideone.com/f27L78 – user4581301 Nov 28 '20 at 04:54
  • 1
    And now that I'm done with this week's episode of The Mandalorian, here's how to get rid of the `goto`s, again without fixing anything else: https://ideone.com/BcwWiC – user4581301 Nov 28 '20 at 05:57
  • 1
    Why you want to avoid `goto`: it's hard to get right, and when you get it right it's even harder to convince other people working on or around your code that you did get it right. It's usually easier to write your way around a `goto` than it is to defend it through code reviews and the unending barrage of "Never use `goto`!". – user4581301 Nov 28 '20 at 06:03

0 Answers0