我如何使用 strcmp() 检查输入验证方法的字符匹配,使用 int 作为 Obj-C 中的 scanf() 输入?
how do i use strcmp() to check for a char match for an input validation method using an int as input with scanf() in Obj-C?
我正在尝试验证我在用 Obj-C 编写的程序中要求的输入。
这是我的 main() 函数中的代码:
#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;
}
这是我在 class 中实施的方法,但该程序无法运行:
//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;
}
}
这是我的输出结果:
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:
无论我输入什么我的 errorCheck() 方法 returns false.
我怀疑发生这种情况是因为我以某种方式没有正确地将输入转换为 char 并且它无法 strcmp() 我试图比较有效性的 char。
有人有什么建议吗?
谢谢!
这是有效的代码。这只是正确声明我的字符的问题:
//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;
}
}
我正在尝试验证我在用 Obj-C 编写的程序中要求的输入。
这是我的 main() 函数中的代码:
#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;
}
这是我在 class 中实施的方法,但该程序无法运行:
//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;
}
}
这是我的输出结果:
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:
无论我输入什么我的 errorCheck() 方法 returns false.
我怀疑发生这种情况是因为我以某种方式没有正确地将输入转换为 char 并且它无法 strcmp() 我试图比较有效性的 char。
有人有什么建议吗?
谢谢!
这是有效的代码。这只是正确声明我的字符的问题:
//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;
}
}