-
May 15th, 2013, 10:46 PM
#1
C++ Program
I'm trying to create a program that will take input from a user and calculate it in a do-while loop. The program does the calculation but the answer is wrong. The loop also doesn't work. The purpose of the program is to see how much an item will cost after a discount is taken off and tax is added.
#include <iostream>
#include <cmath>
using namespace std;
double original_cost;
double discount;
double tax;
double total;
char answer;
int main()
{
do {
cout<<"What is the original price?";
cin>>original_cost;
cout.setf (ios::fixed);
cout.setf (ios::showpoint);
cout.precision(2);
cout<<"What is the discount?";
cin>>discount;
cout.setf (ios::fixed);
cout.setf (ios::showpoint);
cout.precision(2);
cout<<"What is the tax?";
cin>>tax;
cout.setf (ios::fixed);
cout.setf (ios::showpoint);
cout.precision(2);
cout<<"The total is"<<original_cost*discount+original_cost*tax-discount;
cout<<"Do you want to try again?";
cin>>answer;
} while(answer=='Y'||'y');
return 0;
}
-
May 16th, 2013, 04:11 AM
#2
Re: C++ Program
Originally Posted by gamesun
I'm trying to create a program that will take input from a user and calculate it in a do-while loop. The program does the calculation but the answer is wrong. The loop also doesn't work. The purpose of the program is to see how much an item will cost after a discount is taken off and tax is added.
I do NOT see any "calculation" in the code snippet you have posted. Perhaps, it is the reason why "the answer is wrong"?
Besides, you have to use Code tags while posting code snippets. Please, read the Announcement: Before you post.... (see section "Information on posting")
Victor Nijegorodov
-
May 16th, 2013, 06:44 AM
#3
Re: C++ Program
This is not really what you mean.
Code:
} while(answer=='Y'||'y');
This tests if answer is 'Y' or 'y' is non-zero (which it always is). So this statement is always true. In c/c++ you have to formulate the tests correctly and not as you would say in English. The correct code would be
Code:
} while(answer == 'Y' || answer == 'y');
For the calculation,
Code:
original_cost * discount + original_cost * tax-discount;
Multiplication takes precedence over addition, so this statement evaluates as
Code:
(original_cost * discount) + (original_cost * tax) - discount;
Hope this helps.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|