Проблемы с сохранением файла - PullRequest
0 голосов
/ 21 июня 2020

Хорошо. Я пытаюсь завершить sh запись в файл, а затем чтение из файла, но я продолжаю получать сообщение об ошибке при запуске void readData (void). Ошибка: «Соответствующий токен не найден». Что я мог сделать, чтобы упростить эту работу?

Думаю, я не понимаю, как использовать функцию void. Я уверен, что есть еще проблемы, потому что, когда я удаляю void readData (void), ошибка переходит в void writeData (void).

//Specification: Append and display records in a address database 
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void menu(void);
void writeData(void);
void readData(void);
ifstream infile;
string cont = "Y";
string name;
string street;
string city;
string state;
string zip;
string choice;

const char FileName[] = "TestAddress.txt";
ofstream outfile("TestAddress.txt", ios::app);
int main() {
    menu();
    writeData();
    readData();
    return 0;
} //end main
void menu(void) {
    cout << "Would you like to add to the records, display them, or exit?\n";
    cout << "Enter  a  to add,  d  to display, or  e  to exit";
    cin >> choice;
    }
    //allow user to choose to append records, display records or exit the program
//end menu
void writeData(void) {
    while (choice != "a") {
        cout << "Enter the address name: ";
        getline(cin, name);
        cout << "Enter the house number and street: ";
        cin >> street;
        cout << "Enter the city: ";
        cin >> city;
        cout << "Enter the state: ";
        cin >> state;
        cout << "Enter the zip code: ";
        cin >> zip;
        outfile << name << "#" << street << city << "#" << state << "#" << zip << endl;
        cin.ignore();
        cout << "Do you want add another addressor exit? (a/e)";
        getline(cin, choice);
    }
    outfile.close();
    //Write the Address Info to a file
    //loop while user still has data to write to file
    //eg outStream<<name<<”#”; //where # is the delimiter

}
//end write data
void readData(void) {
    //read data from a file
    //use the split function to break a
    //deliminated line of text into fields
    infile.open("TestAddress.txt");
    ifstream inMyStream("TestAddress.txt");
    while (choice != "d") {

        if (inMyStream.is_open()) {

            //set character to use as a line between record displays    
            string recBreaks = "#";
            recBreaks.assign(20, '#');

            int fieldCount = 0;  //keep track of the number of fields read
            int recordCount = 1; //keep track of the number of records read

            //read the first field
            fieldCount = 1;
            string fieldBuffer;
            getline(inMyStream, fieldBuffer, '#');

            while (!inMyStream.eof()) {

                //display the field
                switch (fieldCount) {
                case 1:
                    cout << recBreaks << endl;
                    cout << "record # " << recordCount << endl;
                    cout << "Name...." << fieldBuffer << endl; break;
                case 2:
                    cout << "Street.." << fieldBuffer << endl; break;
                case 3:
                    cout << "City...." << fieldBuffer << endl; break;
                case 4:
                    cout << "State..." << fieldBuffer << endl; break;
                case 5:
                    cout << "Zip....." << fieldBuffer << endl;
                    fieldCount = 0;
                    recordCount++;  break;
                }

                //read the next field
                getline(inMyStream, fieldBuffer, '#');
                fieldCount++;
            }
            cout << recBreaks << endl;

            inMyStream.close();

        }//end read data
    }
...