Thread: Problem with reading writing in file(no, not that simple)

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    3

    Question Problem with reading writing in file(no, not that simple)

    Hello guys,I just beginner(maybe a bit more than that) at C++ with its basic knowledge.

    From what what i know, i tried to make something like facebook, the program isnt complete yet but it isnt working as it is supposed to.
    Lets say i open the program and create two accounts, account no.1 and account no.2, then if i login from account 1 and logout, i am not able to login from account 2, and if i login from account 2 and logout, i am not able to login from account 1.

    I've been listening to OneD and staring at the screen like a fool for about an hour now looking for that one mistake i am making

    So either am making such big mistake that am not able to completely see it or such a small one that am missing it, hope you guys could help.

    (Any suggestions about the code are always welcome)

    Code:
    #include<iostream.h>#include<stdlib.h>
    #include<stdio.h>
    #include<fstream.h>
    #include<string.h>
    #include<conio.h>
    struct date
    {
     int date;
     int monthno;
     int year;
     char month[15];
    };
    class Account
    {
     char password[30];
     char sec_ans[30];
    
    
     public:
      char status[150];
      char email[40];
      char name [30];
      char sec_ques[60];
      date b_day;
      void getdata();    //To be used when creating new account
      void open();
      int comp(char id[],char pass[]);
      void change();     // To change account info
      void Upd_stat();
      void getbdate();
      void change_pass();
      void change_mail();
    }acc,temp;
    void Account :: Upd_stat()
    {
     clrscr();
     cout<<"Enter status : ";
     gets(status);
     cout<<"\nStatus updated successfully!";
    }
    void Account :: change_mail()
    {
     clrscr();
     int exist=0;
     char id[40];
     cout<<"Enter new email address : ";
     gets(id);
     ifstream fin("facebuk.dat",ios::in);
     fin.seekg(0);
     while(!fin.eof())
     {
      fin.read((char*)&temp,sizeof(temp));
      if(strcmp(id,temp.email)==0)
      {
        exist=1;
        if(strcmp(id,email)==0)
         cout<<"That is your currecnt email address!";
        else
         cout<<"Account with the email address already exists";
      }
     }
     if(!exist)
     {
      strcpy(email,id);
      cout<<"\nE-mail address changed Successfully!";
     }
     fin.close();
    }
    void Account :: change_pass()
    {
     char pass[30],conf[30];
     clrscr();
     cout<<"Enter your current password : ";
     gets(pass);
     if(strcmp(pass,password))
     {
      cout<<"Wrong password!";
      return;
     }
     do
     {
      cout<<"Enter new password : ";
      gets(pass);
      cout<<"Confirm new password : ";
      gets(conf);
      if(strcmp(pass,conf))
        cout<<"Passwords do not match! Try agian!\n";
     }while(strcmp(pass,conf));
     strcpy(password,pass);
     cout<<"\nPassword canged successfully!";
    }
    void Account :: getbdate()
    {
     int wrong;
     cout<<"Enter Date of Birth :\n";
     do
     {
      wrong=0;
      cout<<"Enter Date(1-31) : ";
      cin>>b_day.date;
      if(b_day.date<1 || b_day.date>31)
      {
        cout<<"Invalid Date!\n";
        wrong=1;
      }
     }while(wrong);
     do
     {
      wrong=0;
      cout<<"Enter month(1-12) : ";
      cin>>b_day.monthno;
      switch(b_day.monthno)
      {
        case 1 : strcpy(b_day.month,"January");
                    break;
        case 2 : strcpy(b_day.month,"February");
                    break;
        case 3 : strcpy(b_day.month,"March");
                    break;
        case 4 : strcpy(b_day.month,"April");
                    break;
        case 5 : strcpy(b_day.month,"May");
                    break;
        case 6 : strcpy(b_day.month,"June");
                    break;
        case 7 : strcpy(b_day.month,"July");
                    break;
        case 8 : strcpy(b_day.month,"August");
                    break;
        case 9 : strcpy(b_day.month,"September");
                    break;
        case 10 : strcpy(b_day.month,"October");
                     break;
        case 11 : strcpy(b_day.month,"November");
                     break;
        case 12 : strcpy(b_day.month,"December");
                     break;
        default : cout<<"Invalid month!\n";
                     wrong=1;
      }
     }while(wrong);
     cout<<"Enter year : ";
     cin>>b_day.year;
    }
    void Account :: change()
    {
     int ch,wrong=0;
     do
     {
      clrscr();
      cout<<"What would you like to change?\n";
      if(wrong)
        cout<<"\t\t\t\tINVALID CHOICE!\n";
      wrong=0;
      cout<<"- Change email address(Press 1)\n- Change Name(Press 2)\n- Change Password(Press 3)\n- Chnage Date of Birth(Press 4)\n";
      cin>>ch;
      switch(ch)
      {
        case 1 : change_mail();
                    break;
        case 2 : cout<<"Enter name : ";
                    gets(name);
                    cout<<"\nName changed successfully!";
                    break;
        case 3 : change_pass();
                    break;
        case 4 : getbdate();
                    cout<<"\nDate of birth changed successfully!";
                    break;
        default : wrong=1;
      }
     }while(wrong);
     getch();
    }
    void Account :: open()
    {
     int ch,logout;
     do
     {
      logout=0;
      clrscr();
      cout<<"\t\t\t\tWELCOME ";
      puts(name);
      cout<<"\n\nWhat would you like to do today?\n\n- Update Status(Press 1)\n- Change Account info(Press 2)\n- Logout(Press 3)\n";
      cin>>ch;
      switch(ch)
      {
        case 2 : change();
                    break;
        case 3 : logout=1;
                    break;
        case 1 : Upd_stat();
                    break;
        default : cout<<"\n\n\t\t\tINVALID CHOICE. TRY AGAIN!";
      }
     }while(!logout);
    }
    int Account :: comp(char id[40],char pass[30])
    {
     if(strcmp(id,email)==0 && strcmp(pass,password)==0)
      return 1;
     return 0;
    }
    void Account :: getdata()
    {
     clrscr();
     cout<<"\t\t\t\tCREATE AN ACCOUNT\\n\n";
     cout<<"Enter Name : ";
     gets(name);
     cout<<"Enter Email id : ";
     gets(email);
     getbdate();
     cout<<"Enter password : ";
     gets(password);
     cout<<"Enter a security question(In case you forget your password) : ";
     gets(sec_ques);
     cout<<"Enter the answer to your security question : ";
     gets(sec_ans);
    }
    void Profile(int pos)
    {
     ifstream fi("facebuk.dat",ios::in);
     fi.seekg(pos);
     fi.read((char*)&acc,sizeof(acc));
     fi.close();
     acc.open();
     ofstream fo("facebuk.dat",ios::out);
     fo.seekp(pos);
     fo.write((char*)&acc,sizeof(acc));
     fo.close();
     //ifstream fi("facebuk.dat",ios::in);
    }
    void login()
    {
     char id[40],pass[30],tr;
     int check,again,wrong;
     int pos;
     do
     {
      again=0;
      clrscr();
      cout<<"\t\t\t\t\tLOGIN\n\n";
      cout<<"Enter Email id : ";
      gets(id);
      cout<<"\nEnter password : ";
      gets(pass);
      ifstream fi("facebuk.dat",ios::in);
      fi.seekg(0);
      while(fi)
      {
        pos=fi.tellg();
        fi.read((char*)&acc,sizeof(acc));
        check=acc.comp(id,pass);
        if(check)
         break;
      }
      fi.close();
      if(!check)
      {
        cout<<"Incorrect Email and/or password.Try again?(y/n) ";
        do
        {
         wrong=0;
         cin>>tr;
         switch(tr)
         {
          case 'Y' :
          case 'y' : again=1;
                         break;
          case 'N' :
          case 'n' : again=0;
                         break;
          default : cout<<"Invalid . Try again.";
                        wrong=1;
         }
        }while(wrong);
      }
     }while(again);
     if(check)
      Profile(pos);
    }
    void create()
    {
     acc.getdata();
     ofstream fo("facebuk.dat",ios::out | ios::app);
     fo.write((char*)&acc,sizeof(acc));
     fo.close();
    }
    void forgot()
    {
     cout<<"\nSorry, program under construction!";
     getch();
    }
    void main()
    {
    int ch,exit=1,wrong=0;
    do
    {
     clrscr();
     cout<<"\t\t\t\tWELCOME TO FACEBUK\n\n";
     if(wrong)
     {
      cout<<"\t\t\t    INVALID CHOICE. TRY AGIAN.\n\n";
      wrong=0;
     }
     cout<<"- Login (Press 1)\n- Create a new account (Press 2)\n- Forgot Password? (Press 3)\n- Exit (Press 4)";
     cin>>ch;
     switch(ch)
     {
      case 1 : login();
                  break;
      case 2 : create();
                  break;
      case 3 : forgot();
                  break;
      case 4 : exit=0;
                  break;
      default : wrong=1;
     }
    }while(exit);
    }
    Last edited by hrsht; 07-10-2016 at 07:40 PM.

  2. #2
    Registered User
    Join Date
    Jul 2016
    Posts
    3
    This the code fragment where i THINK the problem might be,

    Code:
    int Account :: comp(char id[40],char pass[30])
    {
     if(strcmp(id,email)==0 && strcmp(pass,password)==0)
      return 1;
     return 0;
    }
    void Profile(int pos)
    {
     ifstream fi("facebuk.dat",ios::in);
     fi.seekg(pos);
     fi.read((char*)&acc,sizeof(acc));
     fi.close();
     acc.open();
     ofstream fo("facebuk.dat",ios::out);
     fo.seekp(pos);
     fo.write((char*)&acc,sizeof(acc));
     fo.close();
    }
    void login()
    {
     char id[40],pass[30],tr;
     int check,again,wrong;
     int pos;
     do
     {
      again=0;
      clrscr();
      cout<<"\t\t\t\t\tLOGIN\n\n";
      cout<<"Enter Email id : ";
      gets(id);
      cout<<"\nEnter password : ";
      gets(pass);
      ifstream fi("facebuk.dat",ios::in);
      fi.seekg(0);
      while(fi)
      {
    	pos=fi.tellg();
    	fi.read((char*)&acc,sizeof(acc));
    	check=acc.comp(id,pass);
    	if(check)
    	 break;
      }
      fi.close();
      if(!check)
      {
    	cout<<"Incorrect Email and/or password.Try again?(y/n) ";
    	do
    	{
    	 wrong=0;
    	 cin>>tr;
    	 switch(tr)
    	 {
    	  case 'Y' :
    	  case 'y' : again=1;
    					 break;
    	  case 'N' :
    	  case 'n' : again=0;
    					 break;
    	  default : cout<<"Invalid . Try again.";
    					wrong=1;
    	 }
    	}while(wrong);
      }
     }while(again);
     if(check)
      Profile(pos);
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you work through a somewhat more modern introduction to C++ such as Accelerated C++, which while dated with respect to C++11+, will still give you a very good introduction into how to effectively use C++.

    At the moment, the problems with your code are so plentiful that it is difficult to just gloss over them to help you debug your code. For example:
    • You use pre-standard headers: <iostream.h> and <fstream.h>
    • You are unnecessarily mixing up C++-style I/O with C-style I/O since you also include <stdio.h>
    • In addition to mixing with C-style I/O, you use a C I/O function that is inherently vulnerable to buffer overflow: gets
    • It is not clear why you chose to use C-style string functions over std::string.
    • You incorrectly control loops with fin.eof().
    • You don't seem aware of bool.
    • Your functions are not const-correct.
    • By putting interactive I/O right into your Account class, your member variables become little more than glorified global variables.


    Besides these, you should note that <conio.h> and related functions are non-standard, so think carefully if you really need them, and that void main should be int main.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jul 2016
    Posts
    3
    your post post makes me feel so small as if am non-existent...

    well i cant say i didnt expect an answer like that, cause when i look at codes online, they are alot different from what i was taught at school,we used TurboC++ 3.1, i guess(not sure), and did not have a good teacher either, you see, the code above is more or less everything we were taught at school packed into one.

    I guess i should look for some tutorials online to get a better idea and also find something better than turbo C++ 4.5 for my computer which i hear some people saying is soo old and bad that it should be banned (really?) :|

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it really is that old. For up-to-date compilers, you should look at Visual Studio (which is an IDE but comes packed with the compiler), gcc or clang. For IDEs, popular ones include Visual Studio and Code::Blocks. Of course, there are more.

    It might not be wrong to read an introductory book either, like Accelerated C++, just to get you started on the right path after that horrible good-for-nothing class. It's a shame schools still teach C++ using that compiler. They should be shamed. They are doing their students a disfavour.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading/writing to file
    By JonathanS in forum C Programming
    Replies: 2
    Last Post: 01-25-2012, 12:46 AM
  2. Replies: 1
    Last Post: 11-06-2011, 04:40 PM
  3. File reading/writing problem
    By click66 in forum C Programming
    Replies: 2
    Last Post: 02-08-2011, 08:14 AM
  4. Reading out of and writing into the same file
    By Wiretron in forum C Programming
    Replies: 8
    Last Post: 12-30-2006, 02:04 PM
  5. Replies: 6
    Last Post: 05-12-2005, 03:39 AM

Tags for this Thread