无法在C中使用getchar()和指针表示法获取用户输入

时间:2012-11-06 08:09:37

标签: c pointers notation getchar

我正在将程序从正常数组表示法更改为纯指针表示法,并且我无法在while循环中使用getchar()接收用户输入。我打印出来的是程序正在接收并输出颠倒的问号。我不确定为什么会这样,因为我从未改变变量类型。问题在于接收用户输入的第二个功能。感谢您的帮助。

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

/* Function prototypes */
void fillS1(char * x);

void fillS2(char * x, char * y, char z);

void strFilter(char * a, char * b, char c);

int main(int argc, const char * argv[])
  {
    char s1[42];
    char s2[22];
    char x = 0;

    fillS2(s2, s1, x);

    return 0;
}

/* Function to generate a random string of 40 uppercase letters */
void fillS1(char randomlyGeneratedString[])
{
char * pointerToRandom = randomlyGeneratedString;
int i;

for (i = 0; i < 40; i++) {
    *(pointerToRandom + i) = 'A' + rand() % 26;
}
pointerToRandom[40] = (char)0;

//printf("This is the generated string %s\n", pointerToRandom);

}

/* Function to get user input of characters */
void fillS2(char userString[], char randomString[], char replacementCharacter)
{

char * pointerToUserString = userString;
char * pointerToRandom = randomString;


int i = 0;
int n = 0;
int capitalLetterCheck = 0;

char loopContinue = 0;
char copyString[42];

char * pointerToCopyString = copyString; 

fillS1(pointerToRandom);

do {

    /* For loop to copy the first randomly generated string */
    for(i = 0; i < 42; i++)
        *(pointerToCopyString + i) = *(pointerToRandom + i);

    i = 0;
    capitalLetterCheck = 0;

    /* While loop to to get user input */
    printf("Please enter at least 2 capital letters and a maximum of 20.\n");
    while  (((*(pointerToUserString + i)) = getchar() != '\n'))  {

        /* Counter to determine how many characters were entered */
        i++;

    }

    i++;

    *(pointerToUserString + i) = '\0';

    //printf("This is the value if i %i", i);
    //printf("This is the user's string %s", pointerToUserString);

    /* Capital letter check */
    for (n = 0; n < 20; n++) {
        if (((*(pointerToUserString + i)) >= 'A') && (*(pointerToUserString + i) <= 'Z'))                  
   {          
            capitalLetterCheck++;
        }
    }

    if (i < 3) {
        printf("You need at least two letters\n");
    }

    else if (i > 21){
        printf("You cannot have more than twenty letters\n");
    }

    else if (capitalLetterCheck >= 2) {

        puts(pointerToUserString);

        printf("Enter a character to replace occuring letters.\n");
        scanf("%c", &replacementCharacter);
        getchar();

        strFilter(pointerToCopyString, pointerToUserString, replacementCharacter);

    }

    else

        printf("You  must have 2 capital letters.\n");


    printf("Would you like to enter another string (y/n)?\n");
    loopContinue = getchar();
    getchar();


} while (loopContinue != 'n' && loopContinue != 'N');

}

/* Function to replace letters with the character chosen by the user */
void strFilter(char a[], char b[], char c)
{
char * pointerToA = a;
char * pointerToB = b;


int i = 0;
int n = 0;

while (n < 20) {

    for (i = 0; i < 40; i++) {
        if ((*(pointerToA + i)) == *(pointerToB + n)){
            *(pointerToA + i) = c;
        }
    }
    i = 0;
    n++;
}

puts(a);
}

1 个答案:

答案 0 :(得分:5)

问题出现在if声明中:

(*(pointerToUserString + i)) = getchar() != '\n'

分配是一个表达式,低于优先于比较。这意味着以上内容与:

相同
(*(pointerToUserString + i)) = (getchar() != '\n')

因此,为(*(pointerToUserString + i))分配了表达式getchar() != '\n'的值,这不是您想要的。