1

im looking to make a program in c that will print out a specific number of pizzas (defined by the user), these pizzas will be called at random from a pre-defined array. So far so good, but now im looking to add a unique order id to the pizzas when they are ordered. I'm having issues with this and cant seem to get my head around how to do it (i am very new to c) so any help would be much appreciated. If there was a way to then list these "orders" any help on that would also be fantastic.

#include<stdio.h>
#include <stdlib.h> /* required for randomize() and random() */
#include <conio.h> 

int main()
{
//Initialising array and variable
char* pizza[]={"Marinara","Prosciutto","Prosciutto e Funghi","La Napoletana","L    Atomica","Quattro Stagioni","Capricciosa"};  
int numberofpizza;
int clientID = 0;

//Request how many pizzas for that order.
printf("How many pizzas would you like? ");
scanf("%d", &numberofpizza);



    //Loop for the ammount of pizzas made.
    srand(time(NULL));
    int i=0;

    while (i<(numberofpizza))
    {
    int randompizza = rand()%7;
    printf("%s\n", pizza[randompizza]);
    i++;
    }


getch();
return 0;
}
Alan Stokes
  • 18,815
  • 3
  • 45
  • 64
  • Please remove the C# tag if not relevant. – NoChance Nov 17 '13 at 12:57
  • checkout [`__COUNTER__`](http://stackoverflow.com/questions/652815/has-anyone-ever-had-a-use-for-the-counter-pre-processor-macro) macro – amdixon Nov 17 '13 at 13:08
  • I just want the order id's to be an integer 2 or 3 digits long. I havent tried anything with it yet, I only started C just over a day ago and I am having trouble finding content on creating a unique ID online. I dont know enough about the language to get creative and try and find a method of doing it myself, I was hoping somebody could point me in the right direction via a certain command or link possibly. – user3000359 Nov 17 '13 at 13:09

2 Answers2

0

I think you need to generate a guid.For that you can use the function CryptGenRandom from winapi (since you use getch() i assume windows platform). For more details on how to generate guid have a look at this.

Community
  • 1
  • 1
deeiip
  • 3,319
  • 2
  • 22
  • 33
0

How unique does your order id need to be? If it suffices that your program does not give the same order id twice, just add one to the id of the previous order (i.e., ++order_id) every time you get a new one. The ids will simply be sequential: 1, 2, 3, … (start counting from 100 or 1000 if you want more digits).

If the order id needs to be hard to guess (e.g., to prevent customers from guessing another person's order id), then you could add some random digits to the order id (e.g., printf("order id: %u%03u\n", order_id, rand() % 1000)).

If you want the id to be unique across multiple runs of the program, you can store the previous id somewhere, or base it on the current time (e.g, the return value of time(NULL)).

If the order id needs to be globally unique, i.e., no other pizzeria in the world should reasonably be expected to generate the same order id, ever, then you could generate a UUID or something similar to that, but I expect it would be massively overkill for this assignment.

Arkku
  • 41,011
  • 10
  • 62
  • 84
  • ive taken your advice and now have a random id that is shown after every pizza that is given. How would i be able to then store these to each id? its just that the next thing that i want to do is to list all current orders in id order. Thanks again. – user3000359 Nov 17 '13 at 13:37
  • @user3000359 If you mean how to add the random digits to your order id, something like `order_id * 1000 + (rand() % 1000)` (don't store the result in `order_id` but in another variable). These numbers will already be in order since the most significant digits come from `order_id` (which you should increment after every order: `++order_id`). – Arkku Nov 17 '13 at 13:42
  • Sorry about this, like i said before im not a very good coder and on top of that i have only been learning c a day. I have changed my while loop to the following int i=0; printf("order id: %d\n", order_id); while (i<(numberofpizza)) { int randompizza = rand()%7; printf("%s\n", pizza[randompizza]); i++; } so now it prints the order id, and then lists the random pizza's. Im not sure on 1. How to keep that id unique, and 2. how to store that information in an array. Would i need to use the unique id as the place in the array and then store the pizzas in that? Sorry! – user3000359 Nov 17 '13 at 13:52