using double

This is a discussion on using double within the C Programming forums, part of the General Programming Boards category; ok so the program i have to right for class has me defining a constant call it A well later ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    16

    using double

    ok so the program i have to right for class has me defining a constant call it A

    well later in the program i want to call it as a double... ive tried looking all over and when i do something like


    a = (double(b/A)*11);


    something along those lines it gives me a syntax error....

    any hints or help would be great thanks

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by surfingbum18 View Post
    a = (double(b/A)*11);
    That's a C++-style cast. You need:

    Code:
    a = (double)(b) / A * 11.0;
    Notice that I didn't put (b/A) inside the cast. If b and A are both integers, then (b/A) is divided as an integer, even though you are casting the result. That would not be correct.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    16
    Quote Originally Posted by brewbuck View Post
    That's a C++-style cast. You need:

    Code:
    a = (double)(b) / A * 11.0;
    Notice that I didn't put (b/A) inside the cast. If b and A are both integers, then (b/A) is divided as an integer, even though you are casting the result. That would not be correct.

    what if instead of dividing im trying to do a mod would it be

    a = (double)((b)%A)*11;

    keeping that in mind A is the defined constant at the beginning of my program no matter how i move it around it still comes out wrong when i run the program it returns 11... thanks

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by surfingbum18 View Post
    what if instead of dividing im trying to do a mod would it be

    a = (double)((b)%A)*11;
    That doesn't make sense though. Modulus is an integer-only operation. You are multiplying by an integer. So I do not see why you are casting it to double.

    If all you are trying to do is convert the entire expression to double, just cast the whole thing:

    Code:
    a = (double)(b % A * 11);

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    16
    Code:
    //Cost to buy chips.
    #define CHIP_BUYING 11
    
    //This is for the buying chips
    int buychips(int *cash);
    
    int main (){
    
        if (choice == 1){
        
        numchips += buychips(&cash);               
        cash += (double)((cash%CHIP_BUYING) * 11);
    }
    
    return 0;
    }
    
    int buychips(int *cash){
      int buy_cash, numchips;
     
      printf("How much cash do you want to spend for chips?\n");
      scanf("%d", &buy_cash);  
        printf("\n");
        
        
        //Nullify transaction if the buyer wants to spend more money than allowed.
     
      if( buy_cash > *cash)
       
         printf("Sorry, you do not have that much money. No chips bought.\n\n");
     
      //Execute transaction.
     
      else{
     
         numchips = (buy_cash / CHIP_BUYING);
    
         *cash -= buy_cash;
    
     }
    return numchips;
    }

    okay i just extracted the part of the code that matter because i really dont feel like having the entire code on the interent due to the fact that its homework at a university...


    but what im have issues with is say you buy 500$ worth of chips so when you divide 500 by the 11 you get 45 chips and should have $5 left over from the transaction that should be thrown back into your original amount of money... the original amount of money is cash so how do i get the left over money to get back to cash... the if statement in main is where im having my issues with cash

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
         *cash -= buy_cash;
    Why not make it simple, and here instead do:
    Code:
         *cash -= numchips * CHIP_BUYING;
    Then you won't have to adjust the cash in main(). And I would use a better name for CHIP_BUYING like CHIP_COST or cost_per_chip, or price_per_chip.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 08:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM

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