Problems with scanf

This is a discussion on Problems with scanf within the Tech Board forums, part of the Community Boards category; Why is my code not working and why is the compiler throwing a "numberone undeclared" and a "numbertwo undeclared" message ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    21,749
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,046
    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, 08: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21