For the following (working) code, I need to separate the tasks into functions. I was supposed to do this for my assignment, which I received a bad grade for, but with finals coming up, I want to understand what I'm doing wrong and fix it now before it's too late. I can usually get a program to work, but I am pretty bad with defining and calling functions outside of main, and would appreciate any help.
#include <iostream> //allow input and output
using namespace std; //program uses cin and cout
int availableProducts(); //function declarations
int purchase();
int checkStat();
int main() //begin function main
{
char x=0; //variables declaration and initialization
int a[4]={5,5,5,5};
int b[4]={5,5,5,5};
int row=0;
int column=0;
double sales=0;
do //loop to keep making choices until you select exit
{
int purchase( int a[] , int b[] , int row , int column ); //purchase products
{
do
{
cout << "Please choose a row" << endl;
cin >> row;
cout << "Please choose a column" << endl;
cin >> column;
if (column>4 || column<1)
cout << "That is not a valid choice" << endl; //condition for column out of range
else
switch (row) //switches between arrays
{
case 1:
{
if (a[column-1]<1)
cout << "Sorry, none left" << endl; //informs user item is out of stock, skips decrement of array
else
a[column-1]=a[column-1]--; //decrements a[?] as a function of the variable column
break;
}
case 2:
{
if (b[column-1]<1)
cout << "Sorry, none left" << endl; //informs user item is out of stock, skips decrement of array
else
b[column-1]=b[column-1]-1; //decrements b[?] as a function of the variable column
break;
}
default :
cout << "That is not a valid choice" << endl; //condition for row out of range
}
}
while(row!=1 && row!=2 && column<=4);
if (row==1 && column==4)
cout << "DO THE DEW!" << endl; //program advises you to do the dew
else if (row==2 && column==4)
cout << "Snickers really satisfies" << endl;
else
{
cout << "Thank you for choosing the vending machine simulator" << endl;
cout << "Please come again" << endl; //program is being polite
}
cout << " " << endl;
}
break;
}
case '2':
{
system("cls");
int checkStat( int a[] , int b[] , double sales); //check available products and display sales
{
sales=(((5-a[0]) + (5-a[1]) + (5-a[2]) + (5-a[3]))*1.25)+(((5-b[0]) + (5-b[1]) + (5-b[2]) + (5-b[3]))*0.75);
cout << "Total number of items sold from row 1: " << ((5-a[0]) + (5-a[1]) + (5-a[2]) + (5-a[3])) << endl;
cout << "Total number of items sold from row 2: " << ((5-b[0]) + (5-b[1]) + (5-b[2]) + (5-b[3])) << endl;
cout << " " << endl;
cout << "Total sales are: $" << sales << endl;
}
break;
}
case '3':
{
cout << "Goodbye!" << endl; //break out of loop and exit
break;
}
default:
cout << "That function is not possible" << endl;
}
system("pause"); //pause to see results
system("cls");
}
while(x!='3'); //completes loop to keep choosing
return 0; //indicate successful program execution
} //end function main
Aren't you getting comments on your code when it is marked?
Anyway, what I noticed:
1. Use meaningful variable names. a, b and x don't qualify.
2. Don't declare variables before they are actually needed.
3. Do not use system. See http://www.cplusplus.com/forum/articles/11153/
4. You cannot define a function inside another function. What you're actually doing inside main is to declare a function purchase with the given parameters and then you execute the code that probably was meant for purchase inside main. The declaration (together with the first declaration of purchase) is pointless. Same goes for availableProducts and checkStat.
5. You probably should have merged a and b into one array, as to avoid all the code duplication in the product selection.
6. For calculation of sales, you should have used a loop and a constant array with the prices for each product.