首页 > 解决方案 > 菜单中的逻辑错误和读取功能以保存和读取文件

问题描述

该程序应该从菜单调用读写函数,请求输入,将这些输入保存到文件中,然后将数据输出到屏幕。菜单有效,但我只能写入文件并保存数据并退出。我无法读取或输出保存到文件的内容。对此的任何帮助将不胜感激。我应该有三个工作函数,main()、writeData() 和 readData()。

#include < iostream >
#include < fstream >
#include < string >
    using namespace std;

void menu(void);
void writeData(void);
void readData(void);
const char FileName[] = "TestAddress.txt";
string choice;

int main()
{
    menu();
    return 0;
}
void menu(void)
{
    while (choice != "Q") {
        cout << "What would you like to do?\nA. Read the existing data:\nB. Write new data to the list:\nQ.Quit\n";
        cin >> choice;
        if (choice == "A") {
            readData();
        }
        else if (choice == "B") {
            writeData();
        }
        else
            break;

    }
}

void writeData(void)
{
    ofstream outFile("testAddress.txt");
    string name;
    string street;
    string city;
    string state;
    int zip;
    string cont = "y";
    ifstream inFile;

    while (cont != "q") {
        cout << "Please enter the name:\n";
        getline(cin, name);
        cout << "Please enter the street:\n";
        getline(cin, street);
        cout << "Please enter the city:\n";
        getline(cin, city);
        cout << "Please enter the state:\n";
        getline(cin, state);
        cout << "Please enter the zip code:\n";
        cin >> zip;
        outFile << name << street << "#" << city << state << zip << endl;
        cin.ignore();
        cout << "Would you like to continue? Type q to quit:";
        getline(cin, cont);
        cin.ignore();
    }
    outFile.close();

    inFile.open("testAddress.txt");
    string fieldBuffer;
    if (inFile.is_open()) {
        while (inFile.eof()) {
            getline(inFile, fieldBuffer, ',');
            cout << fieldBuffer;
            cout << endl;

        }
    }
}
//use # sign for delimiter
void readData(void)
{
    ifstream inMyStream(FileName);
    if (inMyStream.is_open()) {
        string recBreaks = "";
        recBreaks.assign(20, '*');

        int fieldCount = 0;
        int recordCount = 1;

        fieldCount = 1;
        string fieldBuffer;
        getline(inMyStream, fieldBuffer, '#');

        while (inMyStream.eof()) {
            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...." << endl;
                    break;
                case 5:
                    cout << "Zip Code...." << endl;
                    fieldCount = 0;
                    recordCount++;
                    break;
                    getline(inMyStream, fieldBuffer, '#');
                    fieldCount++;
            }
            cout << recBreaks << endl;
            inMyStream.close();


        }

    }

}

标签: c++

解决方案


我已经修复了这段代码中的一些错误,感谢所有评论的人!现在一切正常,除了 readData 函数。当我将数据输入文件并运行 readData 函数时,我得到一个无限循环。是否有不合适的中断声明?

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void menu(void);
void writeData(void);
void readData(void);
const char FileName[] = "TestAddress.txt";
string choice;

int main()
{
    menu();
    return 0;
}
void menu(void)
{
    while (choice != "Q")
    {
        cout << "What would you like to do?\nA. Read the existing data:\nB. Write new data to the list:\nQ.Quit\n";
        cin >> choice;
        if (choice == "A")
        {
            readData();
        }
        else if (choice == "B")
        {
            writeData();
        }
        else
            break;

    }
}

void writeData(void)
{
    ofstream outFile("TestAddress.txt");
    string name;
    string street;
    string city;
    string state;
    int zip;
    string cont = "y";
    ifstream inFile;

    while (cont != "q")
    {       
        cout << "Please enter the name:\n";
        cin.ignore();
        getline(cin, name);
        cout << "Please enter the street:\n";
        getline(cin, street);
        cout << "Please enter the city:\n";
        getline(cin, city);
        cout << "Please enter the state:\n";
        getline(cin, state);
        cout << "Please enter the zip code:\n";
        cin >> zip;
        outFile << name<< "#" << street<< "#"<< city << "#" << state << "#" << zip << "#" << endl;
        cin.ignore();
        cout << "Would you like to continue? Type q to quit:";
        getline(cin, cont);
        cin.ignore();
    }
    outFile.close();

    inFile.open("TestAddress.txt");
    string fieldBuffer;
    if (inFile.is_open())
    {
        while (!inFile.eof())
        {
            getline(inFile, fieldBuffer, '#');
            cout << fieldBuffer;
            cout << endl;

        }
    }
}
//use # sign for delimiter
void readData(void)
{
    ifstream inMyStream(FileName);
    if (inMyStream.is_open())
    {
        string recBreaks = "";
        recBreaks.assign(20, '*');

        int fieldCount = 0;
        int recordCount = 1;

        fieldCount = 1;
        string fieldBuffer;
        getline(inMyStream, fieldBuffer, '#');

        while (!inMyStream.eof())
        {
            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 Code...." << fieldBuffer << endl; 
                fieldCount = 0;
                recordCount++;
                break;

                getline(inMyStream, fieldBuffer, '#');
                fieldCount++;       
            }
            cout << recBreaks << endl;
            inMyStream.close();


        }

    }
}

推荐阅读