我正在开发一个处理字母集的 C 程序。但是,我遇到了一个问题。出于某种原因,该代码一次打印消息Enter next element of Set A
两次,因此它不要求输入两者的第一行。
例如:
If the size of Set A is 5, the program will print:
Enter first element of Set A:(user inputs f)
Enter next element of Set A:(doesn't allow user to input value) <--printed at same time with line below
Enter next element of Set A:(allows a user input)
Enter next element of Set A:(doesn't allow user to input value) <--printed at same time with line below
Enter next element of Set A:(allows a user input)
这是代码:
void getSetA(bool setA[], int sizeA)
{
letters element, letter;
int position = 0, num;
printf("\nEnter the first element in Set A: ");
element = getchar();
for(letter = a; letter <= z; letter++)
{
if(element == letter) setA[position + 1] = true;
else position++;
}
for(num = 1; num <= sizeA -1; num++)
{
printf("\nEnter next element of Set A: ");
element = getchar();
for(letter = a; letter <= z; letter++)
{
if(element == letter) setA[position + 1] = true;
else position++;
}
}
}
请注意,我将字母类型定义为字母表中的所有字母,而 setA 是布尔类型的数组。sizeA 是用户之前输入的 Set A 的大小。
先感谢您。