buy one get one free problem

This is a discussion on buy one get one free problem within the C++ Programming forums, part of the General Programming Boards category; I am making a program that calculates the price of apples (its a school one). To throw in a twist ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    13

    buy one get one free problem

    I am making a program that calculates the price of apples (its a school one). To throw in a twist they made it so when you buy one apple you get one free. I am not aloud to use the "if" feature in this program. It is driving me crazy, I don't know how to make it so when I buy 3 apples it shows up as 2 dollars because of this whole buy one get one free thing.

    Here is the code I have so far.
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    double apple (double first,double second); 
    int main(int)
    {
    //vars
    string username;           //Users name.
    double usra,usrap=1.00;                 //Users choice of Appales.
    double usro,usrop=1.50;                //Users choice of Oranges.
    double applecal;           //Apple calculations.
    double orangecal;         //Orange calculations.
    //vars end
    //sig
           cout<<"*********************************************************"<<endl;
           cout<<"*                                                       *"<<endl; 
           cout<<"*              Nicks Grocery store                      *"<<endl; 
           cout<<"*                                                       *"<<endl;
           cout<<"*********************************************************"<<endl;
           cout<<endl;
    //sig end
    //Program
          cout<<"Welcome to Nick's Grocery Store."<<endl;
          cout<<endl;
          cout<<"****Todays Specials.*****"<<endl;
          cout<<"*Buy one Apple get one  *"<<endl;
          cout<<"*free!                  *"<<endl;
          cout<<"*-----------------------*"<<endl;
          cout<<"*Each Orange you buy    *"<<endl;
          cout<<"*deducts 1% off your    *"<<endl;
          cout<<"*bill.                  *"<<endl;
          cout<<"*************************"<<endl;
          cout<<"Max of 100 items per customer"<<endl;
          cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl;
          cout<<endl;
          
          //Asks for Users name.
          cout<<"Enter your name :";
          cin>>username;
          cout<<endl;
          
          //Asks how many Apples the User wants.
          cout<<"How many Apples would you like "<<username<<" :";
          cin>>usra;
          cout<<endl;
          
          
          //Asks how many Oranges the User wants.
          cout<<"How many Oranges woukd you like "<<username<<" :";
          cin>>usro;
          cout<<endl;
          cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl;
          
      applecal= apple(usra,usrap);
      
      cout<<applecal<<endl;
       
          
         
    
    //Program end      
    }
    
    double apple(double first,double second)//Math for Apples
    {
    return first*second;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Maybe you could use division to see how many pairs of apples were requested and then add a dollar if the number is odd.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    13
    sorry if this comment is a bit dumb. Wouldnt you need the "if" function to do that tho?

    Thx for the reply tho.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    No. You could use the result of the division times the price and add the result of modulo 2 times the price. For example, 5 apples makes 5/2 (which is 2) * $1.00 plus 5%2 (which is 1) * $1.00 = $3.00. And 6 apples makes 6/2 (which is 3) * $1.00 plus 6%2 (which is 0) * $1.00 = $3.00.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    first a little nitpicking:
    Code:
    int main(int)
    what's the (int) for?
    Code:
    //vars ...
    is there a reason they're all doubles?
    Code:
    cout<<"How many Oranges woukd you like "<<username<<" :";
    you spelled 'Would' wrong
    Code:
    double apple(...){...}
    why use a function... you could just as easily do that math right in the code

    okay, now on to your problem... change those doubles to integers and maybe the answer will become more clear to you. Remember what happens when you use integer division:
    Code:
    #include <iostream>
    
    int main()
    {
    	int apples;
    	int free;
    	
    	std::cout<<"Enter the number of apples: ";
    	std::cin>>apples;
    	std::cin.ignore(1);
    
    	free=apples/2;
    
    	std::cout<<"You will get "<<free<<" free apples."<<std::endl;
    	return 0;
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    13
    Thank you guys so much you helped a lot. I owe you all one.

    The spelling errors were a rush job sorry. The int was accidentally left there when i was doing a little trial and error with the function. Lol the function is there because they want us to do all the math in another function haha believe me if I had a choice it would all be in the code .

    Again thanks for the help guys.
    -Nick

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> believe me if I had a choice it would all be in the code

    Separating things into functions that do a single job is a good thing, so making that a function is good practice.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    54
    could use a switch also..

    Code:
    switch(input)
    {
    case 1:
        ...
        break;
    case 2:
     etc...

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Daved
    >> believe me if I had a choice it would all be in the code

    Separating things into functions that do a single job is a good thing, so making that a function is good practice.
    erm, since when was putting a simple multiplication into a function a good thing?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Registered User
    Join Date
    Jan 2006
    Location
    Melbourne, Australia
    Posts
    11
    Hey, its a teacher, what do you expect?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> erm, since when was putting a simple multiplication into a function a good thing?

    Always? Of course, there are extremes, but in this case the calculation of the cost of apples given the quantity and price is perfect for separation into a function. It is just a matter of practicing good design. If you separate that into its own function, then in the future you can change the formula quickly and easily (e.g. adding buy one get one free). You can re-use that function anywhere else you want to calculate the cost of apples. Just because you don't use it in multiple places in your program yet doesn't mean you won't.

    >> Hey, its a teacher, what do you expect?

    IMO the teacher is correct in this case. Try to understand the reasons why.

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    such a simple function would be a good candidate for inline, right? And if apple was an object, then the same function could be used to calculate the price of different kinds of apple that have different prices.

  13. #13
    Registered User
    Join Date
    Jan 2006
    Posts
    13
    I have finished the math part of the program but not I am faced with another problem . When I run the program and try to calculate it all it gives me these numbers and a + sign instead of just numbers like 14.00. I think it might be the variables but I have messed with them as much as I can and nothing is changing.

    Here is the code.
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <conio.h>
    
    using namespace std;
    
    float orange (int first);
    float apple (int first); 
    int main()
    {
    //vars
    string username;                //Users name.
    int usra,usro;                  //Users choice of apples. Users choice of Oranges.
    float applecal, orangecal;      //Apple calculations. Orange calculations.
    float addao;                    //adding apples and oranges together.
    float result;                   //The final result.
    //vars end
    //sig
           cout<<"*********************************************************"<<endl;
           cout<<"*                                                       *"<<endl; 
           cout<<"*              Nicks Grocery store                      *"<<endl; 
           cout<<"*                                                       *"<<endl;
           cout<<"*********************************************************"<<endl;
           cout<<endl;
    //sig end
    //Program
          cout<<"Welcome to Nick's Grocery Store."<<endl;
          cout<<endl;
          cout<<"****Todays Specials.*****"<<endl;
          cout<<"*Buy one Apple get one  *"<<endl;
          cout<<"*free!                  *"<<endl;
          cout<<"*-----------------------*"<<endl;
          cout<<"*Each Orange you buy    *"<<endl;
          cout<<"*deducts 1% off your    *"<<endl;
          cout<<"*bill.                  *"<<endl;
          cout<<"*************************"<<endl;
          cout<<"Max of 100 items per customer"<<endl;
          cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl;
          cout<<endl;
          
          //Asks for Users name.
          cout<<"Enter your name :";
          cin>>username;
          cout<<endl;
          
          //Asks how many Apples the User wants.
          cout<<"How many Apples would you like "<<username<<" :";
          cin>>usra;
          cout<<endl;
          
          
          //Asks how many Oranges the User wants.
          cout<<"How many Oranges would you like "<<username<<" :";
          cin>>usro;
          cout<<endl;
          cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl;
          cout<<applecal<<endl<<orangecal;
      applecal= apple(usra);
      orangecal= orange(usro);
                  
     addao= applecal+orangecal;   
     result= usro/100*addao;
             
          cout<<endl;
          cout<<"Your total is :"<<result<<endl;
             
    getch();    
    return 0;     
    
    //Program end      
    }
    //Math for Apples
    float apple(int first)
    {
    return first/2+first%2;
    }
    //Math for Oranges
    float orange(int first)
    {
    return first*1.50;
    }
    Thanks for reading.
    -Nick

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    cout<<applecal<<endl<<orangecal;
    applecal= apple(usra);
    orangecal= orange(usro);

    Do calculations, then print

    applecal= apple(usra);
    orangecal= orange(usro);
    cout<<applecal<<endl<<orangecal;

  15. #15
    Registered User
    Join Date
    Jan 2006
    Posts
    14
    hmm...unlucky you...would have been much easier with the 'if' function...
    so scratch that off...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 06:20 AM
  2. Replies: 12
    Last Post: 06-24-2005, 05:27 PM
  3. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 02:10 AM
  4. SIGABRT upon free()
    By registering in forum C Programming
    Replies: 2
    Last Post: 07-19-2003, 08:52 AM
  5. How can I free what strtok returns?
    By registering in forum C Programming
    Replies: 3
    Last Post: 06-24-2003, 05:56 PM

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