2013-11-09 52 views
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; 
} 
+1

你的問題非常可怕。嘗試縮短到[SSCCE](http://sscce.org/),*請*。 – jrd1

回答

3

你似乎有do-while混了你的條件。

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

只要choice大於0小於6,就會循環。首先,第二個條件是錯誤的(根據您的用戶界面,六個應該是一個可接受的選項)。即使這樣,這可能是你想要的相反 - 我相信你會喜歡選項菜單循環錯誤的選擇,而不是正確的選擇!所以,試試這個:

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

非常感謝你這麼快回復! – user2975029

+0

分號應該在聲明的結尾嗎? –

+0

是的,這是一個'do-while'循環。 – kviiri

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

注:

這是一個簡單的例子:有沒有錯誤檢查或輸入驗證。但是,這是你可以自己做的事情。

+0

非常感謝您的回覆! – user2975029