英文:
Despite adding a break statement, if condition still moves on and prints out incorrect error message
问题
问题出在用户输入例如 "123" 时,应该打印需要输入 3 个更多的数字,但它总是打印 "A character in your guess was not a digit or a whitespace" 错误。原因是在您的代码中有一个逻辑错误。请修改以下部分:
for (int i = 0; i < 6; i++) {
if (isdigit(input[i] == 1)){ // 这一行有问题
numCount++;
if (numCount < maxLength && numCount != 0){
enoughDigits = 1;
}
else{
printf("You need to enter %d digits to complete your guess\n", remainder);
}
error = 1;
}
}
将上述代码块更改为以下内容:
for (int i = 0; i < 6; i++) {
if (isdigit(input[i])) { // 移除 == 1,只检查是否是数字
numCount++;
}
}
if (numCount < maxLength && numCount != 0) {
enoughDigits = 1;
} else {
printf("You need to enter %d digits to complete your guess\n", remainder);
error = 1;
}
这样就可以正确地检查是否输入了足够的数字,并且只在需要时打印错误消息。
英文:
So for an assignment I am supposed to check the input from a user and determine whether:
-
if there are characters in the input: I print an error message saying it is an invalid input and ask for input again until it is valid
-
if there are less than 6 digits in the input: I print an error message that there are x many digits remaining and ask for input again until it is valid
int main() {
int solutionArray[6];
int guessArray[6];
int indexArray[6];
/*Creating the random numbers and asking for seed*/
int seed;
printf("Enter the integer value of the seed for the game: ");
scanf("%d", &seed);
srand(seed);
/*Adding the randomized integers into solutionArray*/
for(int i = 0; i < 6; i++){
solutionArray[i] = rand() % 10;
printf("%3d", solutionArray[i]);
}
printf("\n");
/*Game interface*/
printf("For each turn enter 6 digits 0 <= digit <= 9 \n");
printf("Spaces or tabs in your response will be ignored. \n");
/*Validating conditions of the user input*/
char input[50];
int error = 0;
int enoughDigits = 0;
do {
printf("Enter your guess, 6 digits: ");
scanf("%s", input);
int numCount = 0;
int remainder = 0;
int maxLength = 6;
// Checking to see if there are any characters other than digits and prints error
for (int i = 0; i < 6; i++) {
if (!isdigit(input[i])) {
printf("ERROR: A character in your guess was not a digit or a white space\n");
printf("\n");
error = 1;
break;
}
}
/*Checking to see if enough digits have been inputted*/
remainder = 6 - numCount;
for (int i = 0; i < 6; i++){
if (isdigit(input[i] == 1)){
numCount++;
if (numCount < maxLength && numCount != 0){
enoughDigits = 1;
}
else{
printf("You need to enter %d digits to complete your guess \n", remainder);
}
error = 1;
}
}
// Print the guess if there are no errors
if (error == 0) {
printf("Your guess was:\n");
for (int i = 0; i < 6; i++) {
guessArray[i] = input[i] - '0';
printf("%2d", guessArray[i]);
}
printf("\n");
}
// Printing results of the guesses
int partialMatches = 0;
int perfectMatches = 0;
for (int i = 0; i < 6; i++) {
partialMatches = 0;
for (int j = 0; j < 6; j++) {
if (solutionArray[i] == guessArray[j]) {
partialMatches++;
break; // Exit inner loop after finding a partial match
}
}
if (solutionArray[i] == guessArray[i]) {
perfectMatches++;
partialMatches--;
}
}
if (perfectMatches == 6){
printf("YOU DID IT! ! \n");
error = 0;
break;
}
printf("%d matches, %d partial matches\n",perfectMatches, partialMatches);
error = 1;
} while (error == 1 || enoughDigits == 1);
return 0;
}
My issue comes in when the user inputs for example "123" it should print that I need to enter 3 more digits but it only prints "A character in your guess was not a digit or a whitespace". For some reason it prints that error regardless of the input.
Attached is my code below. Thanks in advance!
答案1
得分: 1
至少有这些问题
缓冲区溢出
scanf("%s", input)
不能防止太多字符保存到 input[]
中。使用一个 宽度。由于 input
的大小为50,使用宽度为49。
scanf("%49s", input)
错误的测试
isdigit(input[i] == 1)
显然应该是 isdigit(input[i])
,以测试 input[i]
是否为数字。
英文:
At least these problems
Buffer overflow
scanf("%s", input)
does not prevent too many characters in saving to input[]
. Use a width. Since input
is size 50, use a width of 49.
scanf("%49s", input)
Wrong test
isdigit(input[i] == 1)
certainly should be isdigit(input[i])
to test if input[i]
is a digit.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论