从数组生成随机字符串

时间:2018-01-05 18:54:04

标签: c arrays

我正在编写一个代码,要求用户输入当前名称及其价格,然后将此数据存储在两个数组PresentArray_name和PresentArray_price中。 PresentArray_name是随机生成的,并且会显示它的价格。我可以打印随机现有名称,但我不能打印礼品价格。我应该使用二维数组,如果是这样,我该怎么做?代码如下:

void add_presents()
{
    int i=0;
    char PresentArray_name[10][30];//A 2D array to store names of presents, 
which can only be 30 characters long
    int PresentArray_price[10];
    printf("This area is a little difficult to navigate as the answer for 
the question is stored before the question is displayed! Simply type in the 
toy hit the enter key, and then input it's price then hit the enter key! 
Don't pay attention to the headings!\n");
    for (i=0;i<10;i++)
    {
        printf("Enter present %d:\n", i+1);
        scanf("%s", &PresentArray_name[i]);//Stores the presents in the 
PresentArray_name
        printf("Enter price for present %d:\n", i+1);
        scanf("%d", &PresentArray_price[i]);//Stores the presents in the 
PresentArray_price
        if (PresentArray_price[i]<5||PresentArray_price[i]>15)
        {
            printf("Invalid! Enter a price between 5 and 15:\n");
            scanf("%d", &PresentArray_price[i]);
        }

    }
    for (i=0;i<10;i++)
    {
        printf("Present %s costs %d\n", PresentArray_name[i], 
PresentArray_price[i]);//Prints the names and the costs of each present
    }
    srand(time(NULL));
    time_t t;
    srand((unsigned)(&t));
    for (i=0;i<total_number_of_presents_required;i++)//Loop counter form 
another part of the code
    {
        printf("Kid: %d gets %s, which costs: %d\n",i+1, 
PresentArray_name[rand()%10], PresentArray_price[i]);
    }
}

2 个答案:

答案 0 :(得分:1)

您可以保存rand函数的返回值,这样就可以将它与名称和价格一起使用。

void add_presents()
{
    int PresentArray_price[10];
    char PresentArray_name[10][30];

    printf("This area...\n");

    for(int i = 0; i < 10; ++i)
    {
        printf("Enter present %d:\n", i + 1);
        scanf("%s", &PresentArray_name[i]);

        printf("Enter price for present %d:\n", i + 1);
        scanf("%d", &PresentArray_price[i]);

        if (PresentArray_price[i] < 5 || PresentArray_price[i] > 15)
        {
            printf("Invalid! Enter a price between 5 and 15:\n");
            scanf("%d", &PresentArray_price[i]);
        }
    }

    for(int i = 0; i < 10; ++i)
        printf("Present %s costs %d\n", PresentArray_name[i], PresentArray_price[i]);

    srand(time(NULL));

    for(int i = 0; i < total_number_of_presents_required; ++i)
    {
        int n = rand() % 10;
        printf("Kid: %d gets %s, which costs: %d\n", i + 1, PresentArray_name[n], PresentArray_price[n]);
    }
}

答案 1 :(得分:0)

如果您了解#include <stdio.h> #include <time.h> struct Present { char name[30]; // note this will only hold a name 29 long to ensure room for NUL terminator int price; }; int main(void) { srand(time(NULL)); // now, you can simply create an array of this struct, and the name // and price will always be together struct Present presents[10]; int i; for (i=0; i<10; i++) { // get user input. You should also check the return value of scanf which I'm omitting here for brevity // get the present name scanf("%s", presents[i].name); // get the price scanf("%d", &(presents[i].price)); } // now when you randomly select a present, you'll get it's name and price i = rand()%10; printf("present name: %s\n", presents[i].name); printf("present price: %d\n", presents[i].price); return 0; } ,我建议使用这些,那么您只有1个数据结构来跟踪:

struct Present

为每个数据字段使用单独的原始数组并不能很好地扩展。如果您还想跟踪当前制造商的名称怎么办?在哪里买的?它被买的日期?您只需将这些字段添加到presents,您仍然只需要在整个代码中处理{{1}},而不是使用不同的数组(和数组名称)。