Cant figure out whats wrong

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. Had to take out a part of the code so it could all fit. the part i am trying to fix though is void editInventory().

Lines 81 - 148.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <windows.h>
using namespace std;

void addInventory()
{
     system("cls");
     char filename[20];
     string Item1, Item2, Item3, Item4, Item5, choice5;
     unsigned int choice, choice2, choice3, choice4, s,s1, s2, s3, s4, answer,answer1,answer2, answer3,answer4, answer5;
               cout << "\nAdd Item 1 ";
               cin >> Item1;
               cout << "Add Item 2 ";
               cin >> Item2;
               cout << "Add Item 3 ";
               cin >> Item3;
               cout << "Add Item 4 ";
               cin >> Item4;
               cout << "Add Item 5 ";
               cin >> Item5;
                              
               cout << "\n\nItem Quantity\n\n"; // choosing quantities for items named above                                         
               cout << Item1 << ": ";
               cin >> s;
               cout << Item2 << ": ";
               cin >> s1;
               cout << Item3 << ": ";
               cin >> s2;
               cout << Item4 << ": ";
               cin >> s3;
               cout << Item5 << ": ";
               cin >> s4; 
               system("cls");
               
               cout << "\nUpdating Darkroom Inventory.... \n";// printing out items and quantities
               cout << Item1 << ": " << s << endl;
               cout << Item2 << ": " << s1 << endl;
               cout << Item3 << ": " << s2 << endl;
               cout << Item4 << ": " << s3 << endl;
               cout << Item5 << ": " << s4 << endl;
               system("cls");
               cout << "\nEnter the name of the file you want your data to be saved in." << endl;
               cin >> filename;
               ofstream Inventory(filename, ios::out);
               Inventory << Item1 << ": " << s << "\n";
               Inventory << Item2 << ": " << s1 << "\n";
               Inventory << Item3 << ": " << s2 << "\n";
               Inventory << Item4 << ": " << s3 << "\n";
               Inventory << Item5 << ": " << s4 << endl;
               
}
void checkInventory()
{
     void editInventory()
{
     
     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;
                      
                      ofstream Inventory("inventory.txt", ios::in);
                      Inventory << Item1 << ": " << s - answer << endl;
                      Inventory << Item2 << ": " << s1 - answer2 << endl;
                      Inventory << Item3 << ": " << s2 - answer3 << endl;
                      Inventory << Item4 << ": " << s3 - answer4 << endl;
                      Inventory << Item5 << ": " << s4 - answer5 << endl;
                      
                      }else {  

                      cout << "\nHow much do you want to subtract to Item 1 " << Item1 << endl;
                      cin >> answer;
                      cout << s - answer << endl;
                      
                      cout << "How much do you want to subtract to Item 2 " << Item2 << endl;
                      cin >> answer2;
                      cout << s1 - answer2 << endl;
                      
                      cout << "How much do you want to subtract to Item 3 " << Item3 << endl;
                      cin >> answer3;
                      cout << s2 - answer3 << endl;
                      
                      cout << "How much do you want to subtract to Item 4 " << Item4 << endl;
                      cin >> answer4;
                      cout << s3 - answer4 << endl;
                      
                      cout << "How much do you want to subtract to Item 5 " << Item5 << endl;
                      cin >> answer5;
                      cout << s4 - answer5 << endl;
                      
                      ofstream Inventory("inventory.txt", ios::in);
                      Inventory << Item1 << ": " << s - answer << endl;
                      Inventory << Item2 << ": " << s1 - answer2 << endl;
                      Inventory << Item3 << ": " << s2 - answer3 << endl;
                      Inventory << Item4 << ": " << s3 - answer4 << endl;
                      Inventory << Item5 << ": " << s4 - answer5 << endl;
                      }                                                                                                    
                  
     
}

int main()
{
       int choice, choice2;          
    do {
    system("cls");
    cout << "Darkroom Inventory Version 1.\n\n";      
    cout << "1. Add Inventory\n";
    cout << "2. Check Inventory\n";
    cout << "3. Edit Inventory\n";
    cin >> choice;
    switch ( choice ) {
           case 1: addInventory(); break;
           case 2: checkInventory();break;
           case 3: editInventory(); break;
                       }
    
    
 

(This comment might well be all over the place, so forgive me if it adds to the confusion)

in main():
- is it a mistake to leave a dangling { (line 130 & 132)?
- can I suggest the flow like this:
program starts->
--check for instances of inventories,
--if there are none prompt user to create a new one
--if there is one, prompt the user whether he wants to read the current inventory or create a new one
etc.

in addInventory():
- there are some unused variables
- line 47 creates a file, but then in editInventory() you use a different file

in checkInventory():
- why are you defining editInventory() inside it?
- line 118: no the same file, ios::in, shouldn't it be ios::app?

in general, you should
- try harder in the design aspect of the program
- do input checking
checkinventory had that inside of it because i had to take some of the code out to beable to fit it all in ther ei can only have 8000 characters so i had to try to fit it all in there that is why some of it seems messed up
change this line:
(118)ofstream Inventory("inventory.txt", ios::in); just leave out ios::in, that should give correct output, the default ofstream constructor will already declare ios::out

some more suggestions:
- use array or better, use structs (and loops)
- the way the functions are designed leave a lot to be desired e.g (like I wrote earlier) it seems that you're using two unrelated files -figure a way so that the file that you use/create in addInventory() is the same file used in editInventory() and displayInventory()
Alrighty. They are both saving to the same file the thing is im getting rediculous outputs in editInventory() like if i already added Bags with the value of 300 i could go to check inventory and it would show correctly but when if go to editInventory() ill add 20 to the value and ill get like 200000 and that 200000 will replace the value in checkInventory(). but im not gona worry about it im just gona rewrite it using arrays and loops.
Topic archived. No new replies allowed.