I'm a beginner in Objective C, so no high complicated words please.
I'm doing a program that removes certain characters (vowels, consonants, punct etc.) but when I tried for the user to input their own statement, the compiler just ignore my fgets
(statement), and it ignores my while loop condition the second time. The problem is on line 81-87(allows the user to input what characters they want removed) and line 98(the while loop input statement. Again I'm not a pro, just know some basics. Sorry for the improper formatting. I don't know stackoverflow that well
#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;
}
The problem is that scanf("%d",&k)
reads only the number, but not the newline character
that you typed in the terminal. Therefore the next fgets()
reads just this newline and nothing else.
One possible fix is to use fgets()
instead of scanf()
, because this always reads an
entire line including the newline terminator, so:
//scanf("%d",&k);
fgets(tempWord, sizeof(tempWord), stdin);
k = atoi(tempWord);
and
//scanf("%c",&l);
fgets(tempWord, sizeof(tempWord), stdin);
l = tempWord[0];
(Note that fgets()
returns NULL
if nothing was read at all, so you should check
for that condition in your code.)