Getting the value of a variable from a file

I want to get the value of a variable from a file. How do I do that?

ofstream file1;
ifstream file2;
int with;
int newstock;
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);
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();
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?
Last edited on
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.

#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>
#include <conio.h>
#include <iomanip>
#include <time.h>

using namespace std;

void intro();
void welcome();
void login();
void menu1();
void location(int x, int y);
void menu2();

int main()
{
intro();
menu1();

system("pause>0");
}

void location(int x, int y)
{
HANDLE hStdout;
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;

hStdout=GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hStdout, &csbiInfo);
csbiInfo.dwCursorPosition.X=x;
csbiInfo.dwCursorPosition.Y=y;
SetConsoleCursorPosition(hStdout, csbiInfo.dwCursorPosition);
}

void welcome()
{
location(0,10);
cout <<setw(60)<<" "<<endl;
cout <<setw(60)<<" "<<endl;
cout <<setw(60)<<" "<<endl;
cout <<setw(60)<<" "<<endl;
cout <<setw(60)<<" 000 000 000000000 00 00000000 000000000 00 00 000000000 "<<endl;
cout <<setw(60)<<" 000 000 00 00 0 0 0 0 0 0 0 00 "<<endl;
cout <<setw(60)<<" 000 000 00 00 0 0 0 0 0 0 0 00 "<<endl;
cout <<setw(60)<<" 000 00 000 00000 00 0 0 0 0 0 0 0 00000 "<<endl;
cout <<setw(60)<<" 000 0000 000 00 00 0 0 0 0 0 0 0 00 "<<endl;
cout <<setw(60)<<" 00 00 00 00 000000000 0000000 00000000 000000000 00 0 0 00 000000000 "<<endl;
cout <<setw(60)<<" 00 00 000000000 0000000 00000000 000000000 00 000 00 000000000 "<<endl;
cout <<setw(60)<<" "<<endl;
cout <<setw(60)<<" "<<endl;
cout <<setw(60)<<" "<<endl;
cout <<setw(60)<<" "<<endl;
cout <<setw(60)<<" "<<endl;

Sleep(500);
system("cls");
}

void intro()
{
system("color 07");
welcome();
system("color 03");
welcome();
system("color 01");
welcome();
system("color 02");
welcome();
system("color 0A");
welcome();
}

void menu1()
{
int option;

location(33,18);
system("color 0A");
cout << "Welcome to Shoes Stock!!!";
Sleep(500);
system("cls");
cout << "[1] - Login" << endl;
cout << "[2] - Exit" << endl;
cout << "Please choose: ";
cin >> option;
Sleep(250);
system("cls");

switch(option)
{
case 1:
{
login();
break;
}

case 2:
{
location(33,18);
system("color 0A");
cout << "Thank You!!!";
Sleep(1000);
system("cls");
break;
}
}
}

void login()
{

string user;
string myuser="geriel";
char pass[10];
string mypass="cruz";
int x=0;

here:

cout << "Enter Username: ";
cin >> user;
cout << "Enter Password: ";
int i=0;
while(1)
{
pass[i]=getch();
if(pass[i]==13)
{
pass[i]='\0';
break;
}
cout << "*";
i++;
}
Sleep(250);
system("cls");
x++;


if(user==myuser && pass==mypass)
{
system("color 0A");
location(35,18);
cout << "\aAccess Granted!";
Sleep(500);
system("cls");
menu2();
}

else
{
system("color 0A");
location(35,18);
cout << "\aAccess Denied!";
Sleep(500);
system("cls");

if(x!=3)
{
goto here;
}

else if (x==3)
{
system("color 04");
Beep(3000,1000);
location(35,18);
cout << "Intruder Alert!!!";
Sleep(2000);
system("cls");
}
}
}

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();
}
}
}
}


Topic archived. No new replies allowed.