using a function to add or subtract values in an array

I'm trying to create a function that, when called someone can input a number that will increase the number of parts in a bin, or decrease depending on which function they choose. I'm relatively new to C++ so any help on how to structure my function would be greatly appreciated.

// Question 2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>

using namespace std;

struct info {
string partDescription;
int numberInBin;
};
//double addParts(float, float);
//double removeParts(float, float);

int main()
{
info parts[10];

ifstream file;
file.open("parts.txt");
if (!file) {
cout << "Error, File Does Not Exist" << endl;
}
//else { cout << "File Found" << endl; }

for (int i = 0; i < 10; i++) {
file >> parts[i].partDescription >> parts[i].numberInBin;
}

int x = 1;
string part;
int bin;
int choice;

while (x == 1){
for (int i = 0; i < 10; i++) {
part = parts[i].partDescription;
cout << i << ". " << left << setw(33) << parts[i].partDescription;
cout << parts[i].numberInBin << endl;
}
cout << endl;
cout << "Please Select one of the Following Parts: ";
cin >> choice;
if (choice == 0) {
cout << "Please Select Either 1 to Add parts to the bin, or 2 to Remove Parts from the bin.";
}
else if (choice == 1) {
cout << "Please Select Either 1 to Add parts to the bin, or 2 to Remove Parts from the bin.";
}
else if (choice == 2) {
cout << "Please Select Either 1 to Add parts to the bin, or 2 to Remove Parts from the bin.";
}
else if (choice == 3) {
cout << "Please Select Either 1 to Add parts to the bin, or 2 to Remove Parts from the bin.";
}
else if (choice == 4) {
cout << "Please Select Either 1 to Add parts to the bin, or 2 to Remove Parts from the bin.";
}
else if (choice == 5) {
cout << "Please Select Either 1 to Add parts to the bin, or 2 to Remove Parts from the bin.";
}
else if (choice == 6) {
cout << "Please Select Either 1 to Add parts to the bin, or 2 to Remove Parts from the bin.";
}
else if (choice == 7) {
cout << "Please Select Either 1 to Add parts to the bin, or 2 to Remove Parts from the bin.";
}
else if (choice == 8) {
cout << "Please Select Either 1 to Add parts to the bin, or 2 to Remove Parts from the bin.";
}
else if (choice == 9) {
cout << "Please Select Either 1 to Add parts to the bin, or 2 to Remove Parts from the bin.";
}
}

system("pause");
return 0;
}
/*double addParts(float x, float parts[i].numberInBin ) {

}

double removeParts(float x, float y) {
}*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void add2arr(int value, int* ip, int index)
{
     ip[index]+= value;
}

//note that it will subtract also, and note that the redundant code blocks are not necessary.

int as; int howmuch;
cout << "Please Select one of the Following Parts: ";
cin >> choice; //this is the index into parts... you don't need a code block for every value of choice
cout << "Please Select Either 1 to Add parts to the bin, or 2 to Remove Parts from the bin.";
cin >> as;
cout << "Please enter how many parts\n"
cin >> howmuch;
if(as == 2) howmuch *= -1; //adding this is same as subtraction.  or you can explicitly subtract by writing a similar function to add.  
add2arr(howmuch, parts, choice); 


I left you with a little work to make it work for your struct. But this is the general idea.
Last edited on
Topic archived. No new replies allowed.