Results 1 to 7 of 7
  1. #1
    Artist/Hacker
    Hacker
    GoodCodes's Avatar
    Join Date
    May 2011
    Posts
    498
    Points
    1,313
         User Info     Contact     Gamer ID
    Join Date
    May 2011
    Posts
    498
    Points
    1,313

    Default Help with C++ Program

    Hey i need some help from someone who knows C++ Programming, My hw is Create a program that allows a shipping clerk at a company to input an item’s name, the total quantity of that item that is currently in inventory, and the number of that item that will fit in a box for shipping. The program should display back the item’s name, the number of full boxes that can be shipped based on the quantity in inventory, and the number of items that will be left over.

    What the program should look like:

    //this is the inputs
    Enter item name: flat head screwdriver
    How many in inventory: 230
    How many fit in a box: 100

    //outputs
    Item: flat head screwdriver
    Number of full boxes: 2
    Quantity left over: 30

    Could someone who knows programming please help me thanks

    0 Not allowed! Not allowed!
    Last edited by GoodCodes; 09-20-2011 at 07:35 AM.
    .................................................. ..................
    Want a Sig? Send me a Pm with your request.
    .................................................. ...................

  2. #2
    Bot Bot's Avatar
    Join Date
    Aug 2011
    Posts
    2,671
    Points
    7,720
         User Info     Contact     Gamer ID
    Join Date
    Aug 2011
    Posts
    2,671
    Points
    7,720

    Default

    You could do that in MS Excel.

    0 Not allowed! Not allowed!








  3. #3
    Nooblet
    Join Date
    Jul 2011
    Posts
    45
    Points
    70
         User Info     Contact     Gamer ID
    Join Date
    Jul 2011
    Posts
    45
    Points
    70

    Default

    look, you create a very simple class that contains all of the items.

    like
    Code:
    class ship
    {
    string name;
    unsigned int NumOfItems;
    unsigned int SizeOfBox;
    
    public:
    unsigned int getNumOfItems()const;
    void setNumOfItems(unsigned int NumOfItems);
    //do the same also with the other variables in the class
    ship();
    //explicit ~destructor isn't needed in this case
    };
    you can initialize the values with zeroes in the constructor and input the new values with set/get functions in the public section of the specific class. It's important that the variables themselves are in private section, since you want to make sure that you cannot input impossible values (divide by zero etc)

    So your setSizeOfBox() function could look like this:
    Code:
    bool ship::setSizeOfBox(unsigned int SizeOfBox)
    {
    if(!SizeOfBox)
    return false;
    
    this->SizeOfBox = SizeOfBox;
    return true;
    }

    And for the output you need to "get" the values and calculate them just on the fly.

    ie.
    Code:
    unsigned int fullBoxes = cShip.getNumOfItems()/cShip.getSizeOfBox();
    unsigned int rest = 0;
    cout << "Number of full Boxes: " << fullBoxes << endl;
    rest = cShip.getNumOfItems()%cShip.getSizeOfBox();
    cout << "Left overs: " << rest << endl;

    I hope this helped you ;)

    PS:
    Don't take the code by word, I just typed it in without testing...

    Quote Originally Posted by eXtr3m3 View Post
    You could do that in MS Excel.
    I guess the point of this homework is NOT to get a result, but the learning effect ;)

    0 Not allowed! Not allowed!
    Last edited by Yoshi Party; 09-20-2011 at 09:12 AM. Reason: little coding mistake

  4. #4
    Artist/Hacker
    Hacker
    GoodCodes's Avatar
    Join Date
    May 2011
    Posts
    498
    Points
    1,313
         User Info     Contact     Gamer ID
    Join Date
    May 2011
    Posts
    498
    Points
    1,313

    Default

    I have it all up to he point on how to get the remainder and print it.
    This is what i have but it does not work.

    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    // declare variables
    int itemQuantity = 0;
    int itemsFit = 0;
    double itemBox = 0.0;
    string itemName = "";

    // enter input items
    cout << "Enter the item name: ";
    getline(cin, itemName);
    cout << "Enter the items quantity in stock: ";
    cin >> itemQuantity;
    cin.ignore(100, '\n');
    cout << "Enter the items that fit in a box: ";
    cin >> itemsFit;
    cin.ignore(100, '\n');
    int itemQuantity = 0.0;

    int itemBox = itemQuantity % itemsFit;


    // display output items
    cout << "Item: " << itemName << endl;
    cout << "Number of full Boxes " << itemBox << endl;
    //cout << "Numer of items Left over " << itemLeft << endl;


    system("pause");
    }

    0 Not allowed! Not allowed!
    .................................................. ..................
    Want a Sig? Send me a Pm with your request.
    .................................................. ...................

  5. #5
    Nooblet
    Join Date
    Jul 2011
    Posts
    45
    Points
    70
         User Info     Contact     Gamer ID
    Join Date
    Jul 2011
    Posts
    45
    Points
    70

    Default

    I copied your code and got several errors like redeclaration of ItemBox with different types which moreover make no sense in the point.
    You don't need double as type, since you don't cut your items to make them fit into the box. You would only put them in if there is enough space for the entire item ;)
    Imagine it and you will more likely understand what I mean...

    the cin.ignore() lines are a little confusing to me :/
    I recommend you to create a class which is propably what your teacher wants you to do in a c++ program ;)
    Then you can also make sure that as said before, no illegal values are used->no more confusing results ;)

    Oh, and instead of system("pause"), which is a windows-only function you should use the lines:
    cin.ignore();
    cin.get()
    to make your program wait for an input before it quits ;)

    Edit:
    and you can also leave out the empty string initialization, since std:tring has a proper constructor for this.
    string name; //without = "";
    is more than enough ;)

    Yet, a PLUS for you, since you didn't try to use const char* but the good std:tring class.

    0 Not allowed! Not allowed!
    Last edited by Yoshi Party; 09-20-2011 at 08:22 AM.

  6. #6
    Artist/Hacker
    Hacker
    GoodCodes's Avatar
    Join Date
    May 2011
    Posts
    498
    Points
    1,313
         User Info     Contact     Gamer ID
    Join Date
    May 2011
    Posts
    498
    Points
    1,313

    Default

    I'm going to wait for my dad to get home and help, I need the ignore function because that's the lesson he's teaching us this week. To ignore the enter in the computer. Also we have to make it grap the remainder that I have no idea how to do yet. My dads an engineer so he knows how to do all this.

    0 Not allowed! Not allowed!
    .................................................. ..................
    Want a Sig? Send me a Pm with your request.
    .................................................. ...................

  7. #7
    Nooblet
    Join Date
    Jul 2011
    Posts
    45
    Points
    70
         User Info     Contact     Gamer ID
    Join Date
    Jul 2011
    Posts
    45
    Points
    70

    Default

    strange, I've never seen ignore() this way.
    Usually it's used without parameter to avoid skipping the next input func ;)

    You can see what I mean in the last two lines:
    cin.ignore();
    cin.get();

    ignore() makes sure that cin.get() won't be skipped and cin.get() does nothing but wait for input...

    ship.h
    Code:
    #ifndef SHIP_H
    #define SHIP_H
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class ship
    {
    	std::string Name;
    	unsigned int NumOfItems;
    	unsigned int SizeOfBox;
    
    public:
    	ship():NumOfItems(0),SizeOfBox(1){};
    
    	std::string getName()const;
    	bool setName(std::string Name);
    
    	unsigned int getNumOfItems()const;
    	void setNumOfItems(unsigned int NumOfItems);
    
    	unsigned int getSizeOfBox()const;
    	bool setSizeOfBox(unsigned int SizeOfBox);
    
    	unsigned int getNumOfFullBoxes()const;
    	unsigned int getNumOfLeftOverItems()const;
    };
    
    #endif // SHIP_H
    ship.cpp
    Code:
    #include "ship.h"
    
    string ship::getName()const
    {
    	return Name;
    }
    
    bool ship::setName(string Name)
    {
    	if(!Name.size())
    		return false;
    
    	this->Name = Name;
    	return true;
    }
    
    unsigned int ship::getNumOfItems()const
    {
    	return NumOfItems;
    }
    
    void ship::setNumOfItems(unsigned int NumOfItems)
    {
    	this->NumOfItems = NumOfItems;
    }
    
    unsigned int ship::getSizeOfBox()const
    {
    	return SizeOfBox;
    }
    
    bool ship::setSizeOfBox(unsigned int SizeOfBox)
    {
    	if(!SizeOfBox)
    		return false;
    
    	this->SizeOfBox = SizeOfBox;
    	return true;
    }
    
    unsigned int ship::getNumOfFullBoxes()const
    {
    	unsigned int NumOfFullBoxes = getNumOfItems() / getSizeOfBox();
    	return NumOfFullBoxes;
    }
    
    unsigned int ship::getNumOfLeftOverItems()const
    {
    	unsigned int NumOfLeftOverItems = getNumOfItems() % getSizeOfBox();
    	return NumOfLeftOverItems;
    }
    main.cpp
    Code:
    #include <iostream>
    #include <string>
    #include "ship.h"
    
    using namespace std;
    
    int main()
    {
    	ship Titanic;
    
    	string temp_name;
    	do
    	{
    		cout << "Name: ";
    		getline(cin, temp_name);
    	}
    	while(!Titanic.setName(temp_name));
    
    	cout << "Number of Items: ";
    	unsigned int temp_num = 0;
    	cin >> temp_num;
    	Titanic.setNumOfItems(temp_num);
    
    	unsigned int temp_size = 0;
    	do
    	{
    		cout << "Size of each Box: ";
    		cin >> temp_size;
    	}
    	while(!Titanic.setSizeOfBox(temp_size));
    
    	cout << "Name: " << Titanic.getName() << endl;
    	cout << "Number of full boxes: " << Titanic.getNumOfFullBoxes() << endl;
    	cout << "Items left over: " << Titanic.getNumOfLeftOverItems() << endl;
    
    	cin.ignore();
    	cin.get();
    	return 0;
    }

    0 Not allowed! Not allowed!

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -10. The time now is 06:00 PM.
Powered by vBulletin®
Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.