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

标签 c++ loops menu categories

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

这是银行对帐单:

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/

相关文章:

javascript - 循环对象属性与数字索引数组

python - for 循环不会迭代函数中的字符串,但不会迭代函数 - python 3

ios - 类似下拉菜单的 Gmail iOS 应用

c++ - 使用折叠表达式打印每个元素之间的空格

c++ - C++继承与虚函数问题

C++ : Automatically run function when derived class is constructed

java - 修复 Java 中的转换错误

c++ - cpp中如何防止多个线程同时使用单例类实例

json - SwiftyJSON 循环与 swift 1.2

html - Asp.net 菜单的最后一个子菜单显示在右侧