-
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();
}
-
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.