very very simple math problem

I'm very very new to c++ and cant seem to find out why my output is coming out like this. Heres my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53


#include <cmath>
#include <iostream>
using namespace std;

int main()
{

	const double TAX_RATE = .0825;

	double MarkedPrice, TubePrice, Tax, SellingPrice, TubeQuantity, TotalTube, SeatUpgrade, BeforeTax,BikeTotal,BikeDiscount;

    //Get the price on the price tag
	int SeatPrice;

	//Calculate TotalBeforeTax
		BeforeTax=MarkedPrice+TotalTube+(SeatUpgrade);

	cout <<"What is the price indicated on the bicycles price tag?";
	cin >> MarkedPrice;
	cout <<"10% off all new bicycles, so the bicycle will be:     "   <<BikeDiscount <<endl;
	cout <<"What is the price indicated on the tubes price tag? ";
	cin >> TubePrice;
	cout <<"How many tubes do you want?";
	cin >> TubeQuantity;
	cout <<"Total cost of tubes will be: "<<TotalTube <<endl;

	//Seat options
	SeatPrice=85;
	SeatUpgrade=SeatPrice*.5 ;
	cout<<"The seat upgrade you wanted will cost $85 but since you bought a new bicycle it comes 50% off" <<endl;
	cout<<"The seat will cost you: $"<<SeatUpgrade <<endl;

	//Calculate Bike Discount 10%
	BikeDiscount= MarkedPrice*(.9);

	//Calculate TotalTube cost
	TotalTube= TubePrice*TubeQuantity;

	//Calculate the sales tax
	Tax=MarkedPrice*TAX_RATE;

	//Calculate the final selling price
	SellingPrice=MarkedPrice + Tax;


	//Display the tax and final selling price
	cout << "Sales tax:  $" << Tax << endl;
	cout << "Final selling price:  $" << SellingPrice <<endl;

	return 0;
}



and then this is my output
What is the price indicated on the bicycles price tag?1248
10% off all new bicycles, so the bicycle will be:     8.71808e+159
What is the price indicated on the tubes price tag? 11.95
How many tubes do you want?2
Total cost of tubes will be: 1.28909e-317
The seat upgrade you wanted will cost $85 but since you bought a new bicycle it comes 50% off
The seat will cost you: $42.5
Sales tax:  $102.96
Final selling price:  $1350.96


If anyone can help thatd be awesome, i know this is super simple Im just not good with programming yet, thank you!
Last edited on
Please Format: your post; (look to the right of the edit box when you post.)
Last edited on
double MarkedPrice, TubePrice, Tax, SellingPrice, TubeQuantity, TotalTube, SeatUpgrade, BeforeTax,BikeTotal,BikeDiscount;

please always initialize variables before use them.

1
2
double MarkedPrice, TubePrice, Tax, SellingPrice, TubeQuantity, TotalTube, SeatUpgrade, BeforeTax,BikeTotal,BikeDiscount;
MarkedPrice = TubePrice = Tax = SellingPrice = TubeQuantity = TotalTube = SeatUpgrade = BeforeTax = BikeTotal = BikeDiscoun = 0;


preferable: one var each line
1
2
3
4
5
6
7
8
9
10
double MarkedPrice =0;
double TubePrice = 0;
double Tax = 0;
double SellingPrice = 0;
double TubeQuantity = 0;
double TotalTube = 0;
double SeatUpgrade = 0;
double BeforeTax = 0;
double BikeTotal = 0;
double BikeDiscount = 0;

ty for the tips, why in my output does it say "10% off all new bicycles, so the bicycle will be: 8.71808e+159" and "Total cost of tubes will be: 1.28909e-317"
thats what I don't get.
because you have not initialized BikeDiscount and TotalTube yet, so the output values can be any number... Always initialize variables first before using them.

double type has 8 bytes, or 64 bits. Since you don't initialize them, these bits can be 000110101011... or 1010110001100... or 00000...00 (if you are lucky) and the program will just print those values out without knowing what they are.

More information about double format (how the computer translates those bits into our floating numbers): http://en.wikipedia.org/wiki/Double-precision_floating-point_format
Last edited on
Several of your calculations appear to be out-of sequence. That is the calculation is done either too soon or too late. In addition there are some uninitialised values used. That is, the absurdly large or small numbers are due to a variable being displayed or used in a calculation before it has been given any value.

Personally, I'd prefer to set each and every value to zero at the start, when it is first declared. That way, you can at least understand where a zero output is coming from, and set about fixing it.

There may be other errors, but this will do for now.
ok thanks a lot, my teacher didnt teach me the start from 0 method then fill in. I will try this, thanks a lot if I have more questions about it I will post
My "set to zero" suggestion was as much an aid to debugging as anything else. In a well-written program it isn't strictly necessary, but can be useful during development.
Topic archived. No new replies allowed.