file2 >> newstock;
newstock = newstock - with;
cout << "The number of stocks as of " << ctime(&t) << " is: " << newstock << endl;
file1 << "The number of stocks as of " << ctime(&t) << " is: " << newstock << endl;
file2.close();
file1.close();
Your first step is to declare and initialize "firststock".
Then it would be a good idea to initialize "newstock" before you try to test against it's value.
What part isn't working? Does your source file exist? Did you remember to save any data that you might have entered into it? Is the call to "std::ifstream.open()" failing? Are you getting an error?
I initialized it in my program. This actually just a part of the whole program. The part that is not working is that I can't get the value of newstock from the text file.
Here is my whole program, though it's still incomplete. Sorry if it's too long. menu2() doesn't seem to work properly.
void menu2()
{
int option;
int stock;
int with;
int newstock=100;
int firststock=100;
int numstock;
cout << "[1] - Restock shoes" << endl;
cout << "[2] - Withdraw shoes in stock" << endl;
cout << "[3] - Check the number of shoes in stock" << endl;
cout << "[4] - Exit" << endl;
cout << "Please choose from the menu: ";
cin >> option;
Sleep(250);
system("cls");
switch(option)
{
case 1:
{
ofstream file;
time_t t;
time(&t);
file.open("d:\\newstocks.txt", ios_base::app);
cout << "How many shoes are you going to stock? ";
cin >> stock;
newstock = firststock + stock;
cout << "The number of stocks as of " << ctime(&t) << " is: " << newstock << endl;
file << "The number of stocks as of " << ctime(&t) << " is: " << newstock << endl;
file.close();
break;
}
case 2:
{
ofstream file1;
ifstream file2;
time_t t;
time(&t);
cout << "How many shoes are you going to take? ";
cin >> with;
if(with>newstock || with>firststock)
{
cout << "Sorry you have insufficient amount of shoes in your stock!";
}
else if(with<newstock || with<firststock)
{
file2.open("d:\\newstocks.txt", ios::in | ios::out);
file1.open("d:\\newstocks.txt", ios_base::app);
file2 >> newstock;
newstock = newstock - with;
cout << "The number of stocks as of " << ctime(&t) << " is: " << newstock << endl;
file1 << "The number of stocks as of " << ctime(&t) << " is: " << newstock << endl;
file2.close();
file1.close();
}
}
}
}