Search code examples
c++c++11vectorsegmentation-faultfstream

Comparison of two vectors leads to an exception


Comparing the two vectors in the if statement throws an exception (segmentation fault).
I was having an attempt to create a system, which, user details are being saved in a file and being read to give security questions to the user.

    #include <iostream>
    #include <string>
    #include <vector>
    #include <fstream>

        int main(int argc, char** argv) {    
                std::vector<std::string>setup_file_contents_vec{};
                std::ifstream setup_file_required_scan( "exposcan.txt" );    
                std::string buffer;
                while(setup_file_required_scan >> buffer) { setup_file_contents_vec.push_back(buffer); }
                std::vector <std::string> user_details_confirmation(3);
                std::cout << " Login details 1/3\n\n";
                std::cout << "First Name : ";
                std::cin >> user_details_confirmation[0];
                if(user_details_confirmation[0] != setup_file_contents_vec[0]) {/* code */} /*Exception occurs*/
    }

Solution

  • Thanks to @IgorTandetnik the issue was that the file was empty.