-
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. :D
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();
}
-
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.
-
also you should look into strings
if your gonna do c++
-
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();
}