C++ 保存和读取文件的菜单和读取函数中存在逻辑错误
该程序应该从菜单调用读写功能,请求输入,将这些输入保存到文件,然后将数据输出到屏幕。菜单工作,但我只能写入文件,保存数据并退出。我无法读取或输出保存到文件中的内容。在此方面的任何帮助都将不胜感激。我应该有三个工作函数:main()、writeData()和readData()C++ 保存和读取文件的菜单和读取函数中存在逻辑错误,c++,C++,该程序应该从菜单调用读写功能,请求输入,将这些输入保存到文件,然后将数据输出到屏幕。菜单工作,但我只能写入文件,保存数据并退出。我无法读取或输出保存到文件中的内容。在此方面的任何帮助都将不胜感激。我应该有三个工作函数:main()、writeData()和readData() #包括 #包括 #包括 使用名称空间std; 作废菜单(作废); 书面无效数据(无效); void readData(void); const char FileName[]=“TestAddress.txt”; 字符串选
#包括
#包括
#包括
使用名称空间std;
作废菜单(作废);
书面无效数据(无效);
void readData(void);
const char FileName[]=“TestAddress.txt”;
字符串选择;
int main()
{
菜单();
返回0;
}
作废菜单(作废)
{
while(选项!=“Q”){
选择;
如果(选项==“A”){
readData();
}
否则如果(选项==“B”){
writeData();
}
其他的
打破
}
}
无效写入数据(无效)
{
流输出文件(“testAddress.txt”);
字符串名;
弦街;;
字符串城市;
字符串状态;
int-zip;
字符串cont=“y”;
河流充填;
while(cont!=“q”){
cout我已经修复了这段代码中的一些错误,THANX给所有评论的人!现在除了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();
}
}
}
#包括
#包括
#包括
使用名称空间std;
作废菜单(作废);
书面无效数据(无效);
void readData(void);
const char FileName[]=“TestAddress.txt”;
字符串选择;
int main()
{
菜单();
返回0;
}
作废菜单(作废)
{
while(选项!=“Q”)
{
选择;
如果(选项==“A”)
{
readData();
}
否则如果(选项==“B”)
{
writeData();
}
其他的
打破
}
}
无效写入数据(无效)
{
流输出文件(“TestAddress.txt”);
字符串名;
弦街;;
字符串城市;
字符串状态;
int-zip;
字符串cont=“y”;
河流充填;
while(cont!=“q”)
{
cout我已经修复了这段代码中的一些错误,THANX给所有评论的人!现在除了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();
}
}
}
#包括
#包括
#包括
使用名称空间std;
作废菜单(作废);
书面无效数据(无效);
void readData(void);
const char FileName[]=“TestAddress.txt”;
字符串选择;
int main()
{
菜单();
返回0;
}
作废菜单(作废)
{
while(选项!=“Q”)
{
选择;
如果(选项==“A”)
{
readData();
}
否则如果(选项==“B”)
{
writeData();
}
其他的
打破
}
}
无效写入数据(无效)
{
流输出文件(“TestAddress.txt”);
字符串名;
弦街;;
字符串城市;
字符串状态;
int-zip;
字符串cont=“y”;
河流充填;
while(cont!=“q”)
{
无法读取文件的原因?存在void readData(void)
,有什么问题?“我无法读取或输出保存到文件中的内容。”-原因?编译错误?是否在代码中添加调试打印或在调试器中运行它?break;getline(…);fieldCount++;
部分看起来可疑。当程序达到中断;
时,您希望发生什么?程序编译并运行,调试没有标记任何内容。为什么无法读取文件?存在无效读取数据(void)
,有什么问题?“我无法读取或输出保存到文件中的内容。”-为什么?编译错误?您是在代码中添加调试打印还是在调试器中运行它?break;getline(…);fieldCount++;
部分看起来可疑。当程序到达break;
时,您希望发生什么?程序编译并运行,调试不会标记任何内容。