好吧,我正在完成一個書店的程序。但是,運行我的代碼時,它的工作原理是 ,但不正確。首先顯示用戶從4個選項中選擇的主菜單功能,輸入數字並顯示下一個選擇的功能。當試圖從第二個功能中選擇 時,程序返回到第一個功能並重新開始。例如, mainmenu() - > invmenu()(從invmenu()) - >中選擇addBook()函數,但取而代之,它返回到mainmenu(),而不是
。我不知道爲什麼會發生這種情況。我嘗試了每個部分分開 ,他們工作得很好。程序返回到第一個功能,而不是下一個功能
This is my code so far
//The main menu displays a window where the user can
//choose which one module to run or exit the program
//Luis Fierro
//11-8-2014
#include "mainmenu.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdio>
#include <cstdlib>
using namespace std;
//prototypes
void mMenu();
void cashier();
void invmenu();
void report();
void bookInfo();
//inventory function prototypes
void lookUpBook();
void addBook();
void editBook();
void deleteBook();
int main()
{
mMenu();
}
void mMenu()
{
int choice;
do{
//display the menu and get a choice
cout << "\t\t\tSerendipity Booksellers\n" << endl;
cout << "\t\t\t\tMain Menu\n\n" << endl;
cout << "1. Cashier Module\n" << endl;
cout << "2. Inventory Database Module\n" << endl;
cout << "3. Report Module\n" << endl;
cout << "4. Exit \n\n" << endl;
cout << "Enter Your Choice: \n" << endl;
cin >> choice;
//make sure numbers are not out of the range
while (choice <= 0 || choice > 4)
{
cout << "Please enter a number in the range 1-4 " << endl;
cout << "Enter Your Choice: \n" << endl;
cin >> choice;
}
//respond to the user's selection
switch (choice){
case 1: cashier();
break;
case 2: invmenu();
break;
case 3: report();
break;
case 4:
cout << "Bye, bye!!!" << endl;
break;
}
} while (choice != 4);
system("Pause");
}
//function cashier
void cashier()
{
//initialize variables
int option;
string date;
int bQuantity;
string isbn;
string bTitle;
float bPrice;
double total = 0.0;
double tax = 0;
double subtotal = 0.0;
//constant
const double YES_OPTION = 1,
NO_OPTION = 2;
//ask the user
cout << "What is the date(MM/DD/YY): " << endl;
cin >> date;
cout << "\n" << endl;
cout << "Number of books purchased: " << endl;
cin >> bQuantity;
cout << "\n" << endl;
cout << "ISBN number: " << endl;
cin >> isbn;
cout << "\n" << endl;
Court << "Title of book: " << end;
cin >> bTitle;
cout << "\n" << endl;
cout << "Cost of book: " << endl;
cin >> bPrice;
cout << " \n\n" << endl;
//display information to the user
cout << "Serendipity Booksellers\n" << endl;
cout << "\tCashier Module\n\n" << endl;
cout << "Date: " << date << " \n" << endl;
cout << "Quantity of Book " << bQuantity << "\n" << endl;
cout << "ISBN " << isbn << " \n" << endl;
cout << "Title: " << bTitle << "\n" << endl;
cout << "Price: $" << bPrice << "\n\n\n" << endl;
//calculations
subtotal = (bQuantity*bPrice);
tax = (bQuantity*bPrice)*0.06;
total = subtotal + tax;
//display final window with all the info
cout << "Serendipity Book Sellers\n\n " << endl;
cout << "Date: " << date << endl;
cout << "\nQty ISBN Title Price Total\n\n" << endl;
cout << "________________________________________________________\n\n\n" << endl;
cout << " " << bQuantity << " \t\t" << isbn << "\t" << bTitle << "\t\t$" << bPrice << "\t\t$" << subtotal << endl;
cout << " \n\n\tSubtotal: " << "\t\t\t\t\t\t$" << subtotal << endl;
cout << " \n\tTax: " << "\t\t\t\t\t\t\t$" << tax << endl;
cout << " \n\tTotal: " << "\t\t\t\t\t\t\t$" << total << endl;
cout << "\n\nThank You for Shopping at Serendipity!\n\n" << endl;
//ask user if needs another transaction
cout << "\t\tAnother transaction?" << endl;
cout << "\t\t1. Yes" << endl;
cout << "\t\t2. No\n" << endl;
cin >> option;
if (option == YES_OPTION)
{
cashier();
}
else if (option == NO_OPTION)
{
cout << "\nPlease come again!!" << endl;
}
cin.get();
}
//second menu
void invmenu()
{
//initialize variable
int choice1;
do {//display the menu
cout << "\t\t\tSerendipity Booksellers\n" << endl;
cout << "\t\t\tInventory Database\n" << endl;
cout << "\n\t\t\t1. Look Up a Book" << endl;
cout << "\n\t\t\t2. Add a Book" << endl;
cout << "\n\t\t\t3. Edit a Book's Record" << endl;
cout << "\n\t\t\t4. Delete a Book" << endl;
cout << "\n\t\t\t5. Return to the main menu" << endl;
cout << "\n\t\t\tEnter Your Choice: \n " << endl;
cout << "Enter Your Choice: \n" << endl;
cin >> choice1;
//make sure numbers are not out of range
while (choice1 <= 0 || choice1 > 5)
{
cout << "Please enter a number in the range 1-5 " << endl;
cout << "Enter Your Choice: \n" << endl;
cin >> choice1;
}
//Respond to the user's selection
switch (choice1){
case 1: lookUpBook();
break;
case 2: addBook();
break;
case 3: editBook();
break;
case 4: deleteBook();
break;
case 5: mMenu();
break;
}
} while (choice1 = !5);
cin.get();
}
//last functions for the invmenu
void lookUpBook()
{
cout << "You selected look up a book" << endl;
}
void addBook()
{
cout << "You selected add a book" << endl;
}
void editBook()
{
cout << "You selected edit a book" << endl;
}
void deleteBook()
{
cout << "You selected delete a book" << endl;
}
學習如何使用調試器並逐步完成代碼的時間。 – 2014-12-07 03:22:16
提示:看看'choice1 =!5'做什麼。 – SleuthEye 2014-12-07 03:28:07