Thread: Problems with scanf

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    3

    Problems with scanf

    Why is my code not working and why is the compiler throwing a "numberone undeclared" and a "numbertwo undeclared" message on the two scanf lines? I have definitely declared the variables!

    Code:
    #import <Foundation/Foundation.h>
    #import <stdio.h>
    
    
    @interface calculator : NSObject
    {
    int numberonetomultiply;
    int numbertwotomultiply;
    int numberone;
    int numbertwo;
    int result;
    }
    
    -(void) disp;
    -(void) setnumberone: (int) numberone;
    -(void) setnumbertwo: (int) numbertwo;
    -(void) runcalc;
    
    @end
    
    @implementation calculator
    
    -(void) disp
    {
    printf("\n%i multiplied by %i yields %i\n", numberonetomultiply, numbertwotomultiply, result);
    }
    
    -(void) setnumberone: (int) numberone
    {
    numberonetomultiply = numberone;
    }
    
    -(void) setnumbertwo: (int) numbertwo
    {
    numbertwotomultiply = numbertwo;
    }
    
    -(void) runcalc
    {
    result = (numberonetomultiply * numbertwotomultiply);
    }
    
    @end
    
    int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    NSLog (@"This program is a calculator that multiplies two numbers.");
    NSLog (@"Input first number,then press enter\n");
    
    
    
    calculator *humaninput = [calculator new];
    scanf ("%i", &numberone);
    printf("Now type the second number, then press enter\n");
    [humaninput setnumberone: numberone];
    scanf ("%i", &numbertwo);
    [humaninput setnumbertwo: numbertwo];
    [humaninput runcalc];
    printf("\nDisplaying answer...\n");
    [humaninput disp];
    
    [pool drain];
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Is is obj-C????

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What's this? Objective-C ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    3
    Yes, this is Obj-C!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Which is not C. I have moved this thread to the Tech Board, but you may be better off posting in a message board with a focus on Objective C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I don't actually know Objective C, and I have never attempted to read it before, so this might be thoroughly nonsense, but here goes.

    It seems like you've declared a class containing four variables:
    Code:
    int numberonetomultiply;
    int numbertwotomultiply;
    int numberone;
    int numbertwo;
    I'm pretty sure you only want the first two. Especially since you seem to be declaring a setnumberone which takes a variable called "numberone" as its parameter. You never use the numberone in the class and you have a name collision there.

    And you haven't declared numberone or numbertwo inside main() at all, just inside the class. Just to be clear, member variables exist once the class is instantiated, and a copy of the variables exists for each instance of the class. I think you want to declare numberone and numbertwo inside main(), or (gasp) as global variables. Hence my suggested modifications are:
    Code:
    #import <Foundation/Foundation.h>
    #import <stdio.h>
    
    
    @interface calculator : NSObject
    {
    int numberonetomultiply;
    int numbertwotomultiply;
    int result;
    }
    
    -(void) disp;
    -(void) setnumberone: (int) numberone;
    -(void) setnumbertwo: (int) numbertwo;
    -(void) runcalc;
    
    @end
    
    @implementation calculator
    
    -(void) disp
    {
    printf("\n%i multiplied by %i yields %i\n", numberonetomultiply, numbertwotomultiply, result);
    }
    
    -(void) setnumberone: (int) numberone
    {
    numberonetomultiply = numberone;
    }
    
    -(void) setnumbertwo: (int) numbertwo
    {
    numbertwotomultiply = numbertwo;
    }
    
    -(void) runcalc
    {
    result = (numberonetomultiply * numbertwotomultiply);
    }
    
    @end
    
    int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int numberone;
    int numbertwo;
    
    NSLog (@"This program is a calculator that multiplies two numbers.");
    NSLog (@"Input first number,then press enter\n");
    
    
    
    calculator *humaninput = [calculator new];
    scanf ("%i", &numberone);
    printf("Now type the second number, then press enter\n");
    [humaninput setnumberone: numberone];
    scanf ("%i", &numbertwo);
    [humaninput setnumbertwo: numbertwo];
    [humaninput runcalc];
    printf("\nDisplaying answer...\n");
    [humaninput disp];
    
    [pool drain];
    return 0;
    }
    Let me know if it compiles . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I might be able to help you. Numberone and numbertwo are instance variables in your calculator class, you can not use them in main. Each {} bracket in C signifies a block where variables are local, outside the block you can not touch them or refer to them by name. That's why you need the setnumber method you got there as well for example. It's just like variables inside functions in C, you need a new variable in main, use that in scanf then in after that in your calculator method.
    Last edited by Subsonics; 07-08-2010 at 04:51 PM.

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    3
    Thank you guys so much!! That worked!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with Calculating
    By danepporambo in forum C Programming
    Replies: 6
    Last Post: 04-25-2006, 01:04 AM
  2. First scanf() skips next scanf() !
    By grahampatten in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:47 AM
  3. Replies: 2
    Last Post: 11-10-2003, 09:12 PM
  4. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM

Tags for this Thread