Inventory System
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Inventory System

  1. #1
    Join Date
    May 2011
    Posts
    2

    Inventory System

    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.

    Lines 81 - 148.
    Code:
    #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()
    {
         string Item1, Item2, Item3, Item4, Item5, choice5;
         unsigned int choice, choice2, choice3, choice4, s,s1, s2, s3, s4, answer,answer1,answer2, answer3,answer4, answer5;
                         system("cls");         
                         ifstream filename("inventory.txt");
                         if (filename.is_open()){
                                                 while(true){ 
                                                                        getline(filename, Item1);
                                                                        getline(filename, Item2);
                                                                        getline(filename, Item3);
                                                                        getline(filename, Item4);
                                                                        getline(filename, Item5);
                                                                        if (!filename.good()) break;
                                                                        cout << Item1 << ": " << endl;
                                                                        cout << Item2 << ": " << endl;
                                                                        cout << Item3 << ": " << endl;
                                                                        cout << Item4 << ": " << endl;
                                                                        cout << Item5 << ": " << endl;
    
                                                                        }
                                                                        filename.close();
                                                 }else { 
                                                       cout << "\nNo Inventory Added\n";
                                                       }    
    }
    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()
    {
        cout << "Login" << endl;
        int exit;
        string user, password;
        cout << "\nUsername: ";
        cin >> user;
        cout << "\nPassword: ";
        cin >> password;
        if (user == "Darkroom" && password == "bluemoon"){
                 cout << "Access Granted" << endl;
                 cout << "Logging on..." << endl;
                 Sleep(1000);             
                 }else {
                       cout << "Accessed Denied Please Exit the program." << endl;
                       cout << "Press 1 to Exit the Program.\t";
                       cin >> exit;
                       if (exit == 1){
                                std::exit(EXIT_FAILURE);       
                       }
                 }  
        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;
                           }
        
        
        
        cout << "\n\n\n\n1.Main Menu\n";
        cout << "2.Exit \n";
        cin >> choice2;
    }while (choice2 ==1);
        if(choice2 == 2){
        std::exit(EXIT_FAILURE);
                        }              
        char response;
        cin >> response;
        return EXIT_SUCCESS;
    }

  2. #2
    Join Date
    Dec 2004
    Location
    Ann Arbor, MI
    Posts
    281

    Re: Inventory System

    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
    --EJMW

  3. #3
    Join Date
    May 2011
    Posts
    2

    Re: Inventory System

    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.

  4. #4
    Join Date
    Aug 2009
    Posts
    439

    Re: Inventory System

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center