法典是两回事,但不是这样。
// 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