0

間違ったオプションを選択した後、メニューが繰り返されることに問題があります。私の指示は、外部ファイルから読み取り、それをメニューで使用することです。銀行の明細書を読んで、カテゴリに分けて、合計する必要があります。銀行の取引明細書の集計方法がよくわかりません。番号を選択すると、何も読み取れません。

銀行の明細書は次のとおりです。

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;
}
4

2 に答える 2

3

あなたはあなたの状態がdo-while混ざっているようです。

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

choiceこれは、 が 0 より大きく 6 より小さい限りループします。まず、2 番目の条件が間違っています (ユーザー インターフェイスによると、6 が許容可能なオプションであるはずです)。それでも、これはおそらくあなたが望んでいることの逆です-オプションメニューが正しい選択ではなく、間違った選択でループすることを望んでいると思います! だからこれを試してください:

while ((choice < 0) || (choice > 6));
于 2013-11-09T22:35:35.520 に答える
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;
}

ノート:

これは単純な例です。エラー チェックや入力検証はありません。ただし、それは自分でできることです。

于 2013-11-09T22:40:13.420 に答える