0

我是Objective C的初学者,所以请不要说太复杂的词。我正在做一个删除某些字符(元音,辅音,点等)的程序,但是当我尝试让用户输入他们自己的语句时,编译器只是忽略我的fgets(语句),它忽略了我的 while 循环条件第二个时间。问题出在第 81-87 行(允许用户输入他们想要删除的字符)和第 98 行(while 循环输入语句。同样我不是专业人士,只知道一些基础知识。抱歉格式不正确。我不太了解stackoverflow

#import <Foundation/Foundation.h>
#define max 100


//characters is the character that the function is going to check for
NSCharacterSet *isChar(NSString * characters)
{
     NSCharacterSet * theChar=[NSCharacterSet characterSetWithCharactersInString:characters];
    return theChar;
}
NSString *removeChar(NSString * myInput , NSString * characters)
{

    NSString * name;
    name=[NSMutableString stringWithString:myInput];
    name = [[name componentsSeparatedByCharactersInSet: isChar(characters)] componentsJoinedByString: @""];



        return name;
}

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        char myString[max];
        char tempWord[max];
        NSInteger length =0;
        NSInteger otherlength=0;
        NSString * NewString;
        NSString * characters;
        NSString * myInput;        
        int k = 0;
        char l=('y');

        NSLog(@"Enter a String");
        fgets(myString, max, stdin);
        length=strlen(myString);
        myString [length - 1] = 0;


        myInput= [[NSString alloc] initWithUTF8String:myString];
        while (l=='y')
        {

        NSLog(@"What would you like to do?");
        NSLog(@"1:Remove Vowels");
        NSLog(@"2:Remove Punctuation");
        NSLog(@"3:Remove Constanants");
        NSLog(@"4:Remove Digits");
        NSLog(@"5:Remove whatever you want");
        scanf("%d",&k);
        if (k==1)
        {
            characters=@"aAeEiIoOuU";
        }
        if (k==2)
        {
            characters=@"!@#$%^&*()+_-|}]{[';:/?.>,<";
        }
        if(k==3)
           {
               characters=@"qQwWrRtTyYpPsSdDfFgGhHjJlLzZxXcCvVbBnNmM";
           }
        if (k==4)
        {
           characters= @"1234567890";
        }
        if (k==5)
        {//My problem starts here
                NSLog(@"Enter a String");
                fgets(tempWord, max, stdin);
                otherlength=strlen(tempWord);
                tempWord [length - 1] = 0;


                characters= [[NSString alloc] initWithUTF8String:tempWord];

        }



        NSLog(@"Your orignal string is: %@ ", myInput);
        NewString=removeChar(myInput,characters) ;
        NSLog(@"Your new string is: %@", NewString);

        NSLog(@"Do you want to continue?");
         //The scanf works the first time but when it goes thru the loop the second time it 
         //it gets ignored
        scanf("%c",&l);
        }
        NSLog(@"Take Care :)");
    }
    return 0;
}
4

1 回答 1

1

问题是scanf("%d",&k)它只读取数字,而不是 您在终端中键入的换行符。因此,下一个fgets()只读取这个换行符,没有别的。

一种可能的解决方法是使用fgets()而不是scanf(),因为这总是读取包括换行符终止符在内的整行,所以:

//scanf("%d",&k);
fgets(tempWord, sizeof(tempWord), stdin);
k = atoi(tempWord);

//scanf("%c",&l);
fgets(tempWord, sizeof(tempWord), stdin);
l = tempWord[0];

(请注意,如果根本没有读取任何内容,则fgets()返回,因此您应该在代码中检查该条件。)NULL

于 2013-09-12T18:49:12.613 回答