#include <iostream> #include <vector> #include <string> #include <algorithm> #in
ID: 3542955 • Letter: #
Question
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>
#include <fstream> //to read and write file
#include <cctype>
using namespace std;
int main()
{
ifstream inputFile;
string password;
string studentList;
int i, index;
vector<string> vectStudentList;
cout << "Please enter the password for the text file: ";
getline (cin, password);
inputFile.open(password.c_str());
if (inputFile.fail())
{
cout << " Error for Opening File. " << endl;
cout << "You have up to 03 times to enter the right password!" << endl;
for (i = 1; i < 4; i++)
{
cout << " Please enter the password for the text file again: ";
getline (cin, password); //validation 2- loop that allows user 3 times to enter password
}
exit (0);
}
while (inputFile >> (studentList))
{
vectStudentList.push_back (studentList);
}
cout << " The External Student List has the size of: " << vectStudentList.size() / 4 << endl << endl;
cout << left << setw (20) << "First Name" << setw (20) << "Last Name" << setw (20) << "ID" << setw (20) << "Enrolled Class" << endl << endl;
cout << "---------------------------------------------------------------------------" << endl;
for (int index = 0; index < vectStudentList.size(); index++)
{
cout << left << setw (20) << vectStudentList[index] << setw (20) << setw (20) << setw (20);
}
return 0;
Explanation / Answer
CHANGED CODE IS IN BOLD IF NOT UNDERSTOOD PLEASE MAKE A COMMENT :
PLEASE RATE - THANKS
DOUBTS - PLEASE ASK
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>
#include <fstream> //to read and write file
#include <cctype>
using namespace std;
int main()
{
ifstream inputFile;
string password;
string studentList;
int i, index;
vector<string> vectStudentList;
cout << "Please enter the password for the text file: ";
getline (cin, password);
inputFile.open(password.c_str( ));
if (inputFile.fail())
{
for (i =3 ; i >0; i--)
{
cout << " Error for Opening File. " << endl;
cout << "You have up to"<< i <<"times to enter the right password!" << endl;
cout << " Please enter the password for the text file again: ";
getline (cin, password); //validation 2- loop that allows user 3 times to enter password
inputFile.open(password.c_str( ));
if (inputFile.fail()&&i==1){
cout << " entered wrong password for the text file !! access denied : ";
exit (0);}
if(!inputFile.fail()) break;
}
cout << " entered correct password for the text file !! access granted : ";
}
string studentList;
while (getline(inputFile, studentList))
{
vectStudentList.push_back (studentList);
}
cout << " The External Student List has the size of: " << vectStudentList.size() / 4 << endl << endl;
cout << left << setw (20) << "First Name" << setw (20) << "Last Name" << setw (20) << "ID" << setw (20) << "Enrolled Class" << endl << endl;
cout << "----------------------------- ------------------------------ ----------------" << endl;
for (int index = 0; index < vectStudentList.size(); index++)
{
cout << left << setw (20) << vectStudentList[index] << setw (20) << setw (20) << setw (20);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.