Numbers adding together = bad

This is a discussion on Numbers adding together = bad within the C Programming forums, part of the General Programming Boards category; Hi guys, Still working on my calculator... now the problem I face is when you press the buttons... this is ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    7

    Numbers adding together = bad

    Hi guys,

    Still working on my calculator... now the problem I face is when you press the buttons...

    this is how it works...

    Code:
    switch (number)
    	{
    		case 0:
    			temp1 = temp1 + 1;
    			[totalField setFloatValue:temp1];
    			break;
    		default:
    			temp2 = temp2 + 1;
    			[totalField setFloatValue:temp2];
    			break;
    	}
    Ok, not sure if you all understand all of that, but basically that's for the button 1. When you press it, depending on what number of 2 in the calculation you'll be using you it adds it to a float, either temp1 or temp2. The problem is of course if you press "1, 2, 3" it doesn't make "123" it makes 6, cos it adds them up. How do you tell it to put them in sequence instead of adding up?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Either use strings in your combo box and convert to floats in calculations or just multiply your temp by 10 everytime you input a new number. The latter can make decimals problematic though.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    7
    i'm not sure how to do that... i'm really new to C

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    So new to C that you're still calling "Objective C using Cocoa" C. In the C language the code your showing would show no signs of using buttons of interfaces what so ever. Besides the fact, the second option I gave you was basic math. Surely you can do that.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    7
    no, no I can do the second thing, infact I did that as soon as I read what you posted. However I mean I don't know how to do the first option... Sorry for not showing it's on a button, I didn't think that was nescessary to show you...

    here's the complete code for it:

    Code:
    - (IBAction)button1:(id)sender
    {
    	switch (number)
    	{
    		case 0:
    			temp1 = temp1 * 10 + 1;
    			[totalField setFloatValue:temp1];
    			break;
    		default:
    			temp2 = temp2 * 10 + 1;
    			[totalField setFloatValue:temp2];
    			break;
    	}
    }
    That's when you press the button, as you can see I included the *10 into it.
    This is the code to show that totalField is a text field:

    Code:
        IBOutlet NSTextField *totalField;
    Also, it is C code, mostly... it uses different things to regular C code... It even compiles using GCC...

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    - (IBAction)button1:(id)sender
    /*...*/
    [totalField setFloatValue:temp1];
    That is not C. It's Objective C (I think) which GCC might compile.
    Also, it is C code, mostly...

    Code:
    temp2 = temp2 * 10 + 1;
    is the same as
    Code:
    temp2 = (temp2 * 10) + 1;
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding line numbers and concatenating a filename
    By durrty in forum C Programming
    Replies: 25
    Last Post: 06-28-2008, 03:36 AM
  2. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 04:03 PM
  3. adding large numbers
    By derek23 in forum C Programming
    Replies: 6
    Last Post: 07-22-2005, 08:02 PM
  4. Adding double numbers in a function
    By ybala in forum C Programming
    Replies: 1
    Last Post: 01-29-2003, 08:36 AM
  5. Shocking(kind of)
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 12-10-2002, 07:52 PM

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