gpt4 book ai didi

c++ - 循环菜单重复自己

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

在选择了错误的选项后,我无法让菜单自行重复。我的指示是从外部文件读取并在菜单中使用它。我需要阅读银行对帐单,将它们分类,然后将它们相加。我不太确定如何将银行对帐单相加,而且一旦您选择了数字,我也无法让它读出任何内容。

这是银行对帐单:

20-Sep-13   c   Restaurant  MCDONALD'S M429 MURFREESBORO TN     $5.03
20-Sep-13 c Groceries KROGER #564 MURFREESBORO TN $5.00
18-Sep-13 c Restaurant THE OLIVE GARD0 MURFREESBORO TN $42.98
17-Sep-13 c Mortgage Bank of America MORTGAGE $1049.00
17-Sep-13 c Groceries KROGER MURFREESBORO TN $37.60
17-Sep-13 c Restaurant SUBWAY 0 MURFREESBORO TN $24.04
17-Sep-13 c Groceries KROGER #564 MURFREESBORO TN $5.00
16-Sep-13 c Groceries DOLLAR-GENERAL WOODBURY TN $98.31
16-Sep-13 c Gas RUTHERFORD COOP WOODBURY TN $59.80
16-Sep-13 c Restaurant D AND J PIZZA A WOODBURY TN $12.09
13-Sep-13 c Restaurant PANERA BREAD #9 MURFREESBORO TN $30.40
13-Sep-13 c Groceries KROGER #564 MURFREESBORO TN $3.88
9-Sep-13 c Groceries KROGER MURFREESBORO TN $58.62
6-Sep-13 c Entertainment DIRECTV ONLINE PMT *** $150.00
6-Sep-13 c Gas Thornton # 605 MURFREESBORO TN $58.00
4-Sep-13 c Entertainment REDBOX MURFREESBORO TN $1.27
4-Sep-13 c Groceries WAL-MART #5057 MURFREESBORO TN $27.36
4-Sep-13 c Restaurant SUBWAY 0 WOODBURY TN $7.88
3-Sep-13 c Groceries KROGER MURFREESBORO TN $75.04
3-Sep-13 c Groceries WAL Wal-Mart Su MURFREESBORO TN $31.43
3-Sep-13 c Restaurant SUBWAY 0 WOODBURY TN $30.72
3-Sep-13 c Groceries DOLLAR-GENERAL WOODBURY TN $5.38
3-Sep-13 c Groceries KROGER MURFREESBORO TN $4.72
29-Aug-13 c Gas RUTHERFORD COOP WOODBURY TN $15.05
29-Aug-13 c Restaurant HARDEE'S 150184 WOODBURY TN $5.21
27-Aug-13 c Gas THORTONS #612 MURFREESBORO TN $53.00
27-Aug-13 c Groceries KROGER MURFREESBORO TN $44.37
26-Aug-13 c Groceries KROGER MURFREESBORO TN $34.24
26-Aug-13 c Restaurant CRACKER BARREL SMYRNA TN $12.84
26-Aug-13 c Groceries KROGER MURFREESBORO TN $10.00

到目前为止,这是我的代码:

//Emily Kounlavong
//CSC 1170-003 BUCHER
//OLA7
//November 11
/*This program will create a menu for the user and display the amount of money spent in each category
depending on the number the user picks. It will display the menu over and over until the user quits.*/

#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <iomanip>

using namespace std;
int main ()

{
//DECLARE VARIABLES
float categoryRestaurants;
float categoryGroceries;
float categoryMortage;
float categoryGas;
float categoryEntertainment;
string spent;
string amountOne;
float amountTwo;
float amountThree;
float amountFour;
float amountFive;
int choice = 0;
string category;
string categoryQuit;
string fileName;
ifstream inData;
ofstream outData;

//OPEN THE FILE
inData.open("ola6FinancialData.txt");

//INTERACT
cout << "HELLO THERE :D" << endl;
cout << fixed << setprecision(2) << "Enter the menu number for the category you are interested in" << endl;
cout << "And the program will show you how much was spent in that category." << endl;

//MENU OPTIONS
do
{
cout << "**********************************************" << endl;
cout << "1. Restaurants" << endl;
cout << "2. Groceries" << endl;
cout << "3. Mortage" << endl;
cout << "4. Gas" << endl;
cout << "5. Entertainment" << endl;
cout << "6. Quit the program" << endl;
cout << "**********************************************" << endl;
cin >> choice;
}
while ((choice > 0) && (choice < 6));

//FILE STUFF
switch (choice)
{


case 1:
cout << "The total spent for Restaurants is " << endl;
break;

case 2:
cout << "The total spent for Groceries is " << endl;
break;

case 3:
cout << "The total spent for Mortage is " << endl;
break;

case 4:
cout << "The total spent for Gas is " << endl;
break;

case 5:
cout << "The total spent for Entertainment is " << endl;
break;

case 6:
cout << "Thanks for using my program. Have a great day! Goodbye." << endl;
break;

default:
cout << "That was not a correct choice. Please try again." << endl;

}

return 0;
}

最佳答案

你似乎把 do-while 搞混了。

while ((choice > 0) && (choice < 6));

只要 choice 大于零且小于六,这就会循环。首先,第二个条件是错误的(根据您的用户界面,六个应该是可以接受的选项)。即使那样,这也可能与您想要的相反——我相信您希望选项菜单循环显示不正确的选择,而不是正确的选择!所以试试这个:

while ((choice < 0) || (choice > 6));

关于c++ - 循环菜单重复自己,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19883908/

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