0 Replies - 16471 Views - Last Post: 14 February 2013 - 10:11 AM

#1 AwesomeProgrammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-February 13

User input comparison

Posted 14 February 2013 - 10:11 AM

Hi. I am starting out on objective C and I made this program:
 #import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    BOOL myBool = TRUE;
    
    while (myBool == TRUE) {
        int a, b, sum;
        char ans;
        
        NSLog(@"Welcome to my calculator. Type in the first number: ");
        scanf("%d", &a);
        
        check.integer <- function(a){
            !length(grep("[^[:digit:]]", format(a, scientific = FALSE)))
        }
        
        check.integer(a)
        
        NSLog(@"Type in the second number: ");
        scanf("%d", &B)/>;
        
        sum = a + b;
        NSLog(@"The answer is: %d", sum);
        
        NSLog(@"Do you want to put in another sum? (y/n)");
        scanf("%s", &ans);
        
        if (ans=='y'){
            myBool = TRUE;
        }
        
        else if (ans=='n'){
            myBool = FALSE;
        }
        while (myBool==FALSE) {
            return 0;
        }

    }
    return 0;
}



It works perfectly fine, however, I would like to be able to compare the input and if the input isn't an integer display a message that says 'Please type in an integer.'
I looked this up on some websites but I couldn't find the thing I needed. Could someone help?

View PostAwesomeProgrammer, on 14 February 2013 - 10:09 AM, said:

Hi. I am starting out on objective C and I made this program:
 #import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    BOOL myBool = TRUE;
    
    while (myBool == TRUE) {
        int a, b, sum;
        char ans;
        
        NSLog(@"Welcome to my calculator. Type in the first number: ");
        scanf("%d", &a);
        
        NSLog(@"Type in the second number: ");
        scanf("%d", &B)/>/>;
        
        sum = a + b;
        NSLog(@"The answer is: %d", sum);
        
        NSLog(@"Do you want to put in another sum? (y/n)");
        scanf("%s", &ans);
        
        if (ans=='y'){
            myBool = TRUE;
        }
        
        else if (ans=='n'){
            myBool = FALSE;
        }
        while (myBool==FALSE) {
            return 0;
        }

    }
    return 0;
}



It works perfectly fine, however, I would like to be able to compare the input and if the input isn't an integer display a message that says 'Please type in an integer.'
I looked this up on some websites but I couldn't find the thing I needed. Could someone help?


Is This A Good Question/Topic? 0
  • +

Page 1 of 1