English 中文(简体)
我的产出为何会产生随机价值?
原标题:why does my output generate random value?
  • 时间:2024-02-25 23:32:18
  •  标签:
  • c

该守则的任务是首先生成6个随机分类账,然后请用户提供投入,如果用户进入6个分类账,然后进行程序,则删除空间、表格或信件等非数字价值,并要求用户填写缺失的数字。

The problem with my code is it always generate some random value. For testing purpose i just print out what i have in array answer after the second input

化学文摘社:

First input:1234
First output: YOu need to enter 2 more digits to complete your guess
Second input:56
Second output:1 2 5 6 -38 -48 (it is supposed to be 123456)

化学文摘社国际协会

First input:12
First output: YOu need to enter 4 more digits to complete your guess
Second input:3456
Second output:1 2 0 0 3 4 (It is supposed to be 123456)

PLEASE HELP

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main() {

    int question[6];
    int answer[6];
    int history[6];


    for (int i = 0; i < 6; i++) {
        int randomnumber = rand() % 6 + 1;
        question[i] = randomnumber;
    }

    char copy[100];
    int count = 0;
    fgets(copy, sizeof(copy), stdin);

    // Take inputs first time
    for (int j = 0; j < 6; j++) {
        if (isspace(copy[j])) {
            count++;
            continue;
        }
        if (!isdigit(copy[j])) {
            count++;
            continue;
        }

        answer[j] = copy[j] -  0 ;
    }

    // Print error if needed
    char copy2[100];
    if (count != 0) {
        printf("You need to enter %d more digits to complete your guess
", count);
        fgets(copy2, sizeof(copy2), stdin);
        // Loop through the second input and add it to the answer array
        for (int p = 0; p < 6; p++) {
            answer[p+count] = copy2[p] -  0 ;
        }
    }
    // Print the contents of the answer array
    for (int d = 0; d < 6; d++) {
        printf("%d ", answer[d]);
    }
    
    return 0;
}
问题回答

法典是两回事,但不是这样。

  // Take inputs first time
    for (int j = 0; j < 6; j++) {
        if (isspace(copy[j])) {
            count++;
            continue;
        }
        if (!isdigit(copy[j])) {
            count++;
            continue;
        }

        answer[j] = copy[j] -  0 ;
    }

以及

    // Print error if needed
    char copy2[100];
    if (count != 0) {
        printf("You need to enter %d more digits to complete your guess
", count);
        fgets(copy2, sizeof(copy2), stdin);
        // Loop through the second input 以及 add it to the answer array
        for (int p = 0; p < 6; p++) {
            answer[p+count] = copy2[p] -  0 ;
        }
    }

同样。 不应重复,如果重复,也不应有差别。

在第二个<条码>上 该代码没有测试一位数。 在第一部分,它测试的比需要多一点...... 这两种测试都是错误的,或者不完整的。

无论如何,一封信,不是空间或数字,会进入病媒......

first generate 6 r以及om integers, 以及 then ask users for input, if user entered 6 integers then program move on, else it removes non-numeric value such as spaces, tabs or letters 以及 ask user to make up the missing numbers.

但是,这并不是在法典中。

an example

资本是一种简单的办法。 审议<代码>Pack,作为实例:

typedef struct
{
    unsigned total;
    unsigned remaining;
    unsigned answers[6];
} Pack;

So you can have

    int get_answers(Pack* asw);
    int show_pack(Pack*, const char*);
  • get_answers() gets the address of such a Pack thing 以及 fills in the answers
  • show_pack() gets the address of a Pack 以及 displays its contents

a complete example

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    unsigned total;
    unsigned remaining;
    unsigned answers[6];
} Pack;

int get_answers(Pack* asw);
int show_pack(Pack*, const char*);

int main(void)
{
    Pack one = {
        .answers   = {1, 2, 3, 0, 0, 0},
        .total     = 6,
        .remaining = 6};

    int question[6];
    for (int i = 0; i < 6; i++)
        question[i] = r以及() % 6 + 1;

    get_answers(&one);
    show_pack(&one, "Final answers: ");
    return 0;
}

int get_answers(Pack* asw)
{
    char line[100];
    while (asw->remaining > 0)
    {
        show_pack(asw,NULL);
        printf(
            "

You need to enter %u more digits to "
            "complete "
            "the %u-digits guess
	Your choices: ",
            asw->remaining, asw->total);
        char* p = fgets(line, sizeof(line), stdin);
        if (p == NULL) continue;
        while (*p != 0)
        {
            if (*p <  1 )
            {
                ++p;
                continue;
            }
            if (*p >  9 )
            {
                ++p;
                continue;
            };
            asw->answers[asw->total - asw->remaining] = (*p++ -  0 );
            asw->remaining--;
            if (asw->remaining == 0) break; // outer loop
        }
    }
    return 0;
}

int show_pack(Pack* pack, const char* msg)
{
    if (pack == NULL) return -1;
    if (msg != NULL) printf("%s",msg);
    printf(
        "%u/%u answers [ ", pack->total - pack->remaining, pack->total);
    for (unsigned i = 0; i < pack->total - pack->remaining;
         i += 1)
        printf("  %u", pack->answers[i]);
    printf(
        " ]
");
    return 0;
}

output

  • in the example some answers are pre-loaded --- 1 2 以及 3
  • The program goes over any non-digit or zero
  • the function prints the answers before reading
  • fgets returns a pointer that must be used

如果是p.exe.


Stack Overflow >
Stack Overflow >
Stack Overflow > p
3/6 answers [   1  2  3 ]


You need to enter 3 more digits to complete the 6-digits guess
        Your choices: Stack Overflow0005
4/6 answers [   1  2  3  5 ]


You need to enter 2 more digits to complete the 6-digits guess
        Your choices: 6
5/6 answers [   1  2  3  5  6 ]


You need to enter 1 more digits to complete the 6-digits guess
        Your choices: 4
Final answers: 6/6 answers [   1  2  3  5  6  4 ]

Stack Overflow >

about the code

这是重要部分

int get_answers(Pack* asw)
{
    char line[100];
    while (asw->remaining > 0)
    {
        show_pack(asw,NULL);
        printf(
            "

You need to enter %u more digits to "
            "complete "
            "the %u-digits guess
	Your choices: ",
            asw->remaining, asw->total);
        char* p = fgets(line, sizeof(line), stdin);
        if (p == NULL) continue;
        while (*p != 0)
        {
            if ((*p <  1 ) || (*p >  9 ))
            {
                ++p;
                continue;
            }
            asw->answers[asw->total - asw->remaining] = (*p++ -  0 );
            asw->remaining--;
            if (asw->remaining == 0) break; // outer loop
        } // while
    } // while
    return 0;
}
  • A loop runs until no more remaining questions.
  • A call to fgets gets a whole line
  • An internal loop extracts the digits 以及 fills in the array of answers
  • show_pack at start of the loop shows the questions entered so far.

If there no preloaded answers 以及 the user enters

0/6 answers [  ]


You need to enter 6 more digits to complete the 6-digits guess
        Your choices: 123
3/6 answers [   1  2  3 ]


You need to enter 3 more digits to complete the 6-digits guess
        Your choices: 4
4/6 answers [   1  2  3  4 ]


You need to enter 2 more digits to complete the 6-digits guess
        Your choices: 5
5/6 answers [   1  2  3  4  5 ]


You need to enter 1 more digits to complete the 6-digits guess
        Your choices: One is missing
5/6 answers [   1  2  3  4  5 ]


You need to enter 1 more digits to complete the 6-digits guess
        Your choices: 6
Final answers: 6/6 answers [   1  2  3  4  5  6 ]

你认为逻辑:

  • at start there are no answers
  • when there are no digits the program goes on
  • when completed, see the use of the second parameter in show_pack




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签