Menu

DevC++ ifstream won't take in user input?

thormander
2022-05-11
2023-02-09
  • thormander

    thormander - 2022-05-11

    I am trying to allow the user to input the name of a .txt file so I can add the integers from it into an array.

        //file reading -----------------
    
        ifstream infile;
    
        string filename;
       cout << "Enter File name (no spaces): ";
       cin >> filename;
    
    
     infile.open(filename);   //<---------WHERE THE ERROR SHOWS
    int count = 0;
    while (!infile.eof()) {
    
        infile >> inputA[count];
        count++;
      }
    infile.close();
    //------------------------------
    

    It always throws this error :[Error] no matching function for call to 'std::basic_ifstream<char>::open(std::string&)'</char>

     

    Last edit: thormander 2022-05-11
  • Vinay Singh

    Vinay Singh - 2023-02-09
    ifstream infile;
    
    string filename;
    cout << "Enter File name (no spaces): ";
    cin >> filename;
    
    infile.open(filename.c_str());
    int count = 0;
    while (!infile.eof()) {
        infile >> inputA[count];
        count++;
    }
    infile.close();
    

    Check this code, If this is working for you or not?

     

Log in to post a comment.