How do I end the program my way?
Oct 2, 2012 at 8:37pm UTC
I'm supposed to make my program work in such a way so that, at the end, it pauses and asks for a key stroke to end the program. How do I do it?
It's supposed to look like this at the end of program:
Instead of:
Press any key to continue...
Which Visual Studio puts in on its own.
This is the code I have right now....
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 40 41 42 43 44 45 46 47 48 49 50 51
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main ()
{
double payment, loan_amount, amount_paid, Ipaid, rate;
char c;
int number_of_payments;
//Data Input
cout << "This program calculates monthly payments and interest on a loan\n" ;
cout << "Enter the interest rate as an annual percentage --> " ;
cin >> rate;
cout << "\n Enter the number of payments --> " ;
cin >> number_of_payments;
cout << "Enter the loan amount --> " ;
cin >> loan_amount;
//Calculations
payment = ((rate/100 * pow( 1 + rate/100, number_of_payments)) / (( pow ( 1 + rate / 100, number_of_payments)-1) )) * loan_amount;
amount_paid = payment * number_of_payments;
Ipaid = amount_paid - loan_amount;
//Report Data
cout << "Loan Amount: " << loan_amount << endl;
cout << "Interest Rate: " << rate << "%" << endl;
cout << "Number of Payments: " << number_of_payments << endl;
cout << "Monthly Payment: " << payment << endl;
cout << "Amount Paid Back: " << amount_paid << endl;
cout << "Interest Paid: " << Ipaid << endl;
//Closing
cout << "Program Over" << endl;
cout << "Press Enter to end -->" ;
cin.get();
return 0;
}
Thanks so much for your help.
Oct 2, 2012 at 9:12pm UTC
Loop this from line 47:
1 2 3
mychar = getch();
if (mychar == '\n' )
return 0;
Last edited on Oct 2, 2012 at 9:12pm UTC
Oct 2, 2012 at 11:50pm UTC
Thanks, Soranz, but all I get are warnings. I'm really new at this.
Oct 4, 2012 at 12:50am UTC
Hey, I was thinking something along the lines of this:
1 2 3 4 5 6 7 8 9 10
string s;
cin.ignore(80,'\n' ); //must clear the stream or else the
//previous entry key will count and 'skip' the end
while (1) //loop until user presses enter
{
getline(cin, s);
if (s.find_first_of('\n' ) != 0)
return 0;
}
Last edited on Oct 4, 2012 at 12:50am UTC
Topic archived. No new replies allowed.