[help]C++, Discount calculation

For class I am supposed to write a program to calcualte discounts...

Using a "for" loop have the program calculate the actual perchase price after ther discount for the nest $20 in $0.50 increments...

Example

Starting Price:
Percent discount:

Price -Diss New $
$1.00 $0.20 $0.80
$1.50 $0.30 $1.20
$1.00 $0.40 $1.60
........
$21.00 $4.20 $16.80

Now here is my code... I am useing VS.NET 2005 Beta 1 and in wont let me compile with "std::" infront of the i/o streams... Why do I need them?

 #include <iostream>
int main() {
// Variables
float startprice;
float discount;
float h;

// info
std::cout << "Enter the Starting Price: t$";
std::cin >> startprice;
std::cout << "Percent Discount: t";
std::cin >> discount;

//For loops and stuff
for(h = startprice; h <= (20 + startprice); (h+.5)) {
std::cout << "Pricet-DisctNew $";
std::cout << h << "t" << h/discount << "t" << h-(h / discount);
}
return 0;
}

#257117

Try:

#include <iostream>

using namespace std;

'

// ...

BTW, you will need an std::endl somewhere to flush the cout-buffer to the screen actually.

#257118

Try:

#include <iostream>
using namespace std;
'
// ...

BTW, you will need an std::endl somewhere to flush the cout-buffer to the screen actually.

Yeah, try what he said, but take out all the std::'s in front of your couts and cins after you put the using namespace in there.

Also, std::endl will actually force a hard-return into your stream. If you want the input right beside your prompts, you could probably use put an std::flush on the end, if I'm not mistaken.

#257164

Okay, I did all that and I get mostly the right answers... but...

I don't understand why I get this where the $1 should be evcery thing else works

Enter the Starting Price:	   $1
Percent Discount (in %): 20
Price -Disc New $
$1 $-1.07374e+008 $1.07374e+008
$1.5 $0.3 $1.2
$2 $0.4 $1.6
$2.5 $0.5 $2
$3 $0.6 $2.4
$3.5 $0.7 $2.8
$4 $0.8 $3.2
$4.5 $0.9 $3.6
$5 $1 $4
$5.5 $1.1 $4.4
$6 $1.2 $4.8
$6.5 $1.3 $5.2
$7 $1.4 $5.6
$7.5 $1.5 $6
$8 $1.6 $6.4
$8.5 $1.7 $6.8
$9 $1.8 $7.2
$9.5 $1.9 $7.6
$10 $2 $8
$10.5 $2.1 $8.4
$11 $2.2 $8.8
$11.5 $2.3 $9.2
$12 $2.4 $9.6
$12.5 $2.5 $10
$13 $2.6 $10.4
$13.5 $2.7 $10.8
$14 $2.8 $11.2
$14.5 $2.9 $11.6
$15 $3 $12
$15.5 $3.1 $12.4
$16 $3.2 $12.8
$16.5 $3.3 $13.2
$17 $3.4 $13.6
$17.5 $3.5 $14
$18 $3.6 $14.4
$18.5 $3.7 $14.8
$19 $3.8 $15.2
$19.5 $3.9 $15.6
$20 $4 $16
$20.5 $4.1 $16.4
$21 $4.2 $16.8

#257199

I don't understand why I get this where the $1 should be evcery thing else works

Don't know why it'd be doing that, but a current code listing might help.

#257357

Code dump

 
#include <iostream.h>
using namespace std;
int main() {
// Variables
float startprice;
float discount;
float h;
float j;
float k;

// info
cout << "Enter the Starting Price: t$";
cin >> startprice;
cout << "Percent Discount (in %): t";
cin >> discount;

//For loops and stuff
cout << "Pricett-DiscttNew $n";
for(h = startprice; h <= (20 + startprice);h) {
k = h * j;
j = discount / 100;

cout << "$"<< h << "tt" << "$"<< k << "tt" << "$" << (h - k) << "n";
h = h + 0.5;
}
return 0;
}

#257457

When you output the -Disc variable, try typecasting it to an int.

cout << "$"<< h << "tt" << "$"<< [b](int)[/b] k  << "tt" << "$" << (h - k) << "n";

(change marked in bold)

#257473

cout << "$"<< h << "tt" << "$"<< (int) k  << "tt" << "$" << (h - k) << "n";

Should be:

cout << "$"<< h << "tt" << "$"<< static_cast<int>(k)  << "tt" << "$" << (h - k) << "n";

;)

#257486

Actually, the starting value of the variable j is undefined in the first loop iteration. Just declare is as

float j = 0;

and the first result line should be ok

#257491

@ AndreasV: C++ showoff! I got that code out of my old C++ book... guess it's time for a new book :(

...or just stick to VB.NET ;)

#257499

cin >> discount;



//For loops and stuff

cout << "Pricett-DiscttNew $n";

for(h = startprice; h <= (20 + startprice);h) {

k = h * j;

j = discount / 100;

Vorlon was half right. The first time through the loop, you multiply h times j before you even assign j. However, his solution will output wrong results. You should have this:

cin >> discount;



j = discount / 100;



//For loops and stuff

cout << "Pricett-DiscttNew $n";

for(h = startprice; h <= (20 + startprice);h) {

k = h * j;

That'll solve your problem.

Oh, and don't typecast to an int. Since prices (and discounts) are in dollars and cents, using floats was the right idea.

#257558