can someone please explain me this piece of code

//say getdetail() is a function declared above

  f.write((char *) & a,sizeof(a));

Recommended Answers

All 8 Replies

So the first 3 lines are a breeze.

Line 1 is a comment.
Line 2 is a blank.
Line 3 is a call to a function above.
Line 4 is a call to some function that we don't have the code to so you would be reading what that function does with what you passed it.

I have to ask why you shared just these 4 lines. WIthout the rest it's not possible to go deep.

Assuming that line 4 is the contents of the function on line three, then your code is totally bogus. Is 'a' an instance of some class, and getdetail() a member function of that class? SHOW YOUR CODE!

I'm thinking that line 4's write may be ostream's write:

If so, f would be an ostream and line 4 writes the contents of object a to a file or to a socket or wherever. It would be consistent with common usage of ostream's write function, plus people often name ofstreams f when saving to a file.

All this is speculation, as you both pointed out, without seeing more code.

this is the whole code, I had a project from school to make a railway management programme, I took help from a friend of mine who sent me this code; Unfortunately he has gone out on a vacation, so I needed your help....
PS:- it is incomplete

void staff();
void user();
class detail
 int tno;          //tno   -> Train Number
 char tname[30];   //tname -> Train Name
 char bp[20];      //bp    -> Boarding Point
 char dest[20];    //dest  -> Destination
 int c1,c1fare;    //c1    -> First class        c1fare -> First class fare
 int c2,c2fare;    //c2    -> Second class       c2fare -> Second class fare
 int d,m,y;        //d -> date , m -> month , y -> year

 void getdetail()
  cout<<"Enter the details as follows"<<endl<<endl;
  cout<<"Train number                 : ";
  cout<<"Train name           : ";
  cout<<"Boarding point        : ";
  cout<<"Destination point           : ";
  cout<<"No of seats in first class "<<endl;
  cout<<"and fare per ticket        : ";
  cout<<"No of seats in second class "<<endl;
  cout<<"and fare per ticket       : ";
  cout<<"Date of travel(in dd/mm/yy)  : ";cin>>d>>m>>y;
 void displaydetail()
  cout<<"Tr_No | Tr_Name | Start | Destin. |";
  cout<<" FC_se | SC_se | Fr_FC | Fr_SC | Date of Jo.";
  cout<<tno<<"   "<<tname<<"  "<<bp<<"  "<<dest<<"    "<<c1<<"     "<<c2;
  cout<<"     "<<c1fare<<"      "<<c2fare<<"      "<<d<<"/"<<m<<"/"<<y<<endl<<endl;

void main()
    char a=219,b;

cout<<a<<"--------RAILWAY SYSTEM MANAGEMENT--------"<<a<<endl;
for(int i=0;i<43;i++)
cout<<endl<<a<<"               1.Staff                   "<<a<<endl;
cout<<a<<"               2.User                    "<<a<<endl;
cout<<a<<"               3.Exit                    "<<a<<endl;
for(int i=0;i<43;i++)
cout<<"Enter your choice:- ";
  case 1:

  case 2:

  case 3:


void staff(){
    int c = 0;
   char pass[6];
   char r[6] = "abcdef";
   int p;
   cout << "Password: ";
   for ( int i = 0;i < 6;i++ )
      pass[i] = getch();
      cout << "*";
   for ( int j = 0;j < 6;j++ )
      if ( pass[j] == r[j] )
         c = c + 1;

   if ( c == 6 )
       char d;
  cout<<"X               ADMINISTRATOR MENU                X \n";
  cout<<"\n\n1. Add details\n";
  cout<<"\n2. Display details\n\n3. User management\n";
  cout<<"\n4. Display passenger details\n\n5. Return to main menu\n";
  cout<<"\nEnter your choice : ";
  case 1:"det.txt",ios::in|ios::out|ios::binary|ios::app);
  f.write((char *) & a,sizeof(a));
  cout<<"\n\nDo you want to add one more record?\n";
  cout<<"y-for Yes\nn-for No\n";
  cout<<"Your answer : ";

      cout << "Invalid Password";


No #include statements. No using namespace std or std:: code in there, so good luck getting cout and cin to work. I believe I was correct. f is supposed to be an ofstream, though it's not declared anywhere. The ios::in should be taken out since f is clearly used for output, not input.

Line 51: char a = 219;

219? That's an odd value for a character. Out of range too if it's 8 bits. 8 bit char would have a range from -128 to 127. And even if it's in range, what is 219 supposed to represent?

The formatting is terrible. You need to indent this code.

Look at lines 97 and 98.

   if ( c == 6 )
       char d;

I seriously doubt that's intentional. I imagine there is supposed to be a starting bracket in there beforechar d but there isn't one, so if c equals 6, create a variable d of type char and then do absolutely nothing with it since it is out of scope by line 99 due to a lack of an opening bracket.

Presumably the a in line 118 was supposed to be an instance of class detail, but where is it declared? Nowhere.

Lines 123 and 124. There's that character d that was declared on line 98 and is now out of scope because of a lack of brackets.

My advice? Start from scratch. Your friend did you no favors giving you this code. As far as him explaining it, he'll explain it as I did above (i.e. pointing out the bugs) or he'll explain what he WANTED it to do, but clearly doesn't know how. It's not worth "figuring out", so either do it yourself from scratch or copy off someone else who is a better programmer.

thanks a lot for your input.
The thing is that in our school we are told to use Turbo c++ (i have no idea why, please don't ask me either :p).
I missed the header files because currently I am doing the code in notepad++, then I'll open it using turbo c++ where i'll add all the basic requirements.
I'm using

  char a=219;

to display a special character....

Anyhow, thanks for pointing out the mistake made with the brackets :p

Lines 81 to 98. You're using C++, not Assembly. Even if you were programming in C, you don't need to program that low-level (i.e. enter and check password one character at a time). Do this...

string password == "abcdef";
string user_entry;
cout << "Password: ";
cin >> user_entry;
if(password == user_entry)
    // code for correct password here.  Note the brackets.

thanks i'll give that a try

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.