사용자 입력 값이 0 이하일 때 오류 메시지가 올바르게 표시되는 데 문제가 있습니다. 나는 그것이 어디로 가야하는지, 아니면 내가 완전히 다른 것을 할 필요가 있을지 확신하지 못한다. 어떤 도움을 주시면 감사하겠습니다. 아래는 내 코드입니다.내 오류 메시지가 제대로 중첩되도록하는 데 문제가 발생했습니다.
지침 : "소프트웨어 회사는 $ 99로 소매하는 패키지를 판매합니다. 수량 할인은"10-19 = 20 % 할인, 20-49 = 30 % 할인, 50-99 = 40 % 할인, 100+ = 50 % 할인. 구매 단위 수를 묻고 구매 총 비용을 계산하는 프로그램을 작성하십시오. 입력 유효성 검사 : 프로그램의 입력을 처리하는 방법을 결정 0보다 작은
#include <iostream>
#include <iomanip> // needed for setprecision
#include <stdio.h>
using namespace std; // need to use cout, cin, etc.
int main()
{
double quantity, package = 99.00, initialPrice, discPrice, discount, calcDiscount, percent; // using double here because future multiplication by .2
cout << "This program will calculate how much of a discount you may receive,\nbased on how many software packages you buy.\n\n";
cout << "How many software packages do you intend to purchase?\n\n";
cin >> quantity;
{
if (quantity <= 0)
cout << "\nThat is not an acceptable amount. Please re-run the program with a amount greater than zero.\n\n";
}
if (quantity >= 10 && quantity <= 19)
discount = 0.2, percent = 20;
else if (quantity >= 20 && quantity <= 49)
discount = 0.3, percent = 30;
else if (quantity >= 50 && quantity <= 99)
discount = 0.4, percent = 40;
else if (quantity >= 100)
discount = 0.5, percent = 50;
else if (quantity <10 && quantity >=1)
discount = 0, percent = 0;
{
initialPrice = package * quantity;
calcDiscount = (discount * package * quantity);
discPrice = initialPrice - calcDiscount;
cout << "\nThe total price for " << quantity << " software packages, minus a " << percent << "% discount of $";
cout << fixed << showpoint << setprecision(2) << calcDiscount << ",";
cout << " is $" << fixed << showpoint << setprecision(2) << discPrice << ".\n\n";
}
return 0;
}
누군가가 19.5의 수량을 입력하면 어떻게 될 것으로 기대합니까? –
여분의 중괄호가 여러 개 있습니다. 예를 들어, 첫 번째'if'는 아무 이유없이 그 안에 싸여 있습니다. 마지막으로'else if' 이후에 많은 양의 코드를 감싸고 있습니다. 그게 마지막으로'else if'와 함께 일어날 작정이라면 그 첫 줄도 포함시켜야합니다. 그렇지 않으면 중괄호를 제거하십시오. – Gavin
C + +를 배우기 위해 어떤 책을 사용하고 있습니까? –