can some help me with c++ code it not working #include #include #include #includ
ID: 2246464 • Letter: C
Question
can some help me with c++ code it not working
#include #include #include #include using namespace std; void menu(void); void writeData(void); void readData(void); const char FileName[] = "TestAddress.txt"; int main() { menu(); return 0; } //end of main function void menu(void) { char userChoice = ' '; do { cout << "(A)ppend Records, (S)how Records, (E)xit" << endl; cin >> userChoice; switch (userChoice) { case 'A': case 'a': // For appending the records writeData(); break; case 'S': case 's': // reading the records from file readData(); break; case 'E': case 'e'://For Exiting the program cout << "Exiting the program Now!" << endl; break; default: cout << "choice is Invalid" << endl; break; }; } while (userChoice != 'e'); } void writeData(void) { string name = " "; string street = " "; string city = " "; string state = " "; string zip = " "; char user_response = 'Y'; do { cout << endl; getline(cin, name); cout << endl << "Name..........."; getline(cin, name); cout << endl << "Street........."; getline(cin, street); cout << endl << "City..........."; getline(cin, city); cout << endl << "State.........."; getline(cin, state); cout << endl << "Zip Code......."; getline(cin, zip); ofstream MyFile(FileName, ios::app); if (MyFile.is_open()) { MyFile << endl << name << "," << street << "," << city << "," << state << "," << zip << endl; MyFile.close(); cout << endl << "Enter another Record? (Y/N)"; cin >> user_response; } else { cout << "File Error: Open Failed"; } } while (user_response == 'Y' || user_response == 'y'); } void readData(void) { string name = " "; string street = " "; string city = " "; string state = " "; string zip = " "; int recordCount = 0; ifstream MySavedFile(FileName); if (!MySavedFile.good()) { cout << " Unable to Open the file " << FileName << endl; return; } do { getline(MySavedFile, name, ','); if (MySavedFile.eof()) break; getline(MySavedFile, street, ','); getline(MySavedFile, city, ','); getline(MySavedFile, state, ','); getline(MySavedFile, zip, ' '); recordCount++; cout << " Record No. " << recordCount; cout << endl; cout << "Name.........." << name << endl; cout << "street.........." << street << endl; cout << "City.........." << city << endl; cout << "State.........." << state << endl; cout << "Zip.........." << zip << endl; cout << "_______________________________________" << endl; } while (true); MySavedFile.close(); cout << endl; }
Explanation / Answer
There are minor issues with the code :
1.Missing header files
2.Corrected the unnecessary newline character in front of name in the display(show functionality)
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
void menu(void);
void writeData(void);
void readData(void);
const char FileName[] = "TestAddress.txt";
int main() {
menu();
return 0;
} //end of main function
void menu(void) {
char userChoice = ' ';
do {
cout << "(A)ppend Records, (S)how Records, (E)xit" << endl;
cin >> userChoice;
switch (userChoice) {
case 'A':
case 'a': // For appending the records
writeData();
break;
case 'S':
case 's': // reading the records from file
readData();
break;
case 'E':
case 'e'://For Exiting the program
cout << "Exiting the program Now!" << endl;
break;
default:
cout << "choice is Invalid" << endl;
break;
};
} while (userChoice != 'e');
}
void writeData(void) {
string name = " ";
string street = " ";
string city = " ";
string state = " ";
string zip = " ";
char user_response = 'Y';
do {
cout << endl;
getline(cin, name);
cout << endl << "Name...........";
getline(cin, name);
cout << endl << "Street.........";
getline(cin, street);
cout << endl << "City...........";
getline(cin, city);
cout << endl << "State..........";
getline(cin, state);
cout << endl << "Zip Code.......";
getline(cin, zip);
ofstream MyFile(FileName, ios::app);
if (MyFile.is_open()) {
MyFile << name << "," << street << "," << city << "," << state << "," << zip << endl;
MyFile.close();
cout << endl << "Enter another Record? (Y/N)";
cin >> user_response;
} else {
cout << "File Error: Open Failed";
}
} while (user_response == 'Y' || user_response == 'y');
}
void readData(void) {
string name = " ";
string street = " ";
string city = " ";
string state = " ";
string zip = " ";
int recordCount = 0;
ifstream MySavedFile(FileName);
if (!MySavedFile.good()) {
cout << " Unable to Open the file " << FileName << endl;
return;
}
do {
getline(MySavedFile, name, ',');
if (MySavedFile.eof())
break;
getline(MySavedFile, street, ',');
getline(MySavedFile, city, ',');
getline(MySavedFile, state, ',');
getline(MySavedFile, zip, ' ');
recordCount++;
cout << " Record No. " << recordCount;
cout << endl;
cout << "Name.........." << name << endl;
cout << "street.........." << street << endl;
cout << "City.........." << city << endl;
cout << "State.........." << state << endl;
cout << "Zip.........." << zip << endl;
cout << "_______________________________________" << endl;
} while (true);
MySavedFile.close();
cout << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.