structures and functions

This is a discussion on structures and functions within the C++ Programming forums, part of the General Programming Boards category; I think i made the structure right???, now I have to make it work with the functions....some by reference, some ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    27

    structures and functions

    I think i made the structure right???, now I have to make it work with the functions....some by reference, some by value... the program works right without the structure (which gives me my first clue that something isn't right). Also how do you clear the screen? I tried clrscr(); and it didn't work. I am in MS Visual C++ console (2003). I'd appreciate any help....
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <conio.h>
    
    #using <mscorlib.dll>
    
    using namespace std;
    
    struct inventory
    {
    public:
    	char product;
    	int quantity;
    };
    
    
    
    void main_menu(char&, int&, float&);
    void get_input(char&, int&, float&);
    float calculate(char, int, float);
    void update_info(char&, int&, float&);
    
    int main()
    {
    	inventory i1;
    
    	i1.product;
    	i1.quantity = 0;
    
    
    	float price, total;
    	char product;
    	int quantity;
    
        main_menu(product, quantity, price);
    	return 0;
    }
    void main_menu(char&, int&, float&)
    {
    	int numbinput, quantity;
    	float price, total;
    	char product;
    
    	do
    	{
    		cout << endl << "Enter a number: " << endl;
    
    		cout << "1.  Enter Information" << endl;
    		cout << "2.  Calculate Sales" << endl;
    		cout << "3.  Update Informaton" << endl;
    		cout << "4.  Exit" << endl << endl << endl;
    		cin >> numbinput;
    		switch (numbinput)
    		{
    			case 1:
    			{
    				get_input(product, quantity, price);
    				break;
    			}
    
    			case 2:
    			{
    				total = calculate(product, quantity, price);
    				cout << "Total: $" << total << endl;
    				cout << "Press the spacebar to continue...";
    				getch();
    				break;
    			}
    
    			case 3:
    			{
    				update_info(product, quantity, price);
    				break;
               	}
    		}
    	}
    	while (numbinput != 4);
    }
     
    void get_input(char& product, int& quantity, float& price)
    {
    	cout << "Product Name: ";
    	cin >> product;
    	cout << "Quantity: ";
    	cin >> quantity;
    	cout << "Price: $";
    	cin >> price;
    	cout << "Press the spacebar to continue...";
    	getch();
    }
    
    float calculate (char product, int quantity, float price)
    {
    	float total;
    
    	cout << "Product: " << product << endl;
    	cout << "Quantity: " << quantity << endl;
    	cout << "Price: " << price << endl;
    	total = price * quantity;
    
        return total;
    }
    
    void update_info(char& product, int& quantity, float& price)
    {
    	cout << "Product: " << product << endl;
    	cin >> product;
    	cout << "Quantity: "<< quantity << endl;
    	cin >> quantity;
    	cout << "Price: " << price << endl;
    	cin >> price;
    	cout << "Press the spacebar to continue...";
    	getch();
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    i'd go something like this:

    Code:
    struct inventoryItem
    {
      inventoryItem() : quantity(1000) {};
      char productID;
      int quantity;
    };
    
    int main()
    {
       inventoryItem inventory[10];//by default start with 1000 of each inventoryItem
    
       int i;
       int choice = -1;
    
       //stock the inventory with items by naming them
       for(i = 0; i < 10; ++i)
       {
          cout << "enter productID" << endl;
          cin >> inventory[i].productID;
        }
    
        while (choice != 0)
        {
           //menu to deal with inventory
           cout << "choose from current selection" << endl;
           cout << "1 select item " << endl;
           cout << "2 display items" << endl;
           cout << "0 quit menu" << endl;
           cin >> choice;
        
           switch (choice)
           {
              case 1:
                  selectItem();
                  break;
              case 2:
                  displayItems();
                  break;
              case 0:
                  terminateMenu();
                  break;
              default:
                  cout << "inappropriate selection----try again" << endl;
                  break;
            }
    }
    
    void selectItem()
    {
        displayItems();
        cout << "enter selection" << endl;
        int selection;
        cin >> selection;
        cout << "enter quantity" << endl;
        int num;
        cin >> num;
        if(inventory[selection].quantity >= num)
          inventory[selection].quantity -= num;
        else 
          cout << "not that many # " << selection << " available." << endl;
    }
    
    void displayItems()
    {
       int i; 
       for(i = 0; i < 10; ++i)
         cout << i << ":  " << inventory[i].productID << endl;
    }
    
    void terminateMenu()
    {
       cout << "if you found our services helpful, please return again." << endl;
    }
    do a search of the board or the FAQ section to learn how to clear the screen.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. COntrol structures and functions questions
    By angelicscars in forum C Programming
    Replies: 1
    Last Post: 11-21-2005, 10:50 AM
  2. passing structures to functions
    By AmazingRando in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2003, 11:22 AM
  3. Array of Structures and Functions
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 05-04-2003, 07:06 AM
  4. passing array structures to functions
    By lukejack in forum C Programming
    Replies: 2
    Last Post: 04-08-2003, 02:17 PM
  5. data structures / hash functions
    By rickc77 in forum C Programming
    Replies: 5
    Last Post: 11-11-2001, 12:54 PM

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