Thread: Review my Program

  1. #1
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719

    Review my Program

    Here is a simple inventory program I made, please review it, and give me comments on how it could be fixed. Try not to add too much to it, just make it better.

    Code:
    //Keeps Track of a stores inventory
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
         char id[40];
         char name[40];
         char quanity[40];
         const static char filename[]="inven.txt";
         cout << "Product #:" << endl;
         cin.getline(id,40);
         cout << "Product Name:" << endl;
         cin.getline(name,40);
         cout << "Product Quanity:" << endl;
         cin.getline(quanity,40);
         ofstream inventory("inven.txt", ios::app);
         if(inventory.bad())
         {
                            cerr << "Error opening file" << filename << endl;
                            system("PAUSE");
                            }
         else if(inventory.good())
         {
                               cerr << "The data has been SUCESSFULLY added to" << " " << filename << endl << endl;
                               }
         
         inventory << id << name << quanity;
         
         ifstream a_file("inven.txt",ios::app);
         a_file >> id >> name >> quanity;
         cout << "Prouduct #:" << id << endl;
         cout << "Product Name:" << name << endl;
         cout << "Product Quanity:" << quanity << endl;
         cin.get();
         }
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    If you do this:

    inventory << id << name << quanity;

    you may well have difficulty reading back from the file as I'm not completely sure that the terminating null char is read to the file and even if it is, I'm not sure that >> will stop input at the null char because I don't remember the null char being considered whitespace. To be safe I'd use a comma delimited file system or delimit by some char other than null. If your current code words for you, fine, but I'm somewhat doubtful.

    I'd also read the file with the same istream method that I obtained user input to be put into the file. Thus if I used getline() to get the product ID from the user, I'd use getline() to read it from the file, too. Using the delimiting char for the terminating char of getline() should work quite well, if you go that route.
    You're only born perfect.

  3. #3
    Banned
    Join Date
    Jun 2005
    Posts
    594
    also you should look into strings
    if your gonna do c++

  4. #4
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Code:
    //Keeps Track of a stores inventory
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
         char id[40];
         char name[40];
         char quanity[40];  // I dont think theyll have a 40 digit number aka waste of space 5-10 at the most
         const static char filename[]="inven.txt"; // Why static?
         cout << "Product #:" << endl;
         cin.getline(id,40);
         cout << "Product Name:" << endl;
         cin.getline(name,40);
         cout << "Product Quanity:" << endl;
         cin.getline(quanity,40);
         ofstream inventory("inven.txt", ios::app);
         if(inventory.bad())
         {
                            cerr << "Error opening file" << filename << endl;
                            system("PAUSE");
                            }
         else if(inventory.good())
         {
                             cerr << "The data has been SUCESSFULLY added to " <<  filename << endl << endl;
                            }
         
         inventory << id << name << quanity; // Not sure if its going to start on a new line for everyone of the variables
         
         ifstream a_file("inven.txt",ios::app);
         a_file >> id >> name >> quanity;
         cout << "Prouduct #:" << id << endl;
         cout << "Product Name:" << name << endl;
         cout << "Product Quanity:" << quanity << endl;
         cin.get();
         }
    My Website
    010000110010101100101011
    Add Color To Your Code!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. review my telephone network simulation program
    By debuger2004 in forum C Programming
    Replies: 3
    Last Post: 06-20-2003, 01:26 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM