Thread: What next?

  1. #16
    Registered User
    Join Date
    Oct 2003
    Posts
    11

    kk

    for some reason what it was asking didnt make sense really, but this is the first program i made in c++, it was originally made in java for my computer science ap class, but i translated it into c++. Well here it is just to show you what i got through so far (which isnt that much.)

    Code:
    /*
    Created by: Mike Mulé
    Note this was created for DelVeccs Java Ap Class.
    Objective: 
    The local bookstore has a markup of 10 percent on each book sold.
    Our objective : To write a program that takes the sales price of a book as input
    and displays the following outputs:
    1) The markup amount of the book just sold.
    2) The wholesale amount (to go to the publisher) of the book just sold.
    3) The total sales prices of all the books sold thus far.
    4) The total markup oamount of all the books sold thus far.
    
    Created on:October 23, 2003
    Ended on: October 28, 2003
    */
    
    
    #include <iostream.h>
    #include <cstdlib>
    
    
    int CalcMarkup();				//protyping teh functions
    int CalcWholesale();			//protyping teh functions
    int CalcTotalSales();			//protyping teh functions
    int CalcTotalMarkup();			//protyping teh functions
    
    double wholesaleprice = 0;		//declaring my variables and their datatype
    int booknumb = 0;				//declaring my variables and their datatype
    double saleprice = 0;			//declaring my variables and their datatype
    double totalsales = 0;			//declaring my variables and their datatype
    double markup = 0;				//declaring my variables and their datatype
    double totalmarkup =0;			//declaring my variables and their datatype
    
    int main(void)
    {
        int menupick = 0;
    
        while (menupick != 5)		//Do the following when menupick is not equal to 5
        {
          cout << "\n----------------------------------------------------------------------";
          cout << "\nPress [1] to determine the markup amount of the book: \n";
          cout << "Press [2] to determine the wholesale amount of the book: \n";
          cout << "Press [3] to determine the total sales prices of all the books: \n";
          cout << "Press [4] to determine total markup amount of all the books: \n";
          cout << "Press [5] to exit: \n";
          cout << "----------------------------------------------------------------------";
          cout << "\n\t\tPick your lucky number: ";
          cin >> menupick;
          cout << endl;
    
          switch (menupick)
          {
                 case 1:
                      {
    					  CalcMarkup();
                      break;
                      }/*
    Created by: Mike Mulé
    Note this was created for DelVeccs Java Ap Class.
    Objective: 
    The local bookstore has a markup of 10 percent on each book sold.
    Our objective : To write a program that takes the sales price of a book as input
    and displays the following outputs:
    1) The markup amount of the book just sold.
    2) The wholesale amount (to go to the publisher) of the book just sold.
    3) The total sales prices of all the books sold thus far.
    4) The total markup oamount of all the books sold thus far.
    
    Created on:October 23, 2003
    Ended on: October 28, 2003
    */
    
    
    #include <iostream.h>
    #include <cstdlib>
    
    
    int CalcMarkup();				//protyping teh functions
    int CalcWholesale();			//protyping teh functions
    int CalcTotalSales();			//protyping teh functions
    int CalcTotalMarkup();			//protyping teh functions
    
    double wholesaleprice = 0;		//declaring my variables and their datatype
    int booknumb = 0;				//declaring my variables and their datatype
    double saleprice = 0;			//declaring my variables and their datatype
    double totalsales = 0;			//declaring my variables and their datatype
    double markup = 0;				//declaring my variables and their datatype
    double totalmarkup =0;			//declaring my variables and their datatype
    
    int main(void)
    {
        int menupick = 0;
    
        while (menupick != 5)		//Do the following when menupick is not equal to 5
        {
          cout << "\n----------------------------------------------------------------------";
          cout << "\nPress [1] to determine the markup amount of the book: \n";
          cout << "Press [2] to determine the wholesale amount of the book: \n";
          cout << "Press [3] to determine the total sales prices of all the books: \n";
          cout << "Press [4] to determine total markup amount of all the books: \n";
          cout << "Press [5] to exit: \n";
          cout << "----------------------------------------------------------------------";
          cout << "\n\t\tPick your lucky number: ";
          cin >> menupick;
          cout << endl;
    
          switch (menupick)
          {
                 case 1:
                      {
    					  CalcMarkup();
                      break;
                      }
                 case 2:
                      {
                          CalcWholesale();
                      break;
                      }
                 case 3:
                      {
    					  CalcTotalSales();
                      break;
                      }
                 case 4:
                      {
    					  CalcTotalMarkup();
                      break;
                      }
                 default:
                      {
                      break;
                      }
          }
       }
         return 0;		//end the function
    }
    int CalcMarkup()
    {
            cout << "\nEnter the total sales price to determine the markup amount: ";		//prompt user
            cin >> totalsales;				//input what the user entered and assign totalsales a value
            markup = (totalsales * .1);		//find the markup
    		wholesaleprice = (totalsales - markup);		//find the wholesale
            cout << "The markup amount on this book was $" << markup;		//display the markup
    		return 0;		//end the function
    }
    int CalcWholesale()
    {
            cout << "\nEnter the total sales price to determine the wholesale price: ";		//prompt user
            cin >> totalsales;				//input what the user entered and assign totalsales a value
    		markup = (totalsales * .1);		//find the markup
            wholesaleprice = (totalsales - markup);		//find the wholesale price
            cout << "The wholesale price on this book was $" << wholesaleprice;		//display the wholesales price
    		return 0;		//end the function
    }
    int CalcTotalSales()
    {
            cout << "\nEnter the number of books: ";		//prompt user
            cin >> booknumb;			//input what the user entered and assign booknumb a value
            cout << "\nNow enter the wholesale price for 1 of the books: ";		//prompt user
            cin >> wholesaleprice;		//input waht the user entered and assign wholesaleprice a value
            saleprice = (wholesaleprice * 1.1);		//calc the sales price
    		totalsales = (saleprice*booknumb);		// calc the total sales price
    		markup = (saleprice - wholesaleprice);		//calculate the markup
            cout << "\nThe total sales prices of all the books is $" << totalsales;		//display teh results
            cout << "\nThe markup per book was $" << markup;		//display the results
    		return 0;		//end the function
    }
    int CalcTotalMarkup()
    {
            cout << "\nEnter the number of books: ";		//prompt user
            cin >> booknumb;		//input what the user entered and assign booknumb a value.
            cout << "\nEnter the wholesale price for one of the books: ";		//prompt user
            cin >> wholesaleprice;		//get the users input and assign wholesaleprice a value
    		saleprice = (wholesaleprice * 1.1);		//calc the sales price
    		totalsales = (saleprice*booknumb);		//calc the total sales price
    		markup = (saleprice - wholesaleprice);	//calc the markup
    		totalmarkup = (totalsales * .1);		//calc the total markup
            cout << "The total markup amount of all the books was $" << totalmarkup << " or $" << markup << " per book."; //display the following
    		return 0;	 //end the function
    }
    Ill prolly go back to that site and check it out and try and make it, i just didnt understand how was i suppose to make it, like i knew how...bah i cant explain it =x. for some reason the code didnt print out that clear. To see what i wrote that is spaced correctly and doesnt look so weird and is actually formatted correctly go to

    markup

    if you guys have any comments about it, please share i wont spam again. sorry and thx

  2. #17
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    two things right away:

    1. #include <iostream> without the .h and using namespace std; underneath. Or the specifics from std.
    2. Limit the use of globals. This might be difficult as in java you cannot pass by reference.

    Also, for the future, in java you have automatic "garbage" collection, in c++ you have to dealocate memory used. I know that is a big difference.

    I've not compiled your code, is there any errors? does it work? if it does work, rewrite it eliminatin the globals.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #18
    Registered User
    Join Date
    Oct 2003
    Posts
    11
    Originally i didn't have globals and i had to redefine them in each function, so i thought it looked more professional to do global, but i remember my teacher saying globals will screw me over one day, so thanks for reminding me about that. Yes the program compiles with 0 errors. How would i deallocate memory or whatever you said =). See at the top when i was asking for an assignment i was asking for something like i had for this one, like where im given my objective then i have to make it hehe. Okay well nite guys.

  4. #19
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    in C++ the most common way to allocate your own memory is to use the new or the new [] operator. You can use the malloc function/macro from C if you want. To deallocate the memory you allocated you need to use the delete operator if you used new and the delete [] operator if you used new []. You would use free if you use malloc. The syntax for these operators should be available in any tutorial discussing dynamic memory.

    Liberty's online version of TYC++ may or may not have the end of chapter questions/programs. You can check. Warning: not everyone is enthralled by Mr. Liberty's book(s), but that's neither here nor there for this purpose.

Popular pages Recent additions subscribe to a feed