我正在研究我们的班级中的c语言,我创建了一些代码来查找其他数组内的一些数字,但是当我使用char值尝试它时,我的代码无法与使用int值一起工作。
这是我的代码:
#include<stdio.h>
int main () {
int num, i, j;
char a[99], ele;
printf("Enter the Character element:");
// get the length of array by the num value
scanf("%d", &num);
printf("Enter the values: \n");
// a loop for getting values for our a[array] line by line
for ( i = 0 ; i < num ; i++ ) {
// get value by index i for array a index by index
//printf("%d\t", (i+1));
//if ( i + 1 == num ) {
// scanf("%c", &a[i]);
//} else {
scanf("%c", &a[i]);
//}
}
printf("Enter the Character elements to be searched:");
// get the value for ele, to use ele for searching inside our a[array]
scanf("%c", &ele);
// we need to set i to 0 for our while loop
j = 0;
// use the while loop to
while ( j < num && ele != a[j]) {
j++;
}
if ( j < num ) {
printf("Character found at the location = %d\n\n\n", j + 1);
} else {
printf("Character Not Found!\n\n\n");
}
return 0;
}
我尝试修复很多次,但是每次遇到错误时,上面的代码就可以正常工作,但是在输入过程中会遇到一些输入值。
答案 0 :(得分:2)
感谢WhozCraig,user3121023的建议,scanf(" %c", &a[i]);
和scanf(" %c", &ele);
中的空格会引起一些不必要的输入,因此这是我的代码,它的工作原理很吸引人。 :))
我只是在%c
前添加空格,一切正常。
#include<stdio.h>
int main () {
int num, i, j;
char a[99], ele;
printf("Enter the Character element:");
// get the length of array by the num value
scanf("%d", &num);
printf("Enter the values: \n");
// a loop for getting values for our a[array] line by line
for ( i = 0 ; i < num ; i++ ) {
// get value by index i for array a index by index
printf("%d\t", (i+1));
scanf(" %c", &a[i]);
}
printf("Enter the Character elements to be searched:");
// get the value for ele, to use ele for searching inside our a[array]
scanf(" %c", &ele);
// we need to set i to 0 for our while loop
j = 0;
// use the while loop to
while ( j < num && ele != a[j]) {
j++;
}
if ( j < num ) {
printf("Character found at the location = %d\n\n\n", j + 1);
} else {
printf("Character Not Found!\n\n\n");
}
return 0;
}
答案 1 :(得分:0)
您的程序中存在一个导致意外行为的问题:
scanf("%d", &num)
从标准输入中读取一个数字,但保留用户键入的换行符作为下一个要读取的字符。scanf("%c", &a[i]);
的进一步调用从用户读取了此待定换行符和另外num-1
个字符,而其余的输入待定... scanf("%c", &ele);
读取stdin中待处理的任何字节,无论是来自用户的字符还是待处理的换行符。因此,程序的行为就好像它没有执行最后一个scanf()
。
您应该使用fgets()
每次从用户一行读取输入,或使用循环刷新挂起的换行符:
#include <stdio.h>
int main() {
int num, i, c;
char a[99], ele;
printf("Enter the number of character:");
// get the length of array by the num value
if (scanf("%d", &num) != 1 || num < 0 || num > 99)
return 1;
// consume the rest of the line typed by the user
while ((c = getchar()) != EOF && c!= '\n')
continue;
printf("Enter the characters on a single line: \n");
// a loop for getting values for our a[array]
for (i = 0; i < num; i++) {
if (scanf("%c", &a[i]) != 1)
return 1;
}
// consume the rest of the line typed by the user
while ((c = getchar()) != EOF && c!= '\n')
continue;
printf("Enter the character to be searched: ");
// get the value for ele, to use ele for searching inside our a[array]
if (scanf("%c", &ele) != 1)
return 1;
// consume the rest of the line typed by the user
while ((c = getchar()) != EOF && c!= '\n')
continue;
// use a for loop
for (i = 0; i < num && ele != a[i]; i++) {
i++;
}
if (i < num) {
printf("Character %c found at the offset %d\n\n\n", ele, i);
} else {
printf("Character %c Not Found!\n\n\n", ele);
}
return 0;
}