[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Loop to accumulate money entered

Posted on 2003-11-26
4
Medium Priority
?
371 Views
Last Modified: 2012-06-27
float entryofcredit ()
{
entrypoint:

cout<<"\nOnly the following coins are valid: \n";
      cout<<"5c, 10c, 20c, 50c, 100c\n";
      cout<<"\n Insert Your Coins Now... Please Press 0 to finish\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);

return totalcredit;
}

//______________________Function Complete_______

How do I get the credit to accumulate within the loop?
0
Comment
Question by:failey
3 Comments
 
LVL 86

Accepted Solution

by:
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";
     cout<<"\n Insert Your Coins Now... Please Press 0 to finish\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);

return totalcredit;
}


0
 
LVL 13

Expert Comment

by:SteH
ID: 9825076
When is the goto used?

To your question: What is your exact problem?  

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

Expert Comment

by:tinchos
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}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question