I'm having a problem, I want to save all ID numbers in the text file, but it only saves the last ID number that user input.
#include <iostream> #include <fstream> #include <iomanip> #include <string> using namespace std; int main() { string line; int idnum; ofstream IdData ("data.txt"); cout<<" Enter your ID number: "<<endl; cin>>idnum; if(IdData.is_open()) { IdData<< "ID number of voter is: "; IdData << idnum << endl; IdData.close(); } else cout<<" Unable to open file"; ifstream Data ("data.txt"); if(Data.is_open()) { while (getline (Data, line)) { cout<< line << endl; } Data.close(); } else cout<<" Unable to open file"; }
IdDatafails to open, your program continues after printing the messsage. You may want to usereturn EXIT_FAILURE;after printing the message.