Help with abs function and loop until user type Q?

Hi

I am a beginner.I am writing mortgage loan program. I already done with the program. But, i don't know how to put abs function and loop the program until user type Q. For the abs, when user type -5 in interestrate, input should 5. I have no idea how to do that. Can someone help me?

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
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main()
{
	cout << "Enter your loan amount: $";
	double loanamount;
	cin >> loanamount;
	
	cout << "Enter your yearly interest rate: ";   // Should use abs function here.
	double interestrate;
	cin >> interestrate;
	
	cout << "Enter the loanterm: ";
	double year;
	cin >> year;
	
	double monthlyinterest;
	monthlyinterest = (interestrate / 100.0)/12;
	
	double numberpayments;
	numberpayments = year * 12;
	
	double payment;
	payment = monthlyinterest*pow((1+monthlyinterest), numberpayments)/(pow((1+monthlyinterest),numberpayments)-1)*loanamount;
	
	cout << "Loan Amount: " << "$" << loanamount << endl;
	cout << "Yearly Interest Rate: " << interestrate << "%" << endl;
	cout << "Number of Years: " << year << " years" << endl;
	cout << "Monthyl Payments: " << "$" << payment << endl;
	cout << "Total amount paid for the entire loan term: " << "$" << payment*numberpayments << endl;
	
	return 0;
}
Last edited on
Just modify the interestrate after the user has given the input:

1
2
3
4
5
cout << "Enter your yearly interest rate: ";   // Should use abs function here.
	double interestrate;
	cin >> interestrate;
        
        interestrate=abs(interestrate);


and loop the program until user type Q

do you want to be able to close the program via pressing Q anytime or ask the user a question?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//for question at the end of the loop

char answer;

while(answer!='Q') //termination condition
{ 
      //your code

      std::cout<<"Type Q if you want to quit\n"; //ask the user
      std::cin>>answer;                          //user answers
      answer=toupper(answer);                    //the answer is transformed to uppercase letter, so the                                    
                                                 //result is the same whether the user types lowercase or uppercase Q

}


If you want to be able to quit the program anytime via pressing Q you will have to do use multithreading. Multithreading is a special form of multi-tasking that allows you to run concurrently 2 or more parts of the same program. A basic syntax that would allow you to quit the program anytime via pressing Q is the following (not sure if it's the best)

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

#include<thread> // for std::thread, C++11 only
#include<conio.h>// for _getch()
//other headers

void exit_condition() //function that will be ran by the parallel thread
{
     while(true)                            // loop continuously so it will always check if Q is pressed
        if(_getch()=='Q' || _getch()=='q' ) //trigger if Q or q is pressed
           abort();                         //end program
}

int main()
{
    std::thread quit_Q (exit_condition); //create new thread that runs the exit function
    quit_Q.detach();                     //detach the thread so it runs independently of main

    while(true)                          // the code is looping continuously and you can only exit by pressing Q
    { 
       // your code
    }

  return 0;
}


Though, I think you should stick with the 1st variant for now.
Last edited on
Topic archived. No new replies allowed.