循环菜单重复

时间:2013-11-09 22:31:19

标签: 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;
}

2 个答案:

答案 0 :(得分:3)

您似乎已将do-while的情况混淆了。

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

只要choice大于零且小于6,这将循环。首先,第二个条件是错误的(根据您的用户界面,六个应该是可接受的选项)。即便如此,这可能与你想要的相反 - 我相信你喜欢选项菜单来循环错误的选择,而不是正确的选择!所以试试这个:

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

答案 1 :(得分:2)

由于您的问题询问如何执行while循环

int choice;

cout << "Enter a choice: " << endl;
cin >> choice;

while (choice != 0) {
    switch(choice) {
        case 1:
            cout << "You entered: " << choice << endl;
            break;
        //all your other options go here..
        default: {
            cout << "Default option goes here..." << endl;
        }
    }
            //this is the important part: you have to update the menu option
            //by prompting the user for a new value:
    cout << "Enter a choice: " << endl;
    cin >> choice;
}

注:

这是一个简单的例子:没有错误检查或输入验证。但是,这是你可以自己做的事情。