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?
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)
#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.