entered variable as a decimal and answer in decimal, c++

Discussion in 'Mac Programming' started by benlangdon, Mar 6, 2008.

  1. macrumors 65832

    benlangdon

    Joined:
    Jan 13, 2008
    #1
    this is what i got
    how do i go from here to compile because it isn't letting me


    /*
    * HomeWork 8.h
    *
    *
    * Created by Ben Langdon on 3/6/08.
    * Copyright 2008 __MyCompanyName__. All rights reserved.
    *
    */
    #include <iostream>

    using namespace std;

    int main ( void )
    {
    int firstAge; //first age
    int secAge; //second age
    int ageSum; //sum of te ages
    float avgAge; //average of ages

    cout<<"\n\n\nBen Langdon\nLab1\n2-28-08\n\n";

    //obtain2 ages from the user
    cout<<"Enter first age:";
    cin>>firstAge;
    cout<<"Enter second age:";
    cin>>secAge;

    //Calculate the sum and average of the ages
    ageSum = firstAge + secAge;
    aveAge = ageSum / 2.0;

    //Output tghe calculated results
    cout<<"\nThe sum of the ages is "<<ageSum<<" and the average is "<<avgAge;
    }
     
  2. macrumors 6502a

    ebel3003

    Joined:
    Jun 20, 2007
    Location:
    "The Google"
    #2
    The problem was that you declared a float as "avgAge" but then referred to it as "aveAge" later in the program. I've doctored it for you.

    Code:
    /*
    * HomeWork 8.h
    *
    *
    * Created by Ben Langdon on 3/6/08.
    * Copyright 2008 __MyCompanyName__. All rights reserved.
    *
    */
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
    	int firstAge; //first age
    	int secAge; //second age
    	int ageSum; //sum of te ages
    	float avgAge; //average of ages
    
    	cout << "\n\n\nBen Langdon\nLab1\n2-28-08\n\n";
    
    //obtain2 ages from the user
    	cout << "Enter first age:";
    	cin >> firstAge;
    	cout << "Enter second age:";
    	cin >> secAge;
    
    //Calculate the sum and average of the ages
    	ageSum = firstAge + secAge;
    	avgAge = ageSum / 2.0;
    
    //Output tghe calculated results
    	cout << "\nThe sum of the ages is " << ageSum << " and the average is "<< avgAge;
    }
    In the future, it would be easier for you to post the code and the error that you're getting. It was easy enough to copy and paste in Xcode and hit compile, this time.
     
  3. thread starter macrumors 65832

    benlangdon

    Joined:
    Jan 13, 2008
    #3
    ya i found that out.
    i was writting it in c++ applications and now im doing it in c++ direct line something.
    i tried posting the errors but it was over a page long.

    but now my problem is when i compile and execute the program which works btw :), when i enter in a value it is supposed to go to the next value to be entered and it will not go to there for some reason. it happens and then it doesn't.


    Code:
    /*
    #include <iostream>
    
    using namespace std;
    
    int main ( void )
    {
    	int itemPrice;     // Item price
    	int tax;           // Tax
    	float totalTax;    // Total tax
    	float total;       // Entire price 
    	
    	cout<<"\n\n\nBen Langdon\nComputer Science 1a\nHW8\n3/06/08\n\n\n";
    	
    	//Enter item price
    	cout<<"Enter Item:";
    	cin>>itemPrice;
    	cout<<"Enter Tax Amount:";
    	cin>>tax;
    	
    	//Calculate
    	totalTax = itemPrice * tax;
    	total = totalTax + itemPrice;
    	
    	
    	//Output the calculated results
    	cout<<"\nThe amount of tax on the item is: "<<totalTax<<"\nTotal "<<total;
    }
    
     
  4. macrumors 6502a

    ebel3003

    Joined:
    Jun 20, 2007
    Location:
    "The Google"
    #4
    Use the enter key next to the quote key rather than the number pad's enter key. I noticed this a little while when trying to compile your code.
     
  5. thread starter macrumors 65832

    benlangdon

    Joined:
    Jan 13, 2008
    #5
    omg wtf is that.
    thank you though
    truly you have saved me so much time i already wrote the other 2 apps due tomorrow but it wouldn't execute it and i just tried and worked on all 3.

    thank you so much.
    i really appreciate the time you spent working with my code

    thank you
     
  6. macrumors 6502a

    ebel3003

    Joined:
    Jun 20, 2007
    Location:
    "The Google"
    #6
    No problem, they were easy fixes and I love working with and helping aspiring programmers. I'm one myself.
     
  7. thread starter macrumors 65832

    benlangdon

    Joined:
    Jan 13, 2008
    #7
    one last question :D
    how do i let it be able to enter in a decimal for a value
    like where do i place %
    in the variable name or when its doing the action
     
  8. macrumors 6502a

    ebel3003

    Joined:
    Jun 20, 2007
    Location:
    "The Google"
    #8
    Instead of using integers or floats, use doubles for decimal values.

    Code:
    int myInteger = 2;
    double myDouble = 2.5;
     

Share This Page