Thread: buy one get one free problem

  1. #16
    Registered User
    Join Date
    Jan 2006
    Posts
    13
    LOL I made a very stupid mistake there. Thank you guys again for helping me. I will let you know how the program turns out.

    -Nick

  2. #17
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Ok I know what you could do within your math, dont need to use if I belive. You want to make it so for ever orange you buy it makes the total lower right? Why not do this?
    1st you need to make 4 ints: int a, int b, int c, and int d.
    2nd make int a apples, int b oranges, and int c and d nothing atm.
    3rd you want to know how meny oranges would take off on apples no? Then take int b - int a that would = how meny apples they will have to buy. This will be int c, make that look like this
    Code:
    c = b-a;
    4th now take int c times lets say 1.50 (this is how much the apples cost alone)
    Code:
    c = c*1.50;
    5th now we will do the same to b
    Code:
    b = b*1.50;
    7th now add thes togather.
    Code:
    d = b+c;
    8th just show what d = to and thats your totall prise. I didnt even need to use the if half. Hope that helps you.
    Last edited by adr; 01-15-2006 at 11:18 AM.

  3. #18
    Registered User
    Join Date
    Jan 2006
    Posts
    13
    I am having another probelem now lol. When the program prints the total it prints it like this 140 and I need it to print like this 14.00. It might be the variable types but I have changed them from int to double to float and there has been no change.

    Thanks for reading.
    -Nick

  4. #19
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    WAIT! forget everything I saidXD Will ok not everything. Ok in my code I found a little a problem... ok thos ints I told you to make, trun them into float like so...
    Code:
    float a;
    Also change step 3 code to
    Code:
    c = a-b;
    Heres what ints, long, bla bla can do with numbers.

    int: used to store integers (whole numbers, no decimals/fractions)
    long: used to store larger integers
    double: used to store numbers with two decimal places
    float: used to store numbers with multiple decimal places
    char: used to store a single character

    This is why you need to learn math kids So you dont have to use the "if" LOL. Best of luck on your work. (I am a kid, I was just playing)
    Last edited by adr; 01-15-2006 at 10:13 PM.

  5. #20
    Registered User
    Join Date
    Jan 2006
    Posts
    13
    I changed the variables that would use float to float but nothing happened, So then I changed them all to float and its still not working. This is very strange. LOL I am basically done with the program with one more little hurdle to jump and its the worst one. Do you guys see anything causing this that I don't see in this code?

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <conio.h>
    
    using namespace std;
    
    float orange (int firsta);//orange prototype.
    float apple (int firstb);//apple prototype. 
    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, resultd;          //The final result. The result of the discount.
    //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;
          
      applecal= apple(usra);//Calling the apple function/
      orangecal= orange(usro);//Calling the orange function.
                  
     addao= applecal+orangecal;//Adding applecal and orangecal together.
        
     resultd= 100/usro*addao;//Finds the amount of money you have saved.
    
     result= resultd-=addao;//Deducts the amount you have saved from the total.
     
           
          cout<<endl;
          cout<<"Your total is :"<<result<<endl;
             
    getch();    
    return 0;     
    
    //Program end      
    }
    //Math for Apples
    float apple(int firsta)
    {
    return firsta/2+firsta%2;
    }
    //Math for Oranges
    float orange(int firstb)
    {
    return firstb*1.50;
    }

  6. #21
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    addao= applecal+orangecal;//Adding applecal and orangecal together.
    cout << addao << endl;
    resultd= 100/usro*addao;//Finds the amount of money you have saved.
    cout << resultd << endl;
    result= resultd-=addao;//Deducts the amount you have saved from the total.
    cout << result << " " << resultd << " " << addao << end;

    Figure it out, do some debugging.

    The last expression looks very odd indeed.

  7. #22
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Here is how I showed it, its just a sample, hope it helps.
    Code:
    #include <iostream>
    
    using namespace std;
    
    float a;
    float b;
    float c;
    float d;
    
    int main(){
    	cout << "Enter a number: ";
    	cin >> a;
    	cout << "Enter a second number: ";
    	cin >> b;
    
    	c = a-b;
    	c = c*1.50;
    	b = b*1.50;
    	d = c+b;
    	
    
    	cout << "The sum is: " << d;
    
    	system("pause");
    
    }

  8. #23
    Registered User
    Join Date
    Jan 2006
    Posts
    13
    Can anyone help me with getting this program to display the total price correctly. It is displaying like this 144 but I want to to display this 14.40.

    Thanks for reading.
    -Nick

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <conio.h>
    
    using namespace std;
    //prototypes
    float applecal(int);//Applecal function.
    float orangecal(int);//Orangecal function.
    //
    int main()
    {
    //text and background color
    system("color 8F");
    //text and background color end
    ////////////////////////////////////////////////////////////////////////////////    
    //vars
    int usro , usra;            //The users choice of apples and oranges.
    float usrac , usroc;        //The calculated results of the users input.
    float add , result;         //Adding both usrac and usroc together. Main result.
    
    //vars end
    ////////////////////////////////////////////////////////////////////////////////
    //sig
           cout<<"             -*********************************************************-"<<endl;
           cout<<"             **                                                       **"<<endl; 
           cout<<"           ****              Nicks store                              ****"<<endl; 
           cout<<"             **                                                       **"<<endl;
           cout<<"             -*********************************************************-"<<endl;
           cout<<endl;
    //sig end
    ////////////////////////////////////////////////////////////////////////////////
    //Program
          cout<<"Welcome to Nick's 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 how many Apples the User wants.
          cout<<"How many Apples would you like :";
          cin>>usra;
          cout<<endl;
       applecal(usra);//calling applecal function.
       usrac=applecal(usra);//getting USRA variable data from applecal.
          
          
          //Asks how many Oranges the User wants.
          cout<<"How many Oranges would you like :";
          cin>>usro;
          cout<<endl;
       orangecal(usro);//calling orangecal function.
       usroc=orangecal(usro);//getting USRO variable data from orangecal.
          
          cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"<<endl;
          cout<<endl;
          
     add = usrac + usroc;//Adds usrac and usroc together.
     
     result = (100/usro*add)- add;
     
          cout<<"Your total is :"<<result<<endl;     
          
    //end program
    getch();
    return 0;
    }
    ////////////////////////////////////////////////////////////////////////////////
    
    //applecal function.
    float applecal(int first)
    {
      return first/2*1.00+first%2;
    }
    ////////////////////////////////////////////////////////////////////////////////
    
    //orangecal function.            
    float orangecal(int first)
    {
          return first*1.50;
    }
    ////////////////////////////////////////////////////////////////////////////////
    Last edited by Nick_365; 02-07-2006 at 08:12 PM.

  9. #24
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    ternary operator to the rescue.
    Code:
    delivered=(type==APPLE?number*2:number);
    Maybe not in the spirit of the assignment, but who cares

  10. #25
    Registered User
    Join Date
    Jan 2005
    Posts
    106
    Why do schools like assigning such asinine programming homework?

  11. #26
    Registered User
    Join Date
    Jan 2006
    Posts
    13
    Quote Originally Posted by jwenting
    ternary operator to the rescue.
    Code:
    delivered=(type==APPLE?number*2:number);
    Maybe not in the spirit of the assignment, but who cares
    lol thanks for the reply but I cant use anything I havent learnt yet. my teacher told me to try deviding the end resuly by 100.00, I get the correct answer and the decemals are in the right place when I do it but it still isnt displaying that extra 0 there. I think it is doing it because the 0s would just keep going and going and going if it had to display them. Is there a way I can get it to just display the last zero like this 14.40 instead of this 14.4.

    Thanks for reading and sorry about spelling mistakes.

    -Nick

  12. #27
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    can you use setprecision
    Last edited by Syneris; 02-08-2006 at 10:25 PM.

  13. #28
    Registered User
    Join Date
    Jan 2006
    Posts
    13
    Quote Originally Posted by Syneris
    can you use setprecision
    I just gave it a try but it still isnt showing that extra 0. I am clueless. Anyone else know whats going on with my program?

    -Thanks for reading.

    -Nick

  14. #29
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Quote Originally Posted by Nick_365
    I am having another probelem now lol. When the program prints the total it prints it like this 140 and I need it to print like this 14.00. It might be the variable types but I have changed them from int to double to float and there has been no change.

    Thanks for reading.
    -Nick
    No offence, but you should really try figuring some of this out yourself ... so far as I can tell, besides the main body of your program, everything was done for you .... try searching through the internet. I recommend google!

  15. #30
    Registered User
    Join Date
    Jan 2006
    Posts
    13
    No offence taken, I guess im kinda bending that rule about people doing your homework for you lol. I will google it and try my best to figure this out. Lol and if that fails then I guess I will be back here to bug you guys some more .

    -Nick

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, 05:20 AM
  2. Replies: 12
    Last Post: 06-24-2005, 04: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, 01:10 AM
  4. SIGABRT upon free()
    By registering in forum C Programming
    Replies: 2
    Last Post: 07-19-2003, 07:52 AM
  5. How can I free what strtok returns?
    By registering in forum C Programming
    Replies: 3
    Last Post: 06-24-2003, 04:56 PM