CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: C++ Program

  1. #1
    Join Date
    Mar 2013
    Posts
    4

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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: C++ Program

    Quote Originally Posted by gamesun View Post
    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

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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
  •  





Click Here to Expand Forum to Full Width

Featured