Manipulating CIN data.

Hello, I just began working with programming here. So I'am just learning about cin and cout.

Our teacher asked us to write a program to create a loan report for the user. Its relative simple: We ask how much the are borrowing, interest rate, how long they will take to pay it off. Then we proceed to give them a report on the information we've received and even adding how much they will have to pay monthly.

I've got the whole thing working and right now I'am just trying to manipulate how cin reads the data the user gives it. One problem I ran into when I had a friend run the program for me was that he entered the loan amount as 10,000(with the comma) while I entered it as 10000(without the comma). Now from his view I understand why he did this, its just easier to read the number. However, it creates a problem with my program(does the wrong calculations) and I started on a quest to see how I could ignore that darn comma or heck even a "$" character.

Here's my code:
//This program displays info. on Monthly Payments

//Preprocessor Directives
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
//Initialize Variables/Arrays
double loanAmount, interestRate, Payments, monthlyPayment,
amountPaidBack, interestPaid, Percentage;


const int SIZE = 10;
const int reportSetw = 10;

char ch;


char firstName[SIZE];
char lastName[SIZE];

cout << "Hello! I'll help you with your loan report today.\n";

//Ask for General Information.
cout << "\nEnter your First and Last Name separated by a space.";
cin >> firstName >> lastName;

//Find out the Loan Amount.
cout << "\nHow much are you borrowing?";
cin >> loanAmount;

//Find out the Interest Rate.
cout << "\nWhat's the interest rate? Enter it in decimal notation. Ex 1% becomes .01.";
cin >> interestRate;

//Find out how many Payments.
cout << "\nHow many months will it take you to pay the loan off?";
cin >> Payments;

//Calculate the Monthly Payment Amount.
monthlyPayment = ((interestRate * pow((1 + interestRate), Payments))/(pow((1 + interestRate), Payments) - 1)) * loanAmount;

//Calculate the Amount Paid Back.
amountPaidBack = monthlyPayment * Payments;

//Calculate Interest Paid.
interestPaid = amountPaidBack - loanAmount;

//Turn Interest rate to a percetage.
Percentage = interestRate * 100;

//Display Report.

cout << "\nLoan Report for " << lastName << ", " << firstName << endl;
cout << "*************************************";
cout << "\nLoan Amount: $ " << setprecision(2) << fixed << setw(reportSetw) << loanAmount;
cout << "\nMonthly Interest Rate: " << setprecision(2) << fixed << setw(9) << Percentage << "%";
cout << "\nNumber of Payments: " << setprecision(0) << fixed << setw(reportSetw) << Payments;
cout << "\nMonthly Payment: $ " << setprecision(2) << fixed << setw(reportSetw) << monthlyPayment;
cout << "\nAmount Paid Back: $ " << setprecision(2) << fixed << setw(reportSetw) << amountPaidBack;
cout << "\nInterest Paid: $ " << setprecision(2) << fixed << setw(reportSetw) << interestPaid << endl;

//Pause program to view info.
//cout << "\nPress a key to continue......";
//cin.ignore();
//cin.get(ch);
system("pause");

return 0;

}


*********************
As you can see its simple in nature. (As I cruised around for an answer and found me way here I found out that system("pause") was bad XD)

Anywho, If I could get some help with this I would appreciated it! If your interested I'am working with Gaddis' book "Starting out with C++" 6th edition.

ok well in the case of the user entering a comma, really the only way around it is to read it in as a string. numbers don't have commas in them (not true numbers anyway). you can then write a function or such to put that string into an integer using stringstream. However by the look of your program by design it doesn't look like you have covered functions yet, or your being lazy. :P

Anyway something like this:
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
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int Convert_2_Int ( string  str )
{
    int i;
    stringstream ss(str);
    if (ss >> i)
        return i;
    else
        return -1;
}

int main()
{

    int amount=0;
    string  strAmnt="";

    do
    {
        cout << "Enter amount: ";
        getline (cin, strAmnt);
        amount =  Convert_2_Int ( strAmnt );
    } while (amount == -1)

    return 0;
}

edit:
The above still won't accept commas (it will just keep asking the user to enter a number until it's valid), however I didn't want to dump too much on you at once.
actually taking a number with commas and parsing it into number the C++ compiler will understand can be done using string manipulation eg. string.find (','); and string.substr() would be able to do this.
Last edited on
Topic archived. No new replies allowed.