Do While Statements

I am having a hard time grasping the idea of do...while statements. Could someone better explain to me how I can get the code I posted to repeat

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
/******************************************************************************
						Monthly Payment Calculator
******************************************************************************/



#include <iostream>
#include <math.h>
#include <string>
using namespace std; 

int main() {
	float years;					/* float - decimal points; */
	float apr;
	float purchasePrice;
	float downPayment;
	float monthlyPayment;				
	
	//cout << "$" << monthlyPayment;	
	
	cout << "What is the price of the vehicle? ";
	cin >> purchasePrice;
	cout << "How much is your down payment? ";
	cin >> downPayment;
	cout << "What is the APR? ";
	cin >> apr;
	cout << "How many years will you finance for? ";
	cin >> years;
	
	float interestRate = apr *0.01 / 12;
	float numberofMonths = years * 12;
	float a = interestRate * (purchasePrice - downPayment);
	float b = pow((1 + interestRate),numberofMonths);
	float c = pow((1 + interestRate),numberofMonths)-1;
	monthlyPayment = (a * (b / c));
	
	cout << "Your monthly payment will be " << "$" << monthlyPayment;
	return 0;
 
I also realized I don't need the header #include <string>. I meant to take that out, but haven't. It does compile and run w/o out it
This will print out 1(I've pre-incremented i variable) even when condition is false, 'cuz this loop runs code inside it at least once, then it checks condition and if it's true in runs code again, if it's false, it goes farther.
1
2
3
4
int i = 0;
do{
  std::cout << ++i;
}while(condition); // with ';', it's important 
I am alittle confused as to what kind of condition I would have to include. I am trying to get my output to ask me, "Do you want to run this again Yes/No?"
Condition is eihter true or false. If you want to ask user you can declare variable, ask user and then check if it's yes or no. Condition would be something like this:
1
2
3
do{
...
}while(input == "Yes"); // or anything similar you want 
Last edited on
I tried that.....at the start of int main () (

I dropped do { my code
....
} while (input == "Yes");

It told me that input was not in the scope, so after the first open bracket. I created an int input;.....this did not work....Earlier I tried using just a continuous loop by using for ( " " ) but that didn't give the command time enough to read the monthly payment before it started at the beginning
You've got to make variable of the correct type. If you want something like this:
Do you want to continue? y/n y

it would be best if input was a char. If you want this:
Do you want to continue? yes/no no

input can be char array or std::string. It won't know if input = "yes" or if it's equal to "no" until you get answear from the user. You can do it that way:
1
2
3
4
5
6
std::string input;
do{
...
std::cout << "Do you want to continue? yes/no ";
std::cin >> input;
} while (input == "yes");
Last edited on
Topic archived. No new replies allowed.