Jul 18, 2016 at 1:25am UTC
please help me convert this POS c program with pointer and structure. Can someone please
void displayPOS();
void displayINVENTORY();
void addProduct();
void updateProduct();
void deleteProduct();
void searchProduct();
char sort();
void cannotFound();
void backUp();
char array[50][50];
char productarray[50][50];
char name ;
int id;
char desc;
int price;
int qty;
char item;
int total;
int cash;
int change;
int temp;
int result;
int b = 0, c = 0;
char backup [50][50];
int status = 0;
int stts = 0;
int lasttotal = 0;
int main() {
int choice;
printf("MAIN MENU\n");
printf("\n[1] POS");
printf("\n[2] INVENTORY");
printf("\n[3] EXIT");
printf("\nCHOICE: ");
scanf("%d",&choice);
if(choice==1)
{
displayPOS();
main();
}
if(choice==2)
{
displayINVENTORY();
main();
}
if(choice==3)
{
printf("\nProgram Dismissed!\n");
exit(0);
}
}
void displayPOS()
{
printf("\nPOS");
printf("\nEnter Customer Name: ");
scanf("%s",&name);
do
{
printf("Enter product ID: ");
scanf("%d",&id);
result = sort();
temp = productarray [result][0];
cannotFound();
if(id == temp && temp != 0)
{
printf("\nDescription: %c",productarray [result][1]);
printf("\nPrice: %d",productarray [result][2]);
printf("\nEnter Quantity: ");
scanf("%d",&qty);
total=productarray [result][2]*qty;
productarray [result][3] = productarray [result][3] - qty;
do
{
printf("\nShop Again? [Y/N]: ");
scanf("%s",&item);
switch(item)
{
case 'y':
lasttotal = lasttotal + total;
status = 0;
if(stts < 1)
{
stts = 0;
}
stts++;
break;
case 'n':
lasttotal = lasttotal + total;
printf("Total: %d ",lasttotal);
printf("\nEnter Cash: ");
scanf("%d",&cash);
change=cash-lasttotal;
printf("Customer Change is: %d",change);
lasttotal = 0;
backUp();
status = 0;
stts = 0;
break;
default:
printf("\nPlease Try Again Thank You!");
status++;
break;
}
}while(status != 0);
}else if(id != temp && temp == 0){
stts++;
if(stts > 3){
main();
}
}
}while(stts !=0);
}
void displayINVENTORY()
{
int choice2;
printf("\nINVENTORY\n");
printf("\n[1] Add Product");
printf("\n[2] Update Product");
printf("\n[3] Delete Product");
printf("\n[4] Search Product");
printf("\n[5] Cancel");
printf("\nEnter Choice: ");
scanf("%d",&choice2);
if(choice2==1)
{
addProduct();
displayINVENTORY();
}
if(choice2==2)
{
updateProduct();
displayINVENTORY();
}
if(choice2==3)
{
deleteProduct();
displayINVENTORY();
}
if(choice2==4)
{
searchProduct();
displayINVENTORY();
}
if(choice2==5)
{
printf("\n Canceled");
main();
}
else
{
printf("\nInvalid Choice\n");
displayINVENTORY();
}
}
void addProduct()
{
printf("\nADD PRODUCT\n");
printf("\nProduct ID: ");
scanf("%d",&id);
productarray [b][0] = id;
getchar();
printf("\nDescription: ");
scanf("%s",&desc);
productarray [b][1] = desc;
getchar();
printf("\nPrice: ");
scanf("%d",&price);
productarray [b][2] = price;
printf("\nQuantity: ");
scanf("%d",&qty);
productarray [b][3] = qty;
printf("\nPRODUCT ADDED TO INVENTORY\n\n");
backUp();
b++;
}
void updateProduct()
{
printf("\nEnter product ID: ");
scanf("%d",&id);
result = sort();
temp = productarray [result][0];
cannotFound();
if(id == temp){
printf("\nDescription: %c",productarray [result][1]);
printf("\nPrice: %d",productarray [result][2]);
printf("\nQuantity: %d",productarray [result][3]);
getchar();
printf("\nEnter new value\n");
printf("\nDescription: ");
scanf("%[^\n]s",&desc);
productarray [result][1] = desc;
getchar();
printf("\nPrice: ");
scanf("%d",&price);
productarray [result][2] = price;
printf("\nQuantity: ");
scanf("%d",&qty);
productarray [result][3] = qty;
printf("\nPRODUCT UPDATED!");
backUp();
}
}
void deleteProduct()
{
char yn;
printf("\nDELETE PRODUCT\n");
printf("\nEnter product ID: ");
scanf("%d",&id);
result = sort();
temp = productarray [result][0];
cannotFound();
if(id == temp){
productarray [result][0] = 0;
productarray [result][1] = 0;
productarray [result][2] = 0;
productarray [result][3] = 0;
printf("\nAre you sure? [Y/N]");
scanf("%s",&yn);
if(yn == 'n'){
for(int i =0; i < 4; i++){
productarray [result][i] = backup [result][i];
}
displayINVENTORY();
}else if(yn == 'y'){
}
backUp();
}
}
void searchProduct()
{
printf("\nSEARCH PRODUCT\n");
printf("Enter Product ID: ");
scanf("%d",&id);
result = sort();
temp = productarray [result][0];
cannotFound();
if(id == temp){
printf("\nDescription: %c",productarray [result][1]);
printf("\nPrice: %d",productarray [result][2]);
if(productarray [result][3] != 0){
printf("\nQuantity: %d",productarray [result][3]);
}else{
printf("\nQuantity: Out of Stack!");
}
backUp();
}
}
char sort()
{
temp = 0;
int count = 0;
for(int i = 0; i < 50; i++){
if(productarray [i][0] == id){
temp = productarray [i][0];
count = i;
}
}
return (char) count;
}
void cannotFound()
{
if( temp == 0){
printf("\nNo Match Found! \n");
}
}
void backUp()
{
for(int i = 0; i < 4; i++){
backup [result][i] = productarray [result][i];
}
}
Jul 18, 2016 at 3:20pm UTC
What is wrong with this code? If it works : fine! Never touch a running system! ^^
I would not touch the code but write a "facade" arround it to use "string" and other classes of c++.
https://en.wikipedia.org/wiki/Facade_pattern
good luck!
Jul 18, 2016 at 3:40pm UTC
It's not clear if your objective is to leave this in C (adding structures and pointers) or rewrite it in C++.
If your objective is to rewrite this in C++, then you have several things you need to fix:
- Change printf to cout
- Change scanf to cin
- Eliminate the recursive calls to main().
- Create a class for products
In either case,
- Eliminate use of globals
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Edit:
There are a number of things wrong with your program as it exists. Primarily, you're trying to store things like name, desc, price, qty into single characters.
Last edited on Jul 18, 2016 at 4:16pm UTC
Jul 18, 2016 at 4:05pm UTC
I take it you want an illustration of the use of data structures and pointers in C. From the code above, you've used for loop initial declarations (for (int i = 0;...){}) which is legal in C++, C99, etc., but not in C89, so I have to ask whether you have to use a specific version of C, or not? The choice of language dramatically affects how the code may be written.