gpt4 book ai didi

c++ - 菜单中的逻辑错误和读取功能以保存和读取文件

转载 作者:太空宇宙 更新时间:2023-11-04 12:29:18 25 4
gpt4 key购买 nike

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


}

}

}

最佳答案

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

#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();


}

}
}

关于c++ - 菜单中的逻辑错误和读取功能以保存和读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59293364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com