Thread: Calculations are stopping my program from executing? Beginner question

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    3

    Calculations are stopping my program from executing? Beginner question

    Hey guys, first post so first I'd like to say hello. I've followed the site and read multiple tutorials for C++ so far. I'm actually going to school for programming. Although I'm quite the beginner, I'm very eager to learn.

    I have this issue with some very basic code that I figured maybe someone could point me in the right direction to a solution. Basically if I leave the calculations out, I can get the outputs displayed, but once I add them in, only the input works. Anyways, onto the code.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    //declaring global variables
    
    double totalPrice = 0;
    double purchasePrice = 0;
    double totalTax = 0;
    float countyTax = 0;
    float stateTax = 0;
    
    //prototpyes
    void input();
    void calc();
    void output();
    
    int main()
    {
        //calls in main
        input();
        calc();
        output();
        system("pause");
        return 0;
    }
    
    void input()
    {
        
        cout << "Enter your purchase price: ";
        cin  >> purchasePrice;
    
       
       
       
    }//end of input
    
    void calc()
    {
        
     countyTax = purchasePrice * .02;
     cin >> countyTax;
     stateTax = purchasePrice * .04;
     cin >> stateTax;
     totalTax = stateTax + countyTax;
     cin >> totalTax;
     totalPrice = (stateTax + countyTax) * purchasePrice;
     cin >> totalPrice;
    
    } //end of calc
    
    void output()
    {
        cout << "Your purchase cost: " << purchasePrice  << endl;
    cout << "Your total county sales tax is: " <<countyTax << endl;
    cout << "Your total state sales tax is : " <<stateTax <<endl;
    cout << "Your total purchase cost including tax is: " <<totalPrice <<endl;
    
    
    }
    
    //end of output
    Thanks for any help in advance!

  2. #2
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    What compiler are you using? What do you mean it "stops?"

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    I've tried the code in Visual Studio and Bloodshed, both have given me the same result. Once executed it will allow me to enter the input for the purchase price, I hit enter, and nothing happens.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
     countyTax = purchasePrice * .02;
     cin >> countyTax;
     stateTax = purchasePrice * .04;
     cin >> stateTax;
     totalTax = stateTax + countyTax;
     cin >> totalTax;
     totalPrice = (stateTax + countyTax) * purchasePrice;
     cin >> totalPrice;
    Take a close look at what this is doing...
    Order of events...
    What does "cin" do?

  5. #5
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    Take a look at calc() and those cin statements... you keep asking the user for more input

    $ ~/a.out
    Enter your purchase price: 23.2
    23
    42
    2
    34
    Your purchase cost: 23.2
    Your total county sales tax is: 23
    Your total state sales tax is : 42
    Your total purchase cost including tax is: 34

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    Wow....I'm embarrassed. I feel like my Python knowledge may have confused me due to always returning variables, however I can't explain why I was thinking "cin" was returning them for me. Thanks guys, that's one stupid mistake I'll never make again.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Devizion View Post
    Thanks guys, that's one stupid mistake I'll never make again.
    As they say...

    True intellect is never making the same mistake twice...
    because you're too busy making new ones.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a Battleship Program
    By HBlakeH in forum C Programming
    Replies: 1
    Last Post: 12-05-2010, 11:13 PM
  2. Executing program from a Command Line
    By pantherman34 in forum C Programming
    Replies: 1
    Last Post: 05-05-2010, 01:13 PM
  3. C program compile but not doing the calculations
    By abs.emailverify in forum C Programming
    Replies: 8
    Last Post: 11-08-2006, 08:43 AM
  4. Replies: 40
    Last Post: 09-01-2006, 12:09 AM
  5. Random Question Assign Program
    By mikeprogram in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2005, 10:04 PM