C++ Program
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
    Wallisellen (ZH), Switzerland
    Posts
    17,291

    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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center