PDA

View Full Version : entered variable as a decimal and answer in decimal, c++

benlangdon
Mar 7, 2008, 12:12 AM
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.
*
*/
#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;
}

ebel3003
Mar 7, 2008, 01:29 AM
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.

/*
* HomeWork 8.h
*
*
* Created by Ben Langdon on 3/6/08.
*
*/
#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.

benlangdon
Mar 7, 2008, 02:01 AM
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.

/*
#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;
}

ebel3003
Mar 7, 2008, 02:09 AM
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.

benlangdon
Mar 7, 2008, 02:13 AM
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.

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

ebel3003
Mar 7, 2008, 02:16 AM
No problem, they were easy fixes and I love working with and helping aspiring programmers. I'm one myself.

benlangdon
Mar 7, 2008, 02:27 AM
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

ebel3003
Mar 7, 2008, 02:59 AM
Instead of using integers or floats, use doubles for decimal values.

int myInteger = 2;
double myDouble = 2.5;