0
我以前見過這個問題,但找不到幫助我解決問題的答案。這裏發生了一些事情。這是顯示菜單,這將允許用戶:退出返回上一級菜單,從文本文件中讀取數據到結構數組中
- 查看費用,
- 更新費用,或
- 。
該信息被保存到應該輸入和輸出到的數據文件中。嘗試查看文件中的數據時,第一個菜單選項有問題。假設已經存在包含該信息的數據文件C:\danceexpenses.txt
:
DJ
100
Site
100
Food
100
Favors
100
Security
100
Ticket printing
100
Etc.
100
etc.
100
該程序將不顯示該信息。它只給每個空行。有什麼我需要添加配置輸出?
void charges()
{
//Structures to hold data
struct Expenses
{
string name;
double cost;
};
int chargesChoice;
const int CHARGES_MENU_VIEW = 1, CHARGES_MENU_UPDATE = 2, CHARGES_MENU_QUIT = 3;
const int NUM_EXPENSES = 8; //number of expenses
Expenses danceExpenses[NUM_EXPENSES]; //array of structures
int index; //loop counter
cout << "\nWhat would you like to do?\n\n"
<< "1. View charges\n"
<< "2. Update charges\n"
<< "3. Return to main menu\n"
<< "Enter your choice: ";
cin >> chargesChoice;
while (chargesChoice < CHARGES_MENU_VIEW || chargesChoice > CHARGES_MENU_QUIT)
{
cout << "\nPlease enter a valid menu choice: ";
cin >> chargesChoice;
}
switch (chargesChoice)
{
case CHARGES_MENU_VIEW: //Display the expenses data
myFile.open("c:\\danceexpenses.txt", ios::in); //open file
if (!myFile)
{
cout << "Error opening file. Program aborting.\n";
return;
}
char output[100];
cout << "\nHere are the expected expenses for the dance:\n";
for (index = 0; index < NUM_EXPENSES; index++)
cout << danceExpenses[index].name << endl << endl;
myFile.close(); //close file
break;
case CHARGES_MENU_UPDATE:
//get expense data
myFile.open("c:\\danceexpenses.txt", ios::out); //open file
if (!myFile)
{
cout << "Error opening file. Program aborting.\n";
return;
}
for (index = 0; index < NUM_EXPENSES; index++)
{
cout << "Enter the name of the expense: ";
cin.ignore();
getline(cin, danceExpenses[index].name);
cout << "Enter the expected cost for this expense: ";
cin >> danceExpenses[index].cost;
}
//Write data to file
for (index = 0; index < NUM_EXPENSES; index++)
{
myFile << danceExpenses[index].name << endl;
myFile << danceExpenses[index].cost << endl;
}
myFile.close(); //close file
break;
case CHARGES_MENU_QUIT:
showMenu();
break;
}
charges();
}
你的代碼似乎有無限尾遞歸,這不會導致幸福,從長遠來看。 – 2013-04-20 19:13:09
嗯我沒有看到任何讀取訪問該文件。或者做一個失蹤的東西? – 2013-04-20 19:25:59