Hello, Ive been working on this inventory for practice for quite some time now and this is the last thing i need to do ive been trying everything i know to get it to work. which is adjusting the supply for each item. I have gotten options 1 and 2 to work and save in thefile i close and open the file and press option 2 and the data saved in the file show up and it works. But when i try to edit the inventory(option 3) it doesnt work it wont save in the file and if it does the numbers are rediculous.
Your issue is that you're using uninitialized values. Here is the beginning of your editInventory() function. You never set s, s1, s2, s3, and s4 to the current values from your file.
Code:
string Item1, Item2, Item3, Item4, Item5, choice5;
unsigned int choice, choice2, choice3, choice4, choices, s,s1, s2, s3, s4, answer,answer1,answer2, answer3,answer4, answer5;
system("cls");
cout << "Edit Inventory" << endl;
cout << "\n1. Add to inventory\n";
cout << "2. Subtract from inventory\n" << endl;
cin >> choices;
if (choices == 1){
cout << "\nHow much do you want to add to Item 1 " << Item1 << endl;
cin >> answer;
cout << Item1 << s + answer << endl;
cout << "How much do you want to add to Item 2 " << Item2 << endl;
cin >> answer2;
cout << s1 + answer2 << endl;
cout << "How much do you want to add to Item 3 " << Item3 << endl;
cin >> answer3;
cout << s2 + answer3 << endl;
cout << "How much do you want to add to Item 4 " << Item4 << endl;
cin >> answer4;
cout << s3 + answer4 << endl;
cout << "How much do you want to add to Item 5 " << Item5 << endl;
cin >> answer5;
cout << s4 + answer5 << endl;
Also, you could save yourself a lot of code by writing many of these repetitive items using arrays and loops. And, since this is a C++ forum, I should also suggest using a class to encapsulate the behavior of your inventory items
Im still new to c++ and tried to do this with what I know. and I dont want to initialize the values to a certain amount or quantity I want to get it to take s from earlier in the function addInventory() read it from there and add it to the answer and print that to the file to save. but its not doing that.. and I cannot figure out why.
Well, for one, you are declaring s-s5 in your editInventory() function. These are NOT the same s-s5 that are in your addInventory() function. C++ has no way to know this. You would need to store those values from addInventory() either in variables global to both functions or in a file. A class would be a good choice (unless you'd rather write the values into a file and use that file in your editInventory() function).
Code:
class MyClass
{
public:
int foo;
void add();
void sub();
};
In this code, both add and sub can use foo in their implementations.
Bookmarks