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

チケットの価格がどうであれ、これらの条項のうち少なくとも 2 つが true になります。20 の場合は 30 ではありません。25 の場合は 20 ではありません。したがって、これら 3 つのうち少なくとも 2 つが常に true になるため、OR 句全体は常に true になります。したがって、コードは次のようになります。

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 に答える