Filestreaming Help

closed account (48CM4iN6)
Hey guys. My project is a reservation system. My problem is in filestreaming ( i think) . My objective that I can't figure out is to show that the cottage is reserved continously. My problem is after the 1st input, it no longer shows the "Reserved" previously. And when I run again the program, I can'y view the change in the availability in seats. Thank you for any help you could give! I'm still a beginner

Here's my code: (you can run it to see)

#include<iostream>
#include<iomanip>
#include<fstream>
#include<conio.h>
#include<windows.h>
#include<string>
#include<cstdlib>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

void admenu();
void usermenu();
void password();
void ACottages(string);
void Cottages(int,int);
int Convert(char);
void Reserve();
void QuitProgram();
void ResetCottages();
void Header();

class ychoice{};
class nchoice{};
class nvalidp{};

struct reserve
{
char name[100];
int ncottage;

};

/**********************************/

string EnterPassword()
{
string NumAsString="";
char ch=getch();//h
while (ch!='\r'){
cout<<'*';//***
NumAsString+=ch;//"hello"
ch=getch();//

}

return NumAsString;

}

/**********************************/
/**********************************/

void main()
{
int ulit=0;
char ans;
bool flag=true;

while(flag)
{
system("cls");
Header();
cout<<"[1]User \n"
<<"[2]Administrator\n"
<<"Choice: ";
cin>>ans;

ans=tolower(ans);

switch(ans)
{
case '1':
usermenu();
flag=false;
break;

case '2':
// password();
system("cls");
admenu();
flag=false;
break;

}//end switch

}//end while
}//end main

/**********************************/
/**********************************/

void password()
{
int ulit=0 , attempts=0;
bool flag=true;

system("cls");
Header();

cout<<"Welcome Administrator!\n"
<<"password:";

while(flag)
{
string password=EnterPassword();

try
{
if(password=="hello")
{
cout<<"\nprocessing your password";

do
{
Sleep(1000);
cout<<".";
ulit++;

}while(ulit<2);
flag=false;

}//end if

else
throw nvalidp();


}//end try

catch (nvalidp)
{
cout<<"\nprocessing your password";
Sleep(1000);
cout<<".";

system("cls");

cout<<"Wrong Password!"<<endl;
cout<<"password:";

attempts++;

if (attempts>=3)
{
flag=false;
system("cls");
cout<<"You Have Exceeded the Attempts Allowed \n"
<<"Program Terminated \n";
exit(1);


}//end if

continue;
}//end catch nvalidp
}//end while

}//end password;

/**********************************/

void admenu()
{
char choice;
string AvailableCottages;
bool flag=true;


while(flag)
{
system("cls");
Header();

cout<<" ==========ADMINISTRATOR MENU==========\n";
cout<<" [1] Available cottages \n"
<<" [2] Reset Available\n"
<<" [3] Quit \n"
<<" Please enter your choice:";
cin>>choice;
choice=tolower(choice);
switch(choice)
{

case '1':
ACottages(AvailableCottages);
flag=false;
break;
case '2':
ResetCottages();
flag=false;
break;
case '3':
QuitProgram();
flag=false;
break;

}//end switch

}//end while
}//end Admenu

/********************************/

void usermenu()
{
char select;
int x=0,y=0;
bool flag=true;
string Acall;

while (flag)
{
system("cls");
Header();

cout<<" ========Welcome Guest=======\n"
<<" [1] View available cottages \n"
<<" [2] Reserve a cottage \n"
<<" [3] Quit \n"
<<" Please your choice:";
cin>>select;
select=tolower(select);
switch(select)
{

case '1':
ACottages(Acall);
flag=false;
break;
case '2':
Reserve();
flag=false;
break;
case '3':
QuitProgram();
flag=false;
break;


}//end switch


}//end while
}//end usermenu

/********************************/

void Cottages(int ColLetter, int NumRows)
{

{
int x,y;
int z=1;
ofstream fout;
string letter = "abcde";
string stat="Available";
string res="Reserved ";

Header();

fout.open("cottages.txt");

for(x=0; x < 5;x++)
fout<<setw(14)<<letter.at(x);

fout<<endl;

for(y = 0; y<8; y++)
{
fout<<" "<<y+1;

for(x=0;x<5;x++)
{
if( ( ColLetter == (x+1) ) && (NumRows == (y+1) ) )
fout<<" [ "<<res<<" ]";
else
fout<<" [ "<<stat<<" ]";
z++;
}//end inner for

fout<<endl;
}//end outer for

ifstream instream("cottages.txt");
char ch;

while(!instream.eof())
{
instream.get(ch);
cout<<ch;
}

instream.close();
}


}//end Cottages

/********************************/


void Reserve()
{
reserve c;
char cInput;
int nInput,i;
int x=0,y=0;
int X,Y;
int z=1;
string letter = "abcde";
string areceive;

ofstream fout;


system("cls");
Header();


fout.open("cottages.txt");
ACottages(areceive);

cout<<"\nHow many cottages would you like to Reerve? [1-40] : ";
cin>>c.ncottage;

for(i=0;i<c.ncottage;i++)
{



cout<<"\nEnter The Cottage Number You Want to Reserve ["<<i+1<<"/"<<c.ncottage<<"]\n"
<<"[letter number] :";
cin>>cInput>> nInput;

X = Convert(cInput);
Y = nInput;

Cottages(X,Y);

cout<<" Your Cottage is at: "<<cInput<<" "<<Y<<endl;

}//end outer for


fout.close();


}//end Reserve


/********************************/

void ACottages(string available)
{
string stat="Available";
string letter = "abcde";
available=stat;
int x,y;
int z=1;
ofstream fout;

available=stat;

fout.open("cottages.txt");
Header();

for(x=0; x < 5;x++)
fout<<setw(14)<<letter.at(x);

fout<<endl;

for(y = 0; y<8; y++)
{
fout<<" "<<y+1;

for(x=0;x<5;x++)
{
fout<<" [ "<<available<<" ]";
z++;
}//end inner for

fout<<endl;
}//end outer for;

ifstream instream("cottages.txt");
char ch;

while(!instream.eof())
{
instream.get(ch);
cout<<ch;
}

instream.close();


}//end ACottages

/********************************/

void ResetCottages()
{
cout<<endl;

}//end ResetCottages


/********************************/

void QuitProgram()
{
char ans;
do
{
try
{
cout<<"Do You Really Want to Quit ? [y/n]";
cin>>ans;

ans=tolower(ans);

if (ans=='y')
throw ychoice();
else if (ans=='n')
throw nchoice();

}//end try

catch(ychoice)
{
system("cls");
cout<<"Program Terminated"<<endl;
exit(1);

}//end catch ychoice

catch(nchoice)
{
main();


}//end catch nchoice


}while((ans!='y')||(ans!='n'));

}//end QuitProgram

void Header()
{
system("cls");
cout<<" ***********************************************************************\n"
<<" * *\n"
<<" * RESORT R E S E R V A T I O N *\n"
<<" * *\n"
<<" ***********************************************************************\n";
cout<<endl;
cout<<endl;
}

int Convert(char cLetter)
{
cLetter = toupper(cLetter);
switch(cLetter)
{
case 'A':
return 1;
break;
case 'B':
return 2;
break;
case 'C':
return 3;
break;
case 'D':
return 4;
break;
case 'E':
return 5;
break;

}
}
In your reserve() function you open and close a file but never write anything to it in between.
closed account (48CM4iN6)
How do I solve it then?
Here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    fout.open("cottages.txt");
    ACottages(areceive);

    cout<<"\nHow many cottages would you like to Reerve? [1-40] : ";
    cin>>c.ncottage;

    for(i=0; i<c.ncottage; i++) {



        cout<<"\nEnter The Cottage Number You Want to Reserve ["<<i+1<<"/"<<c.ncottage<<"]\n"
            <<"[letter number] :";
        cin>>cInput>> nInput;

        X = Convert(cInput);
        Y = nInput;

        Cottages(X,Y);

        cout<<" Your Cottage is at: "<<cInput<<" "<<Y<<endl;

    }//end outer for


    fout.close();


Like iHutch said, you do nothing with fout other than open and close it...did you want to add data to it? Maybe like...fout<<" Your Cottage is at: "<<cInput<<" "<<Y<<endl;?
closed account (48CM4iN6)
I wanted that everytime I input the cottage number to show "Reserve". Everytime i input another one it does not show the Reserve I inputted earlier.
Why do you think this is a filestreaming issue? You aren't outputting to the file at all, you have no code that does such a thing. So aside from the file streaming, how about you walk me through, step-by-step, what your issue is. What are the expected results? What are the actual results?
closed account (48CM4iN6)
Sorry I'm still a beginner. The expected results should show "Reserve" for every input of the user . The problem in my code is that whenever the user inputs another cottage number, the output only displays the "Reserve" one at a time. It disregards previous inputs of the user.
You would need to set up an array to do that, store the information in the array and access each member individually to know if it's reserved or available. Looking through your code, you need to cut out a lot of things that aren't being used. It's making it hard to go through everything you have.
closed account (48CM4iN6)
Is this it that you are talking about? it displays the same result

void Cottages(reserve temp[], int ColLetter, int NumRows,int snum)
{

{
int z=1;
int i=0;
int x=0,y=0,w=0;
ofstream fout;
ifstream fin;

Header();

temp[i].stat="Available";
temp[i].res="Reserved ";

string letter="abcde";

fin.open("cottages.txt");
if (!fin.fail())
{
fout.open("cottages.txt");
for(i=0;i<snum;i++)
{


for(temp[i].cnumber=0; temp[i].cnumber < 5;temp[i].cnumber++)
fout<<setw(14)<<letter.at(temp[i].cnumber);

fout<<endl;

for(temp[i].rnumber = 0; temp[i].rnumber<8; temp[i].rnumber++)
{
fout<<" "<<temp[i].rnumber+1;

for(temp[i].cnumber=0;temp[i].cnumber<5;temp[i].cnumber++)
{
if( ( ColLetter == (temp[i].cnumber+1) ) && (NumRows == (temp[i].rnumber+1) ) )
fout<<" [ "<<temp[i].res<<" ]";
else
fout<<" [ "<<temp[i].stat<<" ]";
z++;
}//end inner for

fout<<endl;
}//end outer for

fout.close();

}//end outermost for

}//end if




ifstream instream("cottages.txt");
char ch;

while(!instream.eof())
{
instream.get(ch);
cout<<ch;
}

instream.close();

}


}//end Cottages

/********************************/


void Reserve()
{
reserve c[100];;
int i, x=0,y=0, num;
int colX,rowY;
int z=1;
ofstream fout;

system("cls");
Header();


ACottages();

cout<<"\nHow many cottages would you like to Reserve? [1-40] : ";
cin>>num;



for(i=0;i<num;i++)
{



cout<<"\nEnter The Cottage Number You Want to Reserve ["<<i+1<<"/"<<num<<"]\n"
<<"[letter number] :";
cin>>c[i].cletter>> c[i].rnumber;

colX = Convert(c[i].cletter);
rowY = c[i].rnumber;


Cottages(c, colX,rowY,num);

cout<<" Your Cottage is at: "<<c[i].cletter<<" "<<rowY<<endl;
cout<<endl;



}//end outer for



}//end Reserve
Ok, I'm going to explain this once. Please put any code you post into [code][/code] tags, it will format your code and keep you indentation and make it much easier to read.

Why are you using filestream? The idea behind filestream is to save the work someone did in your program this time, for next time. It looks like you're trying to use it for a storage center. That is a no no.

Filestream takes up a lot of memory when you constantly open and close files. This will slow your computer down.

In Cottages(), you have the function scope and a completely seperate scope, only one is required:
1
2
3
4
void Cottages(reserve temp[], int ColLetter, int NumRows,int snum)
{

{


What are you trying to do on this line?:
1
2
            for(temp[i].cnumber=0; temp[i].cnumber < 5; temp[i].cnumber++)
                fout<<setw(14)<<letter.at(temp[i].cnumber);


Here, you open the same file twice:
1
2
    fin.open("cottages.txt");
    ifstream instream("cottages.txt");


You also never close the fin. This can cause issues.

This isn't right:
reserve c[100];;

Here, you declare 1-40 cottages, but in the very beginning of the function you created an array of 100, why?:
 
    cout<<"\nHow many cottages would you like to Reserve? [1-40] : ";


It looks like you made a good attempt at using the arrays, but it's hard to understand the array of cottages, not your fault. However, it's very hard to test it with only two sections. Make it easy on me and post your entire code between code tags and I'll go through it line by line to see what the issue is.

Also, if you plan on using filestream, make a read() and write() function. Outside of here, there should be NO other filestream calls. I would also only use the write function right before the program closes. Think about notepad, what would happen to your computer if it closed down the file and reopened it every time you typed 1 letter?
Topic archived. No new replies allowed.