Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Loop to accumulate money entered

Posted on 2003-11-26
Medium Priority
369 Views
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?
0
Question by:failey
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 86

Accepted Solution

jkr earned 2000 total points
ID: 9825053
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);

}

0

LVL 13

Expert Comment

ID: 9825076
When is the goto used?

where are credit and totalcredit defined? What are their types?
0

LVL 9

Expert Comment

ID: 10285871
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}

Tinchos
EE Cleanup Volunteer
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
###### Suggested Courses
Course of the Month11 days, 14 hours left to enroll