# Loop to accumulate money entered

float entryofcredit ()
{
entrypoint:

cout<<"\nOnly the following coins are valid: \n";
cout<<"5c, 10c, 20c, 50c, 100c\n";
Sleep (400);

cin>>credit;
cout<<"--------------------------------------\n";

while(credit !=0)
{
switch(credit)
{
case '5':
cout<<"\nYou entered 5 cents"<<endl;
break;
case '10':
cout<<"\nYou entered 10 cents"<<endl;
break;
case '20':
cout<<"\nYou entered 20 cents"<<endl;
break;
case '50':
cout<<"\nYou entered 50 cents"<<endl;
break;
case '100':
cout<<"\nYou entered 100 cents"<<endl;
break;
default:
cout<<"This coin is invalid "<<endl;
break;
goto entrypoint;
}

totalcredit = totalcredit + credit;
cout<<"\nThe total money entered so far            is... "<<totalcredit<<endl;
cout<<"----------------------------"<<endl;
cout<<"\nInsert Coin... Press 0 to finish"<<endl;
cin>> credit;
}

cout<<"\nThe total money entered is... "<<totalcredit<<endl;
cout<<"--------------------------------"<<endl;
Sleep(500);

}

//______________________Function Complete_______

How do I get the credit to accumulate within the loop?
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
There are several big problems:

- 'totalcredit' seems to be declared outside the function
- you are using a 'goto', which is a 'no-no'.
- the credit is entered outside the loop
- you are trying to 'switch' on strings

The following should gove you an idea:

#include <stdlib.h>

int entryofcredit ()
{

int totalcredit = 0;
char buffer [ 16];
int credit;
char* pcCnvEnd;

cout<<"\nOnly the following coins are valid: \n";
cout<<"5c, 10c, 20c, 50c, 100c\n";
Sleep (400);

do {

cout<<"\nThe total money entered so far            is... "<<totalcredit<<endl;
cout<<"----------------------------"<<endl;
cout<<"\nInsert Coin... Press 0 to finish"<<endl;
cin>> credit;
cin>>buffer;
cout<<"--------------------------------------\n";

credit = strtol ( buffer, &pcCnvEnd, 10);

if ( *pcCnvEnd) {

cout << "Not a coin" << endl;

continue;
}

switch(credit)
{
case 5:
cout<<"\nYou entered 5 cents"<<endl;
break;
case 10:
cout<<"\nYou entered 10 cents"<<endl;
break;
case 20:
cout<<"\nYou entered 20 cents"<<endl;
break;
case 50:
cout<<"\nYou entered 50 cents"<<endl;
break;
case 100:
cout<<"\nYou entered 100 cents"<<endl;
break;
default:
cout<<"This coin is invalid "<<endl;
break;
}

totalcredit = totalcredit + credit;

} while ( credit != 0);

cout<<"\nThe total money entered is... "<<totalcredit<<endl;
cout<<"--------------------------------"<<endl;
Sleep(500);

}

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
When is the goto used?

where are credit and totalcredit defined? What are their types?
Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: jkr {http:#9825053}