First Come First Serve

i'm trying to implement the concept of first come first serve into this code. What can I do?

#include <stdio.h>
#include <iostream>
#include <cctype>
#include <cstdlib>
#include <conio.h>
#include <iomanip>
#include <string.h>
using namespace std;
int main()
{
char *l = " ------------------------------------------------------------------------------\n";
int order = 1;
char name[30],choice;
int num1=0, num2=0, num3=0, num4=0, num5=0,num_customers;
int sentinel=0,rawr=1,ans=1;
double UnitPrice1= 204.00, UnitPrice2= 75.00,UnitPrice3= 39.00, UnitPrice4= 149.00, UnitPrice5= 25.00;
double AmountofSale1=0, AmountofSale2=0, AmountofSale3=0, AmountofSale4=0, AmountofSale5=0,total=0,pepay=999999999,tsenj=0;
system("COLOR A");
while (ans != 0)
{
{
sentinel++;
cout<<"\t __ __ __\n";
cout<<"\t / \\ / \\ ____ | | ____ ____ _____ ____ \n";
cout<<"\t \\ \\/\\/ // __ \\| |/ ___\\/ _ \\ / \\ / __ \\\n";
cout<<"\t \\ /| ___/| | \\__( <_> )| Y Y \| ___/\n";
cout<<"\t \\__/\\__/ \\____\\|__|\\_____>____/ |__|_|__|\\____\\\n";
cout<<"\t _____ ________ __ _\n";
cout<<"\t / \\ ____ \\ \\ ____ ____ _____ | | __| | ______\n";
cout<<"\t / \\ / \\_/ ___\\ | | \\ / _ \\ / \\\\__ \\ | | / __ |/ ___/\n";
cout<<"\t/ Y \\ \\___ | | ( <_> ) | \\/ __ \\| |/ /_/ |\\___ \\\n";
cout<<"\t\\____|__ /\\_____>_________/\\____/|___|___|_____|__|\\____/\\_____/\n\n";
cout<<"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
cout<<" xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ";
cout<<" **************************************************************************** ";
cout<<"\n\tEnter your name to continue: ";
gets(name);
do
{
system("cls");
cout<<"\n CASHIER : ";
puts(name);
cout<<" Do you want to proceed to the main menu? "<<name;
cout<<" \n Press [Y] for Yes, and [N] for No.\n\n\tEnter Choice : ";
cin >> choice;
} while ((choice !='Y')&&(choice !='N')&&(choice !='y')&&(choice !='n'));
system("cls");
switch (choice)
case 1=='Y'|'y':
cout<<" _____\n";
cout<<" / \\ ____ ____ __ __\n";
cout<<" / \\ / \\_/ __ \\ / \\ | | |\n";
cout<<" / Y \\ ___/| | \\| | |\n";
cout<<" \\____|____/\\____>____|____|_____|\n\n";
cout <<" _____(1) Super Burger Php 204.00_____\n"
<<" _____(2) Amazing Spaghetti Php 75.00_____\n"
<<" _____(3) French Fries of Heaven Php 39.00_____\n"
<<" _____(4) Spicy Chicken Wings Php 149.00_____\n"
<<" _____(5) Softdrinks Php 25.00_____\n";
}
while (rawr != 0)
{
do
{
cout<<" \n From the list of food, what would you like : ";
cin>>order;
} while ((order > 5)&&(order < 1));
switch(order)
{
case 1:
cout<<" How many Super Burger would you like to order : ";
cin>>num1;
AmountofSale1 = UnitPrice1 * num1;
break;
case 2:
cout<<" How many Amazing Spaghetti would you like to order : ";
cin>>num2;
AmountofSale2= UnitPrice2 * num2;
break;
case 3:
cout<<" How many French Fries of Heaven would you like to order : ";
cin>>num3;
AmountofSale3= UnitPrice3 * num3;
break;
case 4:
cout<<" How many Spicy Chicken Wings would you like to order : ";
cin>>num4;
AmountofSale4= UnitPrice4 * num4;
break;
case 5:
cout<<" How many Softdrinks would you like to order : ";
cin>>num5;
AmountofSale5= UnitPrice5 * num5;
break;
default: cout<<" Please choose a valid item from our list\n";
}
cout<<fixed;
cout.precision(2);
cout<<" You have ordered:\n\n";
cout<<l;
cout<<" ITEM\t\t\t\tQUANTITY\tUNIT PRICE\tAMOUNT OF SALE\n";
cout<<l;
cout<<" Super Burger:\t\t\t"<<num1<<"\t\tPhp "<<UnitPrice1<<"\tPhp "<<AmountofSale1<<"\n";
cout<<" Amazing Spaghetti:\t\t"<<num2<<"\t\tPhp "<<UnitPrice2<<"\tPhp "<<AmountofSale2<<"\n";
cout<<" French Fries of Heaven:\t"<<num3<<"\t\tPhp "<<UnitPrice3<<"\tPhp "<<AmountofSale3<<"\n";
cout<<" Spicy Chicken Wings:\t\t"<<num4<<"\t\tPhp "<<UnitPrice4<<"\tPhp "<<AmountofSale4<<"\n";
cout<<" Softdrinks:\t\t\t"<<num5<<"\t\tPhp "<<UnitPrice5<<"\tPhp "<<AmountofSale5<<"\n";
total=0;
total=total+AmountofSale1+AmountofSale2+AmountofSale3+AmountofSale4+AmountofSale5;
cout<<l;
cout<<"\t\t\t\t\t\tThat would be: Php "<<total<<"\n";
cout<<l;
do
{
cout<<" Order Again [0] no [1] yes : ";
cin >> rawr;
} while ((rawr != 0)&&(rawr != 1));
}
do
{
system("cls");
if (pepay < total)
{
cout<<"\t\t--- Insufficient Money... PAY OR DIE ??? ---\n";
}
cout<<"\n\tCashier : "<<name<<"\t\t\t\t\tTransaction # "<<sentinel<<"\n";
cout<<l;
cout<<" You have ordered:\n\n";
cout<<" ITEM\t\t\t\tQUANTITY\tUNIT PRICE\tAMOUNT OF SALE\n";
if (num1 != 0)
{
cout<<" Super Burger:\t\t\t"<<num1<<"\t\t"<<UnitPrice1<<"\t\t"<<AmountofSale1<<"\n";
}
if (num2 != 0)
{
cout<<" Amazing Spaghetti:\t\t"<<num2<<"\t\t"<<UnitPrice2<<"\t\t"<<AmountofSale2<<"\n";
}
if (num3 != 0)
{
cout<<" French Fries of Heaven:\t"<<num3<<"\t\t"<<UnitPrice3<<"\t\t"<<AmountofSale3<<"\n";
}
if (num4 != 0)
{
cout<<" Spicy Chicken Wings:\t\t"<<num4<<"\t\t"<<UnitPrice4<<"\t\t"<<AmountofSale4<<"\n";
}
if (num5 != 0)
{
cout<<" Softdrinks:\t\t\t"<<num5<<"\t\t"<<UnitPrice5<<"\t\t"<<AmountofSale5<<"\n";
}
cout<<l;
cout<<"\tTotal : "<<total;
cout<<"\n\tEnter Received Cash : ";
cin >> pepay;
} while (pepay < total);
tsenj = pepay - total;
cout<<"\tYour change is : "<<tsenj<<"\n\n\n\n\t\t";
do
{
cout<<" New transaction [0] no [1] yes : ";
cin >> ans;
} while ((ans != 0)&&(ans != 1));
}
system("cls\n\n\n\t\t");
system("PAUSE");
return 0;
}
Take an order, take cash, give change.

Put that in a loop.

This code has a lot of problems. How did it get this far in such a bad state?
Sorry, but this code makes me cringe...
please use code tags to make it more more readable
for starters...

*system("cls\n\n\n\t\t"); system() is like typing commands into a command line, so the "\n" and "/t" wont work and whats the point of them in the first place??? p.s. i would advise against using system() anything

*you should rewrite your code in a more appealing way

*name your variables better PLEASE
hard to make sense of what you have, but you can probably retrofit a queue to do it.

put the 'things you want to process' into a queue.
remove one, process it.
a queue is a FIFO device which naturally yields a FCFS handling.

most non-threaded code is already linear. That is, most programs naturally do things in the order data comes in. Get some input, process it now, get more input. That is also FCFS logic, and you don't have to DO anything to get it if your design supports this approach. This is what Repeater said, reworded.


Hi ChelseaL,
Maybe you have a requirement ?
Are you just willing to implement your own queue system ?

What you ask for is just what we call a queue. We also call that "first in, first out" or as you stated it "first come, first serve".

If you want to make a simple implementation of that kind of stuff (a queue), you can use a vector<> for example, and a vector of strings if you wish to have texts intead of number.

For the example, I rather use number for the simplicity.

So the underlying structure used for the implementation of the queue will be a
vector<int>
.
Each "client" is an integer (int) and you queue them when they "arrive" and you "unqueue" them each time you take care of her.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <vector>

class Queue
{
public:
	void oneMoreClient(int clientID)
	{
		queue.push_back(clientID);
	}
	int processOneClient()
	{
		return isEmpty() ? -1 : queue[currentClient++];
	}
	bool isEmpty() const
	{
		return queue.size() <= currentClient;
	}

	Queue()
	:	currentClient(0)
	{}

private:
	std::vector<int> queue;
	int currentClient;
};


int main()
{
	Queue restaurant;
	restaurant.oneMoreClient(1);
	restaurant.oneMoreClient(2);
	std::cout << restaurant.processOneClient() << std::endl;
	restaurant.oneMoreClient(3);
	restaurant.oneMoreClient(4);
	std::cout << restaurant.processOneClient() << std::endl;
	restaurant.oneMoreClient(5);
	std::cout << restaurant.processOneClient() << std::endl;
	std::cout << restaurant.processOneClient() << std::endl;
	std::cout << restaurant.processOneClient() << std::endl;
	std::cout << restaurant.processOneClient() << std::endl;
	std::cout << restaurant.processOneClient() << std::endl;
	std::cout << restaurant.processOneClient() << std::endl;
	std::cout << restaurant.processOneClient() << std::endl;
	//--
	return 0;
}

This is ugly implementation right. But it's just to show the most important :
- the two primitive functions of a queue : queue and unqueue. Here those two functionalities are called oneMoreClient() and processOneClient()
- and the isEmpty() function which reflects the status of the queue being empty or not.

In the sample test, you see that 5 clients arrive and to make it simple, they are 1, 2, 3, 4, and 5.
You can then put any of the "std::cout" line anywhere you want... each time, a client will be unqueued... at the end, if you ask for more client, the queue will give you "-1" symbolizing "no more client" or "empty queue".

And then, you can use the queue, dequeue... structure of the std:: ^_^
Last edited on
Topic archived. No new replies allowed.