Bill shortage: Basic Coding I am a beginner in coding. I was doing this question today. 


  A new movie has been released. Many people are buying tickets which costs 25 dollars. There are many people wanting a ticket but each person has either one of   25 dollar bill, 50 dollar bill or 100 dollar bill. 
  
  People will only come in the order of queue. You initially have no money. Write a program to see if you can sell ticket to everyone or not.


This is my solution 

#include&lt;iostream.h&gt;
#include&lt;conio.h&gt;
#include&lt;stdlib.h&gt;

void main()
{  clrscr();
    int queue;
    int bill[100];
    cout&lt;&lt;"Enter number of people in queue:";
    cin&gt;&gt;queue;
    cout&lt;&lt;"\n(Bill value 25/50/100)";
    for (int i=0;i&lt;queue;i++)
    {
        cout&lt;&lt;"Bill person "&lt;&lt;i+1&lt;&lt;" have:";
        cin&gt;&gt;bill[i];
    }
    int bill25=0;
    int bill50=0;
    int bill100=0;
    for(i=0;i&lt;queue;i++)
    {
        if(bill[i]==25)
        {   bill25++;
        }
        else if(bill[i]==50)
        {
            if(bill25&gt;0)
            {
                bill25--;
                bill50++;
            }
            else
            {
                cout&lt;&lt;"No change";
                exit(0);
            }
        }
        else if(bill[i]==100)
        {
            if(bill50&gt;0&amp;&amp;bill25&gt;0)
            {
                bill50--;
                bill25--;
                bill100++;
            }
            else if(bill25&gt;=3)
            {
                bill25-=3;
                bill100++;
            }
            else
            {
                cout&lt;&lt;"No change";
                exit(0);
            }
        }
        else
        {
            cout&lt;&lt;"Wrong bill";
            exit(0);
        }
    }
    cout&lt;&lt;"Tickets sold";
    getch();
}


My testing has found this to be correct (maybe not the best but correct) solution. But I am not sure about it. Are there any possibilities in which this code fails. Also what would be a better solution at my level?
Okay, first up, I'm a bit rusty with C++, so I apologize if this isn't formatted correctly.

But, the first thing that leaps out is "Single Responsibility Principle".  Aka - each function should be responsible for one thing and have one reason to change.

Or in short: don't write everything in your Main().

What is your Main() doing, at a high level?  It's:


Grabbing inputs for how many people and what bills they have
It's setting up the register (with no bills in it)
It's looping through each person
It's checking how that person impacts the current register
It's outputting the success/failure of the venture.


Awesome!  So... what should your main look like in an abstract sense?

main()
{
    int patronCount = GetPatronCount();
    int[] billAmounts = GetBillAmounts();
    int[] register = SetUpRegister();
    for (i = 0; i &lt; patronCount; i++)
    {
        bool wasAbleToMakeChange = MakeChangeForPatron(register, billAmounts[i]);
        if (!wasAbleToMakeChange)
        {
            // code to return out with failure
        }
    }
    // code to return success
}


So... why would we do this?  Well, because right now, if you need to tweak a line of code in your code?  That could have ramifications all throughout your function.  You can't exactly digest it in small bits, because everything has 'scope' with everything else.  And if you needed to change something, you'd have to consider how that change affects everything else in that function.

This is incredibly important as you grow as a programmer - you're going to be changing code a lot more than you're going to be composing it - and the simpler you can make changing it, the better off you'll be.

Also, it becomes much easier to see what the program does.  Because the function names and variable names document exactly what it's doing at a high level.  It's getting a patron count.  Imagine someone reading your code (and only your code - no explanations)... how quickly would they figure out what it's doing?  Contrast that to my version's main().  So if someone else needs to work with your code (or if you need to work with it, but don't remember it very well) it'll be able to be grok'ed a lot quicker.