Jump to content

What's wrong with my C++ program?

infernowheels

*EDIT*

As reference, here's the problem:

10358698_10204004030335136_3617331768106

 

This program should loop until cost reaches 0.. I've tried every possible condition for the while loop however it still only runs once in Dev C++ 4.9.9.2...

 

DISCLAIMER:

>Some conditions(like >=, > and !=) gives me a blank program for some reason..

>Removing the semicolon after the while statement makes the compiler not recognize the while loop..

 

#include<iostream>

using namespace std;

main()
{
float cost = 1000, rate = 0.015, payment = 50, interest;
int month = 0;

while(cost <= 0);
{
interest = (cost*rate);
payment = (payment - interest);
cost = (cost - payment);
month++;
}

cout << interest << endl;
cout << payment << endl;
cout << cost << endl;
cout << month << endl;

system("pause");
return 0;
}

{B t t tk Pf t B t t tk Pf tk B Pf} <--- This is my language. BEATBOXING FOR LIFE!

Link to comment
Share on other sites

Link to post
Share on other sites

The loop is a bit messed up. You said you want it to run until the cost hits zero, but the way you have written it, the loop will only execute while the cost is less than or equal to zero to begin with. You want it the other way around, execute while the cost is greater than zero (and consequently stop when cost is no longer greater than zero). Also there should not be a semicolon after a loop statement

while(cost>0) {

}

Link to comment
Share on other sites

Link to post
Share on other sites

The loop is a bit messed up. You said you want it to run until the cost hits zero, but the way you have written it, the loop will only execute while the cost is less than or equal to zero to begin with. You want it the other way around, execute while the cost is greater than zero (and consequently stop when cost is no longer greater than zero). Also there should not be a semicolon after a loop statement

while(cost>0) {

}

Refer to Disclaimer above code...

{B t t tk Pf t B t t tk Pf tk B Pf} <--- This is my language. BEATBOXING FOR LIFE!

Link to comment
Share on other sites

Link to post
Share on other sites

#include<iostream>

using namespace std;

int main() // better put that int in front
{
float cost = 1000, rate = 0.015, payment = 50, interest = 0.0; // better initialize interest
int month = 0;

while(cost <= 0) // and there shouldn't be semicolon here
{
interest = (cost*rate);
payment = (payment - interest);
cost = (cost - payment);
month++;
}

cout << interest << endl;
cout << payment << endl;
cout << cost << endl;
cout << month << endl;

system("pause");
return 0;
}

Link to comment
Share on other sites

Link to post
Share on other sites

#include<iostream>

using namespace std;

int main() // better put that int in front

{

float cost = 1000, rate = 0.015, payment = 50, interest = 0.0; // better initialize interest

int month = 0;

while(cost <= 0) // and there shouldn't be semicolon here

{

interest = (cost*rate);

payment = (payment - interest);

cost = (cost - payment);

month++;

}

cout << interest << endl;

cout << payment << endl;

cout << cost << endl;

cout << month << endl;

system("pause");

return 0;

}

>Actually my compiler works even without int before main.

>Initializing interest to zero makes no difference.

>I know there shouldn't be one, but for some reason the while loop statement gets skipped by the compiler(even with the right conditions) if I don't put one in.

{B t t tk Pf t B t t tk Pf tk B Pf} <--- This is my language. BEATBOXING FOR LIFE!

Link to comment
Share on other sites

Link to post
Share on other sites

>Actually my compiler works even without int before main.

>Initializing interest to zero makes no difference.

>I know there shouldn't be one, but for some reason the while loop statement gets skipped by the compiler(even with the right conditions) if I don't put one in.

by putting the semicolon, you make a while loop with an empty body, and then under it just a block of code. It's like writing this:

 

while(cost <= 0) 

}

interest = (cost*rate);

payment = (payment - interest);

cost = (cost - payment);

month++;

 

So i would say the compiler or the IDE is the problem because on mine it runs fine.

Not sure what else to assume.

Link to comment
Share on other sites

Link to post
Share on other sites

  1 #include<iostream>  2   3 using namespace std;  4   5 int main()  6 {  7     float cost = 1000, rate = 0.015, payment = 50, interest = 0;  8     int month = 0;  9  10     while(month < 50) 11     { 12         cout << cost << " "; 13         interest = (cost*rate); 14         payment = (payment - interest); 15         cost = (cost - payment); 16         month++; 17     } 18  19     std::cout << interest << std::endl; 20     std::cout << payment << std::endl; 21     std::cout << cost << std::endl; 22     std::cout << month << std::endl; 23  24 //    system("pause"); 25     return 0; 26 }

Output:

 

1000 965 944.475 938.117 945.831 967.732 1004.15 1055.63 1122.94 1207.1 1309.37 1431.27 1574.65 1741.64 1934.76 2156.9 2411.39 2702.06 3033.25 3409.94 3837.79 4323.2 4873.46 5496.81 6202.63 7001.48 7905.35 8927.8 10084.2 11391.8 12870.3 14541.9 16431.6 18567.7 20982.4 23711.8 26796.9 30284 34225.3 38680 43714.9 49405.5 55837.2 63106.4 71322.3 80608 91102.8 102964 116370 131521 1972.82

-17124.2

148646

50

 

 

Seems like calculation error.

Link to comment
Share on other sites

Link to post
Share on other sites

the errors i see are these:

while(cost <= 0);

that is wrong because of

1. the semicolon, that will just keep testing the condition endlessly

2. the condition: you want that loop to keep going as long as there is something to pay, i.e. cost > 0

 

cost, payment and interest should be integer variables, and you need them to store the amount of cents (e.g. 1000$ are 100000 cents). storing prices in floats will leave you with the problem of always having a very small payment to do, or having to pay 30.23425$ which is clearly not possible

 

 

anyway there has been a post exactly identical to this one in the past, maybe you can find it (edit: link)

Link to comment
Share on other sites

Link to post
Share on other sites

>Actually my compiler works even without int before main.

>Initializing interest to zero makes no difference.

>I know there shouldn't be one, but for some reason the while loop statement gets skipped by the compiler(even with the right conditions) if I don't put one in.

What if you port it to a different compiler? Learn good code practice

Initializing to 0 makes a difference, Visual Studio initializes to minimum value, i.e. negative billions

Delete the semi-colon and make while condition cost>0. At the moment you are only allowing to enter the loop when cost is smaller than 0. And its 1000 do begin with, it'll never enter the loop.

 

Lastly, good tip from Ciccioo, money should be stored as integer and as cents/pennies or your local equivalent.

 

EDIT:

about your disclaimer, result gives you blank program because with:

cost = 1000;

and 

while(cost<=0);

you miss the loop and go to couts immediately, and with:

while(cost>0);

your are stuck in the loop (just that one line, the stuff in {} is not executed because of the semi-colon) because cost will always be bigger than 0 in this case.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×