I am trying to validate the input i am asking for in a program i am writing in Obj-C.
this is the code in my main() function:
#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;
}
this is the method in the class i have implemented where the program is not working:
//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;
}
}
this is what my output looks like:
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:
no matter what i enter my errorCheck() method returns false.
i have a suspicion that this is happening because i am somehow not properly casting the input as a char and it is not able to strcmp() the char(s) i am trying to compare for validity.
anybody got any suggestions?
thanks!
here is the code that worked. it was simply a matter of declaring my char(s) correctly:
//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;
}
}