Quote (Eep @ Jul 4 2012 01:38pm)
looking in mirrors is for hipsters
here is my finished code now:
Code
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double x, rate, total = 0;
cout << setprecision(2) << showpoint << fixed;
cout <<"Please enter the rate of interest" << endl;
while (cin >> rate) {
if (rate < .01 || rate > .1)
cout << "Please enter a percent between 1% and 10% please!" << endl;
else break;
}
cout <<"Please enter some values"<< endl;
while (cin >> x) {
if (x<0)
cout << "Negative number detected: Ignored." << endl;
else {
total += x;
total *= 1+rate;
}
}
cout <<"Your total is: $" << total << endl;
//double variables means total might not be 100% accurate!!
return 0;
}
Hey, just looked at this and had some observations. I haven't read through the all the posts, but it seemed like a flame-war for a bit. So, moving along....
The first while loop, while it would achieve your desired result, doesn't make sense to me. Here's how I would have done it:
Code
cin >> rate
while (rate < .01 || rate . 1) {
cout << "Please enter a percent between 1% and 10% please!" << endl;
cin >> rate.
}
In the second while loop, there is no break, creating yourself a nice little infinite loop. Here is what I would do:
Code
cout <<"Please enter some positive values (enter a negative value to stop)"<< endl;
cin >> x;
while (x > 0) {
total += x;
total *= (1 + rate);
cin >> x;
}
What the loop needed was some sort of exit criteria, and since you only wanted positive values, make negative values your loop breaker. That's a fun term. Might have to learn to play an instrument and start a band called Loop Breaker.
This post was edited by joeshabadoo on Jul 5 2012 11:34am