0

晚上大家。

我有一个计划,该计划旨在提供票价,并让父母和儿童筹款活动来打折机票。该程序很简单,但我还应该进行数据验证,以确保用户只输入 20、25 或 30 作为票价。否则,该程序有机会重新进入。但是,当我运行程序时,无论我输入什么,它都会返回一个错误,即使它是正确的数字。另外,如果我碰巧注释掉循环,它运行良好,但会自行关闭并说:

'High School Dance.exe': Loaded 'C:\Users\Shaidi\Documents\Visual Studio 2010\Projects\High School Dance\Debug\High School Dance.exe', Symbols loaded.
'High School Dance.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
'High School Dance.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
'High School Dance.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
'High School Dance.exe': Loaded 'C:\ProgramData\Norton\{0C55C096-0F1D-4F28-AAA2-85EF591126E7}\NIS_20.2.0.19\Definitions\BASHDefs\20130412.001\UMEngx86.dll', Cannot find or open the PDB file
'High School Dance.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded.
'High School Dance.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
The thread 'Win32 Thread' (0xa5c) has exited with code -1073741510 (0xc000013a).
The thread 'Win32 Thread' (0x304c) has exited with code -1073741510 (0xc000013a).
The program '[10896] High School Dance.exe: Native' has exited with code -1073741510 (0xc000013a).

任何人都可以帮忙吗?这是程序:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

//Prototypes
void studentTicket (void);
void parentFund(void);
void childFund(void);
void totalCosts(void);
void displayTotal (void);
void highestCost (void);
void process (void);

//Global Variables
double ticketPrice, candySold, magsSold, parentDiscount = 0, studentDiscount = 0,     schoolCost = 0;

int main(void)
{
    process();
    return 0;
}

void process(void){
    studentTicket();
    parentFund();
    childFund();
}

void studentTicket(void){
    double ticketPrice;
    cout << "How much does the ticket cost?" <<endl;
    cin >> ticketPrice;

    cout << endl;

    while (ticketPrice != 20 || ticketPrice != 25 || ticketPrice != 30){
        cout << "Invalid amount. A ticket can only cost $20, $25, or $30. Please re-enter a valid amount: " <<endl;
        cin >> ticketPrice;
    }

}

void parentFund(void){

    cout << "How many magazines did the parent sell?" <<endl;
    cin >> magsSold;

    if (magsSold >= 0 && magsSold <= 9){
        parentDiscount = 0;
    } else if (magsSold >= 10 && magsSold <= 15){
        parentDiscount = ticketPrice * .05;
    } else if (magsSold >= 16 && magsSold <= 30){
        parentDiscount = ticketPrice * .15;
    } else if (magsSold >= 31 && magsSold <= 49){
        parentDiscount = ticketPrice * .30;
    } else if (magsSold >= 50){
        parentDiscount = ticketPrice * .50;
    } else {
        cout << "The value entered in invalid." << endl;
    }
    cout <<endl;
    cout << "****************************************************************" <<endl;
    totalCosts();
    cout << "****************************************************************" <<endl;
}

void childFund(void){

    string studentName;
    cout << endl;
    cout << "****************************************************************" <<endl;

    cout << "Enter the student name: " <<endl;
    cin >> studentName;

    cout << endl;
    cout << "How many candies did " <<studentName << " sell?" <<endl;
    cin >> candySold;

    if (candySold > 50){
        studentDiscount = (candySold - 50) * .25;
        studentDiscount += candySold * 15;
    } else  if (candySold >= 1 && candySold <= 50){
        studentDiscount = candySold * .15;
    } else {
        cout << "The number entered is invalid. Please try again." <<endl;
    }
    cout << "****************************************************************" <<endl;
    cout << endl;
    cout << "****************************************************************" <<endl;
    totalCosts();
    cout << "****************************************************************" <<endl;
}

void totalCosts (void){
    schoolCost = studentDiscount + parentDiscount;
    cout << "Parent Cost: " << ticketPrice - parentDiscount << endl;
    cout << "Student Cost: " << ticketPrice - studentDiscount << endl;
    cout << "School Cost: " << schoolCost << endl;

}
4

1 回答 1

3
while (ticketPrice != 20 || ticketPrice != 25 || ticketPrice != 30){
    cout << "Invalid amount. A ticket can only cost $20, $25, or $30. Please re-enter a valid amount: " <<endl;
    cin >> ticketPrice;
}

无论票价是多少,这些条款中至少有两个是正确的。如果是 20,则不是 30。如果是 25,则不是 20。因此,这三个中至少有两个将始终为真,因此整个 OR 子句将始终为真。所以你的代码是一样的:

while (true){
    cout << "Invalid amount. A ticket can only cost $20, $25, or $30. Please re-enter a valid amount: " <<endl;
    cin >> ticketPrice;
}

显然你永远无法退出这个while循环。

于 2013-04-23T04:47:23.607 回答