Błąd logiczny - czy ktoś może pomóc?

0

Witam,
czy ktoś może podpowiedzieć mi na co mam zwrócić uwagę szukając rozwiązania błędu logicznego? Próbowałam już na wiele sposobów, jednak nadal wychodzi mi podatek całkowity równy zeru, natomiast cena brutto się zaokrągla... bardzo proszę o pomoc.

//-----------------------------------------------------------------
// void priceCalculation()
//
// Summary: Reads price and number of articles from user. Calculates and prints
// quantity, vat and price with vat
// Returns: -
//-----------------------------------------------------------------
void priceCalculation()
{
// Define and initialize constants and variables
const int RATE = 25; // tax rate in percent
double price = 0; // price per piece
int nrOfArticles = 0; // number of articles
double rateEUR = 0; // tax rate in EUR
double totalPrice = 0; // price incl. tax rate
// Read price and number of articles
cout << "Enter the price excl. the tax rate: ";
cin >> price;
cout << "Enter the number of articles: ";
cin >> nrOfArticles;
// Calculate total price and tax rate
rateEUR = totalPrice * RATE;
totalPrice = nrOfArticles * price *(1 + RATE);
// Display result with 2 decimals
cout << fixed << showpoint << setprecision(2);
cout << nrOfArticles << " number of articles cost " << totalPrice << " euro. "
<< endl << "Of this " << rateEUR << " euro is the tax rate." << endl;
}

0

Dlatego, że rateEUR = totalprice(0) * Rate. A jak wiadomo 0*n = 0 :D
Edit: pomyliłaś dwie linijki: zamień totalPrice z rateEUR miejscami i będzie gites.

0

Niestety cały czas mam gdzieś błędy...

#include <iostream>
#include <iomanip>
using namespace std;
// Prototypes
void priceCalculation();
int main()
{
char answer;
do
{
priceCalculation();
cout << "One more time? (Y/N): ";
cin >> answer;
}while (answer = 'Y' || answer == 'y');
return 0;
}
//-----------------------------------------------------------------
// void priceCalculation()
//
// Summary: Reads price and number of articles from user. Calculates and prints
// quantity, vat and price with vat
// Returns: -
//-----------------------------------------------------------------
// void priceCalculation()
//
// Summary: Reads price and number of articles from user. Calculates and prints
// quantity, vat and price with vat
// Returns: -
//-----------------------------------------------------------------
// void priceCalculation()
//
// Summary: Reads price and number of articles from user. Calculates and prints
// quantity, vat and price with vat
// Returns: -
//-----------------------------------------------------------------
void priceCalculation()
{
// Define and initialize constants and variables
const int RATE = 25; // tax rate in percent
double price = 0; // price per piece
int nrOfArticles = 0; // number of articles
double rateEUR = 0; // tax rate in EUR
double totalPrice = 0; // price incl. tax rate
// Read price and number of articles
cout << "Enter the price excl. the tax rate: ";
cin >> price;
cout << "Enter the number of articles: ";
cin >> nrOfArticles;
// Calculate total price and tax rate
totalPrice = nrOfArticles * price *(1 + RATE);
rateEUR = totalPrice * RATE;
// Display result with 2 decimals
cout << fixed << showpoint << setprecision(2);
cout << nrOfArticles << " number of articles cost " << totalPrice << " euro. "
<< endl << "Of this " << rateEUR << " euro is the tax rate." << endl;
}

screenshot-20181111152718.png

0

Dobrze, tylko co Ci nie działa? Powiedz najpierw co chcesz osiągnąć.

0

Chce policzyć sumę brutto (niestety wychodzi mi netto) i osobno sumę podatku VAT od tego zakupu. Kiedy podam cenę 10 euro, 1 sztuka i 25% VAT chcę aby program policzył że mam do zapłacenia 12,5 EUR w tym 2,5 EUR wynosi podatek.

1

Popatrz: rateEUR = totalPrice * RATE w tym miejscu nie dzielisz, a mnożysz podatek co daje jakieś absurdalne wyniki. Ponadto nie dzielisz przez 25% tylko przez samo 25 , czyli ze 100 euro podatek wyniesie 4€. Czy aby na pewno? Dlatego możesz po prostu łatwo zadeklarować że 25% to 4. 100/4 da już 25, czyli poprawnie. Później wystarczy wypisać Cenę netto + podatek = totalPrice+rateEUR.
Oto jak wygląda kod po przekształceniu zmiennych:

totalPrice = nrOfArticles * price;
rateEUR = totalPrice / RATE; //zmień stałą RATE na 4.
// Display result with 2 decimals
cout << fixed << setprecision(2);
cout << nrOfArticles << " number of articles cost " << totalPrice+rateEUR<< " euro. "

Ponadto pętla ma zły operator logiczny. Zmień OR na AND, aby działała prawidłowo.

0

Działa!!! Dziękuję OGROMNIE!

1 użytkowników online, w tym zalogowanych: 0, gości: 1