mardi 19 mai 2015

Having problems with rand() in c

I am trying to write a program that simulates a pyjama picking. The main function need to get the amount of pyjamas and the kind of pyjamas as arguments(3 kinds of pyjamas: cheap, regular and expensive). Each pyjama has a name, size and price, the program will show the user the number of pyjamas and kind he entered as parameters. Then it will randomize 10 pyjamas as the following: different names different size (XS, S, M, L, XL) price: cheap: 10-30$ regular: 30-60$ expensive: 60-732.5$ Then the user will be able to choose the pyjamas that he liked until he reaches the number he entered in the arguments. I wrote this code:

#include<stdio.h>
#include<stdlib.h>
#include <time.h>
int main(int argc, char** argv)
{
    srand(time(NULL));
    int i, j;
    char* size = (char*)malloc(sizeof(char) * 3);
    float* price = (float*)malloc(sizeof(float));
    char* name = (char*)malloc(sizeof(char) * 8);
    printf("The number of pyjamas is: %s and the kind of pyjamas is: %s\n", argv[1], argv[2]);
    for (i = 0; i < 10; i++)
    {
        if (argv[2] == "cheap")
        {
            *price = (float)(rand() % 20 + 10);
        }
        if (argv[2] == "regular")
        {
            *price = (float)(rand() % 30 + 30);
        }
        if (argv[2] == "expensive")
        {
            *price = (float)(rand() % (6725 + 600)) / 10;
        }
        *size = (char)(rand() % 5);
        if (size == 0)
        {
            size = "XS";
        }
        if (size == 1)
        {
            size = "S";
        }
        if (size == 2)
        {
            size = "M";
        }
        if (size == 3)
        {
            size = "L";
        }
        if (size == 4)
        {
            size = "XL";
        }
        for (j = 0; j < 8; j++)
        {
            *name = (char)(rand() % 26 + 97);
        }
        printf("%f\n", price);
        printf("%s\n", size);
        printf("%s\n", name);
    }
    printf("%d", price);
    system("PAUSE");
    return (0);
}

I haven't done the picking part yet but the parts that I did do don't compile. I get this error for lines 31, 35, 39 and 43: warning C4047: '==' : 'char *' differs in levels of indirection from 'int' I also get this error for lines 14, 18 and 22: warning C4130: '==' : logical operation on address of string constant
And this: warning C4244: 'function' : conversion from 'time_t' to 'unsigned int', possible loss of data And this: warning C4100: 'argc' : unreferenced formal parameter I don't know how to fix them please help!




Aucun commentaire:

Enregistrer un commentaire