Serendipity书商软件程序C ++

时间:2012-12-14 17:08:49

标签: c++

这是我正在研究的一个项目,它来自我用来学习C ++的书 - “从C ++开始”。我目前在项目的收银员部分遇到问题。它要求用户输入图书的日期,数量,等值,标题和价格。然后,它询问用户是否希望输入另一本书。无论是键入“y”还是“n”,它都会继续执行程序的下一部分。在输入“y”进入另一本书之后,我真的不知道为什么for循环不会重复。此外,日期显示时会显示垃圾,这是我需要修复的另一件事。任何帮助,将不胜感激。肯定存在更多问题但主要问题在于第一个for循环中的收银员功能。我没有包括整个程序,因为它很长。

/*
 *  mainmenu.cpp
 *  Serendipity Booksellers software
 *
 *  Created by Abraham Quilca on 9/5/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */


#include<iostream>
#include<iomanip>
#include<cstring>
#include"mainmenu.h"
using namespace std;

char bookTitle[20][51],
 isbn[20][14],
 author[20][31],
 publisher[20][31],
 dateAdded[20][11];
int qtyOnHand[20];
double wholesale[20];
double retail[20];;

int main()
{
    int choice;

do
{
    cout << "\t\t   Serendipity Booksellers"<< endl;
    cout << "\t\t\t  Main Menu" << endl << endl;
    cout << "\t\t1. Cashier Module" << endl;
    cout << "\t\t2. Inventory Database Module" << endl;
    cout << "\t\t3. Report Module" << endl;
    cout << "\t\t4. Exit" << endl << endl;
    cout << "\t\tEnter your choice: ";
    cin >> choice;
    cout << endl;
    switch (choice)
    {
        case 1:
            cashier();
            break;
        case 2:
            invmenu();
            break;
        case 3:
            reports();
            break;
        case 4:
            continue;
            break;
        default:
            cout << "\t\tPlease enter a number in the range 1-4." << endl << endl;
    }
}   
while(choice != 4);
cout << "\t\tYou selected item 4." << endl;
return 0;
}

// Cashier function

void cashier()
{
    char again;
    char date[8];
    int quantity[20] = {0};
    char ISBN[20][20] = {0};
    char title[20][40] = {0};
    float price[20] = {0}, bookTotal[20] = {0}, subtotal, total, tax;
    const float tax_rate = .06;

    cout << "Serendipity Booksellers" << endl;
    cout << " Cashier Module" << endl << endl;

    for(int count = 0; count < 20; count++)
    {
        cout << "Date: ";
        cin >> date;
        cout << "Quantity of Book: ";
        cin >> quantity[count];
        cout << "ISBN: ";
        cin >> ISBN[count];
        cout << "Title: ";
        cin.ignore();
        cin.getline(title[count], 40);
        cout << "Price: ";
        cin >> price[count];
        bookTotal[count] = quantity[count] * price[count];
        subtotal += price[count];
        cout << "Would you like to enter another book? (Y/N) ";
        cin >> again;
        if(again == 'N' || 'n')
            count = 21; // This line will end the for loop
    }
    // Calculating tax and total
    tax = subtotal * tax_rate;
    total = subtotal + tax;

    cout << "\n\nSerendipity Booksellers" << endl << endl;
    cout << "Date:" << date << endl << endl;
    cout << "Qty\t ISBN\t\t "
        << left << setw(40) << "Title" << "Price\t Total" << endl
        <<     "-------------------------------------------------------------------------------"
        << endl << endl;
    for(int count = 0; count < 20; count++)
    {
        cout << quantity[count] << "\t " << ISBN[count] << "   " << left << setw(40) << title[count] 
        << setprecision(2) << fixed << "$" << setw(6) << price[count] << " $" << setw(6) << bookTotal[count]
        << endl << endl;
    }
    cout << "\t\t\t Subtotal" << "\t\t\t\t         $" << setw(6) << subtotal << endl;
    cout << "\t\t\t Tax" << "\t\t\t\t                 $" << setw(6) << tax<< endl;
    cout << "\t\t\t Total" "\t\t\t\t                 $" << setw(6) << total << endl << endl;
    cout << "Thank You for Shopping at Serendipity!" << endl << endl;
}

2 个答案:

答案 0 :(得分:2)

if(again == 'N' || 'n')

这不符合你的想法。看看它是这样的:

if((again == 'N') || ('n'))

again == N是真还是n是真的吗?好n将永远为真(它是一个非零值的char)所以你的循环总是会立即结束。你想要的是:

if(again == 'N' || again == 'n')

此外,您可以使用恰当命名的break关键字:

来摆脱循环
if (again == 'N' || again == 'n') {
  break;
}

答案 1 :(得分:1)

循环的问题是这一行:

 if(again == 'N' || 'n')

C ++并不知道你的意思是检查again这两个字符。相反,它尝试失败的again == 'N',然后尝试'n',其中 - 不为零 - 评估为真。

相反,请尝试:

if (again == 'N' || again == 'n')
  break;