Trouble validating input

I'm having trouble validating user input in the DISPLAY WELCOME MSG module of my program. I've read the thread on using the getline function, but while solving the problem of an infinite loop if the user enters a char rather than an integer it presents other problems.

One problem that it gives me is the break that is in the code breaks out of the function and returns to the next step of the main program.

Any suggestions would be greatly appreciated. Be gentle... it's my first program....

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

void welcome (int &ans);
void loan_info (float &amt, float &rate);
float payment (float bal, float m_rate, int n_payments);
float power (int exponent, float number);

int main ()
{

	float loan_amt, apr, loan_payment;
	int n_payments, choice;

	system ("color 0A");								//set fonts to green

	welcome(choice);									//call welcome module
	loan_info (loan_amt, apr);							//call loan_info module

	if (choice==1)
		for(n_payments=36; n_payments <= 60; n_payments +=12)
		{
			loan_payment = payment (loan_amt, apr, n_payments);	//call payment module
			cout <<endl<<"Your monthly payment for "<<n_payments<<" months will be:		$" <<loan_payment;
		}
	else
		for(n_payments=180; n_payments <= 360; n_payments +=180)
		{
			loan_payment = payment (loan_amt, apr, n_payments);	//call payment module
			cout <<endl<<"Your monthly payment for "<<n_payments<<" months will be:		$" <<loan_payment;
		}	
	
	return 0;
}

/*********************  DISPLAY WELCOME MSG  ********************/
void welcome (int &ans)
{
	do
	{
		cout<<"Welcome, This program will calculate monthly loan payments."<<endl<<endl;
		cout<<"Would you like to calculate payments for a home loan or car loan?"<<endl<<endl<<endl;
		cout<<"Enter 1 for car loan Enter 2 for home loan:\n>>>  ";
		cin>>ans;
		if ((ans!=1 ) && (ans!=2))
		{
			system("CLS");
			cout<<"I DID NOT RECOGNIZE YOUR INPUT PLEASE SELECT 1 OR 2!"<<endl<<endl;
		}
	}
	while ((ans!=1 ) && (ans!=2));	
}

/*********************  GET LOAN INFORMATION  *******************/
void loan_info (float &amt, float &rate)
{ 
	cout<<"Please enter the amount of the loan:			";cin>>amt;
	cout<<"Please enter the annual percentage rate:		";cin>>rate;	
}

/******************  CALCULATE MONTHLY PAYMENT  *****************/
float payment (float bal, float m_rate, int months)
{
	float bill;

	bill=1.0;	
	m_rate = m_rate/100/12;
	bill = (bal * m_rate) / (1 - (power (-months,(1 + m_rate))));
	return(bill);
}

/*******************************************************************/
float power (int exponent, float number)
{
	float result;
	int i;

	result = 1.0;
	for (i=0; i< abs (exponent); i++) result *= number;
	if (exponent > 0)
		return (result);
	else
		return (1.0 / result);
}
Your program works pretty well. The problem you have is because of the stream nature. If the reading of numbers doesn't go well, the wrong reading info remains in the stream and gives you trouble if you're trying to read another number. You have to handle this. Read a string instead. In this way the stream will drain into that string whatever the information it hold avoiding conversion errors. Further, you do the conversion yourself using "atoi" function, that works without surprises. So, your "welcome" function should receive some modifications like:

1
2
3
4
5
6
7
8
9
10
11
12
void welcome (int &ans)
{
  string sTemp;

  //...

  //cin>>ans;
  cin >> sTemp;
  ans = atoi(sTemp.c_str());

  //...
}

Another thing - you don't have to reinvent the wheel, you might use the existing cmath's "pow" function. That means adding "#include <cmath>" and replacing your "power" function with "pow". You redefine basic functions like you did only in extreme cases, like when you have to write code for some embedded systems where you are very tight on code length and can't afford to include unused functions. But this doesn't seem to be your case.
Thanks for the info.

I agree with using the pow function.. but unfortuanately it was a requirement to use the power function as written.
i like using charactors for my user input, that way no matter what a user enters, it wont crash the program.. like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <limits>

using namespace std;
int main()
{
    start:
    cout << "choose 1, 2, or 3" << endl;
    char a;
    cin >> a;
    if (a=='1') cout << "you chose 1" << endl;
    if (a=='2') cout << "you chose 2" << endl;
    if (a=='3') cout << "you chose 3" << endl;
    cin.sync();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    goto start;
    return 0;
}
Topic archived. No new replies allowed.