2016-08-31 2 views
0

J'essaie de valider l'entrée que je demande dans un programme que j'écris en Obj-C.Comment puis-je utiliser strcmp() pour rechercher une correspondance char pour une méthode de validation d'entrée en utilisant un int comme entrée avec scanf() dans Obj-C?

c'est le code dans ma fonction principale():

#import <Foundation/Foundation.h> 
#import "PointOfSale.h" 

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

     PointOfSale *myMovieTix = [[PointOfSale alloc] init]; 
     char l; 
     int counter; 

     do{ 
      printf("Welcome to the Regal Entertainment Group Movie  
        Ticket System.\n \n"); 
      printf("Please select your language settings from one of 
        the following options: \n \n"); 
      printf("\t 1. English \n"); 
      printf("\t 2. Spanish \n"); 
      printf("\t 3. Portuguese \n \n"); 

      printf("Enter your selection here: "); 

      scanf("%c", &l); 

      if(counter == 6) 
      { 
       l = 1; 
       break; 
      } 
      else 
      { 
       counter++; 
      } 
     } while (![myMovieTix errorCheck: &l]); 



    } 
    return 0; 
} 

c'est la méthode dans la classe que j'ai mis en œuvre où le programme ne fonctionne pas:

//this method checks for input errors dont know if will use in main or 
//in implementation file 
- (bool) errorCheck: (char *) c 
{ 
    //debug statement 
    //printf("inside errorCheck \n"); 

    //char "array" to use with strcmp() 
    char e[1]; 
    char s[2]; 
    char p[3]; 

    //using strcmp() to test input against entry 
    if(strcmp(c,e) != 0 || strcmp(c,s) != 0 || strcmp(c,p) != 0) 
    { 
     //return false if input is anything but a match 
     printf("error is returning false because input doesnt match"); 
     return false; 
    } 
    else 
    { 
     //return true if input matches a possible entry 
     printf("error is returning true because input matches"); 
     return true; 
    } 
} 

c'est ce que mon la sortie ressemble à:

Welcome to the Regal Entertainment Group Movie Ticket System. 

Please select your language settings from one of the following options: 

    1. English 
    2. Spanish 
    3. Portuguese 

Enter your selection here: 1 
error is returning false because input doesnt matchWelcome to the Regal   
Entertainment Group Movie Ticket System. 

Please select your language settings from one of the following options: 

    1. English 
    2. Spanish 
    3. Portuguese 

Enter your selection here: error is returning false because input 
doesnt matchWelcome to the Regal Entertainment Group Movie Ticket 
System. 

Please select your language settings from one of the following options: 

    1. English 
    2. Spanish 
    3. Portuguese 

Enter your selection here: 

peu importe ce que j'entre ma méthode errorCheck() renvoie false. J'ai un soupçon que cela se passe parce que je ne suis pas en train de lancer l'entrée comme char et que je ne peux pas strcmp() le caractère (s) que j'essaie de comparer pour la validité.

Quelqu'un a-t-il des suggestions?

merci!

Répondre

0

Voici le code qui a fonctionné. il s'agissait simplement de déclarer correctement mon/mes caractère (s):

//this method checks for input errors in the language selection menu 
- (bool) errorCheck: (char) c 
{ 
    //debug statement 
    //printf("inside errorCheck \n"); 

    //char "array" to use with strcmp() 
    char e = '1'; 
    char s = '2'; 
    char p = '3'; 

    //printf("c deref: %c \n", c);//debug statement 

    //printf("comparison: %c = %c \n", c, s);//spanish char debug 
    //statement 

    //using strcmp() to test input against entry 
    if(c == e || c == s || c == p) 
    { 
     //return true if input matches a possible entry 
     //printf("error is returning true because input matches \n 
     //\n"); //debug statement 
     return true; 
    } 
    else 
    { 
     //return false if input is anything but a match 
     //printf("error is returning false because input doesnt match 
     //\n \n");//debug statement 
     return false; 
    } 
}