Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please check this simple C++ code, as the \'getfilename\' stuff didn\'t work pro

ID: 665405 • Letter: P

Question

Please check this simple C++ code, as the 'getfilename' stuff didn't work properly.

(no bugs detected,strange..)

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>

using namespace std;

void printlines()
{
int counter;

string inputBuffer;
string inputFileName;
ifstream inputFile;
{
cout << "Enter a filename to open: ";
getline(cin, inputFileName);

inputFile.open(inputFileName.c_str());;
if (inputFile.fail()) {
cerr << "File not found" << endl; // message to error stream
system("PAUSE");
exit (1); // end program, return a failure
}

do {
getline(inputFile, inputBuffer);
cout << inputBuffer << endl;
counter++;
if (counter % 20 == 0)
{
cout << "~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
} while (!inputFile.eof());
}
inputFile.close();
}

void addname()
{
ifstream outputFile;
string inputFileName;
string Data;

cout << "Enter a filename to open: ";
getline(cin, inputFileName);

outputFile.open(inputFileName.c_str(), ios::app);
if (outputFile.fail()) {
cerr << "File not found" << endl; // message to error stream
system("PAUSE");
exit (1); // end program, return a failure
}

cout << "Please enter the names you want to add into the file ";
getline( cin, Data);
cout << "You successfully added " << Data <<" to the end of file "<< inputFileName <<"" << endl;

outputFile >>Data;
outputFile.close();
}

void Quit()
{
system("cls");
cout<<" THANK YOU FOR USING THE FILE MANAGER ...... ";
exit(0);
}
char choice;

char Menu()
{
char choice;
cout<<" WELCOME TO FILE MANAGER ";
cout<<"=============================================";
cout<<" 1 Print names from the file ";
cout<<" 2 Add a name to the file ";
cout<<" q Quit ";
cout<<" Select Option: ";
cin>>choice;
return choice;
}

int main(int argc, char *argv[])
{

do{
choice = Menu();
switch(choice)
{
case '1' :printlines();
break;
case '2' :addname();
break;
case 'q' :Quit ();
break;
default:cout<<" Task Finished ";
}
}
while( choice != 'q');
return 0;

return choice;
}

Explanation / Answer

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
using namespace std;
void printlines()
{
int counter;
string inputBuffer;
string inputFileName;
ifstream inputFile;
{
cout << "Enter a filename to open: ";
getline(cin, inputFileName);
inputFile.open(inputFileName.c_str());;
if (inputFile.fail()) {
cerr << "File not found" << endl; // message to error stream
system("PAUSE");
exit (1); // end program, return a failure
}
do {
getline(inputFile, inputBuffer);
cout << inputBuffer << endl;
counter++;
if (counter % 20 == 0)
{
cout << "~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
}
} while (!inputFile.eof());
}
inputFile.close();
}
void addname()
{
ofstream outputFile;
string inputFileName;
string Data;
cout << "Enter a filename to open: ";
getline(cin, inputFileName);
outputFile.open(inputFileName.c_str(), ios::app);
if (outputFile.fail()) {
cerr << "File not found" << endl; // message to error stream
system("PAUSE");
exit (1); // end program, return a failure
}
cout << "Please enter the names you want to add into the file ";
getline( cin, Data);
cout << "You successfully added " << Data <<" to the end of file "<< inputFileName <<"" << endl;
outputFile >>Data;
outputFile.close();
}
void Quit()
{
system("cls");
cout<<" THANK YOU FOR USING THE FILE MANAGER ...... ";
exit(0);
}
char choice;
char Menu()
{
char choice;
cout<<" WELCOME TO FILE MANAGER ";
cout<<"=============================================";
cout<<" 1 Print names from the file ";
cout<<" 2 Add a name to the file ";
cout<<" q Quit ";
cout<<" Select Option: ";
cin>>choice;
return choice;
}
int main(int argc, char *argv[])
{
do{
choice = Menu();
switch(choice)
{
case '1' :printlines();
break;
case '2' :addname();
break;
case 'q' :Quit ();
break;
default:cout<<" Task Finished ";
}
}
while( choice != 'q');
return 0;
return choice;
}