In C++ program in visual studio step step in main and class each file. The progr
ID: 3823549 • Letter: I
Question
In C++ program in visual studio step step in main and class each file.
The program should have the following functions: a function that increases a specific bin's part count by a specified number. Add Parts: Remove Parts: a function that decreases a specific bin's part count by a specified number. When the program runs, it should repeat a loop that performs the following steps: The user should see a list of what each bin holds and how many parts are in each bin The user can choose to either quit the program or select a bin. When a bin is selected, the user can either add parts to it or remove parts from it. The loop then repeats, showing the updated bin data on the screen. Input Validation: No bin can hold more than 30 parts, so don't let the user add more than a bin can hold. Also, don't accept negative values for the number of parts being added or removed.Explanation / Answer
Here is the code for you:
#include <iostream>
#include <iomanip>
using namespace std;
struct Bin
{
string partName;
int numOfParts;
};
void addParts(Bin bins[], int index, int numOfParts)
{
bins[index].numOfParts += numOfParts;
if(numOfParts < 0)
{
cout << "The number of parts to add cannot be a negative." << endl;
cout << "If you want to remove from bin, choose a different module." << endl;
bins[index].numOfParts -= numOfParts;
}
else if(bins[index].numOfParts > 30)
{
cout << "The bin overflows if added this many parts." << endl;
cout << "The specified number of parts cannot be added." << endl;
bins[index].numOfParts -= numOfParts;
}
else
cout << "The given number of parts added successfully to the bin." << endl;
}
void removeParts(Bin bins[], int index, int numOfParts)
{
bins[index].numOfParts -= numOfParts;
if(numOfParts < 0)
{
cout << "The number of parts to remove cannot be negative." << endl;
cout << "If you want to add to bin, choose a different module." << endl;
bins[index].numOfParts += numOfParts;
}
else if(bins[index].numOfParts < 0)
{
cout << "The bin underflows if removed this many parts." << endl;
cout << "The specified number of parts cannot be removed." << endl;
bins[index].numOfParts += numOfParts;
}
else
cout << "The given number of parts removed successfully from the bin." << endl;
}
void displayBins(Bin bins[], int size)
{
for(int i = 0; i < size; i++)
cout << setw(5) << i+1 << left << setw(15) << bins[i].partName << " " <<setw(5) << bins[i].numOfParts << endl;
}
int main()
{
Bin bins[10] = {{"Valve", 10}, {"Bearing", 5}, {"Bushing", 15}, {"coupling", 21}, {"Flange", 7}
,{"Gear", 5}, {"Gear Housing", 5}, {"Vacuum Gripper", 25}, {"Cable", 18}, {"Rod", 12}};
int index, numOfParts;
while(true)
{
displayBins(bins, 10);
cout << "1. Add Parts." << endl;
cout << "2. Remove Parts." << endl;
cout << "3. Exit." << endl;
cout << "Enter your choice: ";
int choice;
cin >> choice;
switch(choice)
{
case 1:
cout << "Enter the serial number of part: ";
cin >> index;
while(index < 1 || index > 10)
{
cout << "Please follow with the menu." << endl;
break;
}
cout << "Enter the number of parts to add: ";
cin >> numOfParts;
addParts(bins, index, numOfParts);
break;
case 2:
cout << "Enter the serial number of part: ";
cin >> index;
while(index < 1 || index > 10)
{
cout << "Please follow with the menu." << endl;
break;
}
cout << "Enter the number of parts to remove: ";
cin >> numOfParts;
removeParts(bins, index, numOfParts);
break;
case 3: return 0;
default: cout << "Invalid menu choice." << endl;
cout << "Please follow with the menu." << endl;
}
}
}