0

----我有两个 NSString 实例,其中一个是在 while 循环中定义的,而另一个是在那之后。Xcode 似乎认为,由于第一个实例(我们将调用 string1)处于 while 循环中,因此不会定义它。但是,为了让程序退出 while 循环,它总是会定义 STRING1。一个 NSString 在另一个 while 循环中也是一样的。

----在两个while循环之外,最后,在代码中我对它们都做了一个NSString方法(isEqualtoString),但是Xcode告诉我没有定义string1和string 2。该程序应该可以工作,但编译器阻止了我。有什么我可以改变的,让 string1 和 string2 出现在 Xcode 的眼中。

----我将它用于注册页面,我需要在 while 循环中使用它们,因为它们需要循环直到用户通过控制台输入符合我要求的用户名。

编辑:在实际代码中添加。

int numb1, numb2;

char usercheck1[60];
char usercheck2[60];
//Registration
    numb2 = 1;
    while (numb2 == 1){
        numb1 = 1;
        while (numb1 == 1){
            numb1 = 0;
            NSLog(@"Username:");
            fgets(usercheck1, sizeof usercheck1, stdin);
            int c2;
            while((c2 = getchar()) != '\n' && c2 != EOF);
            if (usercheck1 [sizeof (usercheck1)-1] == '\n'){ // In case that the input string has 12 characters plus '\n'
               usercheck1 [sizeof (usercheck1)-1] = '\0';} // Plus '\0', the '\n' isn't added and the if condition is false.
            NSString* string1 = [NSString stringWithUTF8String: usercheck1];
            //Makes sure string contains no spaces and string length is correct size.
            if ([string1 length] > 12){
               NSLog (@"Username must be 12 characters or less!");
               numb1 = 1;}
            if ([string1 length] < 5){
               NSLog (@"Username must be 4 characters or more!");
               numb1 = 1;}
            if ([string1 rangeOfString:@" " ].location != NSNotFound){
               NSLog(@"Username cannot contain spaces!");
               numb1 = 1;}
      }
        numb1 = 1;
        while (numb1 == 1){
            numb1 = 0;
            NSLog(@"Confirm Username:");
            fgets(usercheck2, sizeof usercheck2, stdin);
            int c2;
            while((c2 = getchar()) != '\n' && c2 != EOF);
            if (usercheck2 [sizeof (usercheck2)-1] == '\n'){ // In case that the input string has 12 characters plus '\n'
            usercheck2 [sizeof (usercheck2)-1] = '\0';} // Plus '\0', the '\n' isn't added and the if condition is false.
            NSString* string2 = [NSString stringWithUTF8String: usercheck2];
            //Makes sure string contains no spaces and string length is correct size.
            if ([string2 length] > 12){
               NSLog (@"Username must be 12 characters or less!");
               numb1 = 1;}
            if ([string2 length] < 5){
               NSLog (@"Username must be 4 characters or more!");
               numb1 = 1;}
            if ([string2 rangeOfString:@" " ].location != NSNotFound){
               NSLog(@"Username cannot contain spaces!");
               numb1 = 1;}
        }
        if ([string2 isEqualToString: string1] == YES){
            NSLog(@"Usernames confirmed! Username:%s", string2);
            numb2 = 0;}
        else {NSLog(@"Usernames do not match. Try again");
            numb2 = 1;}
    }

}

如您所见,如果它实际编译并运行它会起作用,但编译器只是不喜欢我在 isEqualToString 的 if 语句中使用 string2。它给了我错误:“使用未声明的标识符'string2'”另外,将该语句和else语句移到两个子while语句之外,它给了我string1和string2的错误。

XCode 版本是 4.6.3,我正在为 10.8.4 上的 Mac OS X 编程

4

1 回答 1

1

您不能访问声明它们的范围之外的变量。由于string1andstring2是在两个while块中声明的,因此您不能在while块之外使用它们。

这段代码有很多可以改进的地方。尝试这样的事情:

NSString *username1;
NSString *username2;
while (1) {
    while (1) {
        NSLog(@"Username:");
        char usercheck[60];
        fgets(usercheck, sizeof usercheck1, stdin);

        int c2;
        while ((c2 = getchar()) != '\n' && c2 != EOF);
        if (usercheck [sizeof (usercheck) - 1] == '\n') { // In case that the input string has 12 characters plus '\n'
           usercheck[sizeof (usercheck)-1] = '\0';
        } // Plus '\0', the '\n' isn't added and the if condition is false.

        NSString *string1 = [NSString stringWithUTF8String:usercheck];
        // Makes sure string contains no spaces and string length is correct size.
        if ([string1 length] > 12) {
           NSLog(@"Username must be 12 characters or less!");
        } else if ([string1 length] < 5) {
           NSLog(@"Username must be 4 characters or more!");
        } else if ([string1 rangeOfString:@" "].location != NSNotFound) {
           NSLog(@"Username cannot contain spaces!");
        } else {
           username1 = string1;
           break; // username is good
        }
    }

    while (1) {
        NSLog(@"Confirm Username:");
        char usercheck[60];
        fgets(usercheck, sizeof usercheck, stdin);

        int c2;
        while ((c2 = getchar()) != '\n' && c2 != EOF);
        if (usercheck[sizeof (usercheck) - 1] == '\n') { // In case that the input string has 12 characters plus '\n'
            usercheck[sizeof (usercheck) - 1] = '\0';
        } // Plus '\0', the '\n' isn't added and the if condition is false.

        NSString *string2 = [NSString stringWithUTF8String:usercheck];
        //Makes sure string contains no spaces and string length is correct size.
        if ([string2 length] > 12) {
           NSLog (@"Username must be 12 characters or less!");
        } else if ([string2 length] < 5) {
           NSLog (@"Username must be 4 characters or more!");
        } else if ([string2 rangeOfString:@" "].location != NSNotFound) {
           NSLog(@"Username cannot contain spaces!");
        } else {
           username2 = string2;
           break;
        }
    }

    if ([username1 isEqualToString:username2]) {
        NSLog(@"Usernames confirmed! Username:%@", username1);
        break;
    } else {
        NSLog(@"Usernames do not match. Try again");
    }
}
于 2013-07-22T20:41:34.263 回答