Using centinel

D

d.j.

I want this program to stop after you enter 0 in for the customer number
and not have to enter 0 for all fields before it ends. Please help.. Thanks

#include <iostream.h>

int main()
{
float principle = 1;
float ni = 1; // number of payments per year
int count = 0;// total loans processes
int cust = 1; //customer number
float intrest = 1;// interest charges
float tpay = 0;//total payments
float rate = 1;//intrest in percentages

while (principle != 0)
{
cout <<"\nEnter customer number\n";
cin >> cust;

cout <<"\nEnter payments per year\n";
cin >> ni;

cout << "\nEnter intrest charge\n";
cin >> intrest;

cout << "\nEnter principal loan amount\n";
cin >> principle;

cout << "\Enter total numbers of payments\n";
cin >>tpay;

if (principle == 0)
cout << "the end\n";
else count++;
if (principle > 0)

rate = (2 * ni * intrest* 100) / (principle * (tpay + 1));

if (principle>0)
cout << "customer number & principle loan amt: "<< cust << " " <<
principle << " ";
if (principle > 0)
cout << " Interest in percentages " << rate << " % " << '\n';
}
cout << "Total loans processed " << count << '\n';

return 0;
}
 
V

Victor Bazarov

d.j. said:
I want this program to stop after you enter 0 in for the customer number
and not have to enter 0 for all fields before it ends. Please help.. Thanks

#include <iostream.h>

int main()
{
float principle = 1;
float ni = 1; // number of payments per year
int count = 0;// total loans processes
int cust = 1; //customer number
float intrest = 1;// interest charges
float tpay = 0;//total payments
float rate = 1;//intrest in percentages

while (principle != 0)
{
cout <<"\nEnter customer number\n";
cin >> cust;

So, here your program already knows the 'cust' value. Check if it's
the "sentinel" value and bail out of the 'while' loop. Read about
'if' statement and 'break' statement.
 
J

John Harrison

d.j. said:
I want this program to stop after you enter 0 in for the customer number
and not have to enter 0 for all fields before it ends. Please help.. Thanks

Simple enough.
#include <iostream.h>

int main()
{
float principle = 1;
float ni = 1; // number of payments per year
int count = 0;// total loans processes
int cust = 1; //customer number
float intrest = 1;// interest charges
float tpay = 0;//total payments
float rate = 1;//intrest in percentages

while (principle != 0)
{
cout <<"\nEnter customer number\n";
cin >> cust;

if (cust == 0)
break;

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top