1

这是我正在从事的一个项目,它来自我用来学习 C++ 的书——“从 C++ 开始”。我目前对项目的收银部分有疑问。它要求用户输入图书的日期、数量、isbn、书名和价格。然后,它会询问用户是否希望输入另一本书。无论他们键入“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;
}
4

2 回答 2

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

这并不像你认为的那样。像这样看:

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

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

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

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

if (again == 'N' || again == 'n') {
  break;
}
于 2012-12-14T17:11:13.613 回答
1

循环的问题是这一行:

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

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

相反,请尝试:

if (again == 'N' || again == 'n')
  break;
于 2012-12-14T17:12:48.460 回答