CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Threaded View

  1. #1
    Join Date
    Feb 2003
    Posts
    87

    Question Can someone please help - my program compiles but I get a assertion error

    I have this code which compiles but I get an assertion error when I run the application:

    It is to do with the file I have decalred f1. I need a variable for a file that will allow me to read and write to it because I am manipulating the file position. I have listed my code below.

    Can someone please help.
    Code:
    #include <windows.h>
    #include <conio.h>
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    struct PAYROLL {
    	char fname[20];
    	char lname[20];
    	float pay;
    	char password[20];
    }x;
    
    
    void enter_password();
    void clear_screen();
    void MakeArrayBlank();
    char Name[20];
    char LName[20];
    
    string FileName;
    
    float payment;
    bool RunMore;
    HKEY hKey;
    char szBuf[10];
    bool RunOnce;
    char val[10];
    char c;
    char pass[10];
    int counter;
    
    fstream f1 (FileName.c_str(),ios::in || ios::app);
    
    	  
    bool Isthere =false;
    
    void main ()
    {
    
    
      DWORD dwSize=0;
      DWORD dwValue;
      DWORD valType =0;
      DWORD dwLen = sizeof(val);
    
      f1.clear();
      strcpy(Name,"");
      strcpy(LName,"");
      strcpy(x.fname,"");
      strcpy(x.lname,"");
      strcpy(x.password,"");
    
      x.pay=0;
    
    
      
      int choice;
      int counter=0;
    
      char password[20];
       
    //open registry to look for a key
    
      strcpy(szBuf,"true");
    
      dwSize = sizeof(dwValue);
    
    
      if(::  RegOpenKeyEx(HKEY_LOCAL_MACHINE,
    	  "system",
    	  0,
    	  KEY_SET_VALUE | KEY_QUERY_VALUE,
    	  &hKey)==ERROR_SUCCESS)
      
         long rc = RegQueryValueEx(hKey,
    				"PayRoll Flag",
    				NULL,
    				&valType,
    				reinterpret_cast<unsigned char *>(val),
    				&dwLen);
      
    
      if (strcmp (val,"true") ==0)
    	   RunOnce = false;
       else
    	   RunOnce = true;
    
       ::RegCloseKey(hKey);
    
       cout << RunOnce;
    
       if (RunOnce == true) //write the key if first time it is run
       {
    		
    	  if(::  RegOpenKeyEx(HKEY_LOCAL_MACHINE,
    	  "system",
    	  0,
    	  KEY_SET_VALUE | KEY_QUERY_VALUE,
    	  &hKey)==ERROR_SUCCESS)
    	  {
    		  if (::RegSetValueEx(hKey,
    			  "PayRoll Flag",
    			  0,
    			  REG_SZ,
    			  (LPBYTE) szBuf,
    			  strlen(szBuf) + 1))
    		  {
    			  ::RegCloseKey(hKey);
    				return;
    		  }
    	  }
    
       }
    
    
    
    
    
      FileName = "c:/hello.dat";
      
      f1.open(FileName.c_str(),ios::app);
      f1.write((char*)&x,sizeof(x));
               //writes to disk
      f1.clear();
      f1.close();
      
      
      cout << "**************************************************";
      cout << "\n \n \n \n";
      cout << "Enter your first name : ";
      fflush(stdin);
      gets (x.fname);
      cout << "Enter your last name : ";
      fflush(stdin);
      gets (x.lname);
      
      strcpy(Name,x.fname);
      strcpy(LName,x.lname);
    
    
    if (RunOnce == false)
    {
      Isthere=false;
      f1.clear();
      f1.clear();
      f1.open(FileName.c_str(),ios::in); //opens file for reading
      f1.read((char*)&x,sizeof(x)); //reads file
     
    
      while (!f1.eof() || !Isthere==true)
      {
         if ((strcmp(x.fname,Name) == 0) && (strcmp(x.lname,LName) ==0))
    	 {
    		 Isthere=true;
    		
    	 }
        else
    	 f1.read((char*)&x,sizeof(x));
      }
    
      f1.close();
    
      if ((Isthere == true) && (strcmp(x.password,"") !=0))
      {
         
    	  f1.clear();
    	  f1.open(FileName.c_str(),ios::in); //opens file for reading
          f1.read((char*)&x,sizeof(x)); //reads file
    
    	   cout <<"\n" "\n" "\n" <<"Enter your password : ";
    	  cin >> password;
         
          if ((strcmp(x.password,password) ==0)) 
    	  {
    	  clear_screen();
    	  cout <<"x.password is : "  <<x.password <<endl;
    	  cout <<"password is : " << password << endl;
    	  cout <<"x.fname is : " << x.fname << endl;
    	  cout <<"Name is : " << Name << endl;
    	  cout <<"x.lname is : " << x.lname << endl;
    	  cout <<"LName is : " << LName << endl;
    	  _getch();
    	  }
    	
      
       else
       {
    	 
    
    
    	  if (Isthere==false)
    	  {
             cout << "\n" <<"You must enter the correct password ";
    	     cout << "\n" <<"You have 3 attempts";
             cin >> password;
    
    		 counter = counter+1;
             if (counter==3) 
    		 {
    			 exit(1);
    		 }
    	  }
       }
      }
      else
    	 {
    	  cout <<"x.password is : "<< x.password;
    	  cout <<"\n" "\n" "\n" "You must enter a password now : ";
    	  cin >> x.password;
    	  f1.clear();
          f1.open(FileName.c_str(),ios::app);
    	  f1.write((char*)&x,sizeof(x)); //write file
    cout <<"x.password is : " << x.password;	
      }
      
      
      
    }
    
    if (RunOnce == true)
    {
    enter_password();
    
    f1.open(FileName.c_str(),ios::app);
    f1.write((char*)&x,sizeof(x)); //write file
    f1.clear();
    f1.close();
    cout << x.fname;
    }
    
      Isthere=false;
     f1 << x.fname;
     f1 <<"Yo!!!";
    
     f1.open(FileName.c_str(),ios::in); //opens file for reading
     f1.read((char*)&x,sizeof(x)); //reads file		
     
    
    clear_screen();
    
      do {
    
     //ERROR NEED WHILE LOOP TO FIND END OF FILE
    
      while (!f1.eof() && !Isthere==true)
      {
         cout <<x.fname;
    	  if ((strcmp(x.fname,Name) == 0) && (strcmp(x.lname,LName) ==0))
    	 {
    		 Isthere=true;
    	 }
        else
    	 f1.read((char*)&x,sizeof(x));
      }
    
      f1.close();
    
      cout <<"******************************";
      cout <<"\n" <<"This session is for";
      cout <<"\n" <<x.fname <<" " << x.lname;
      cout <<"\n" "\n";
    
      cout  <<" \n "<< "1 : Enter Pay";
      cout << "\n "<<  "2 : Display your details";
      cout << "\n " << "3 : Change the path to install information";
      cout <<" \n " << "9 : Quits the program";
      cout <<"\n" "\n" "\n";
    
      cin >> choice;
      
      if (choice == 1) 
      {
    	   
    	  f1.open(FileName.c_str(),ios::app);
    
     cout <<"\n" << "Enter pay : " ;
     cin >> x.pay;
    
     f1.write((char*)&x,sizeof(x));
     f1.close();
     exit(1);//exit program - another fix would be to set choice to 9
      }
    
     else if (choice == 2)
     {	 
         Isthere = false;
    	 clear_screen();
    	
    	 
    		 if ((strcmp(x.fname,Name)==0) && (strcmp(x.lname,LName)==0))
    		 {
                Isthere=true;
    			 payment=x.pay;
    	        strcpy(Name,x.fname);
    			strcpy(LName,x.lname);
    			fflush(stdin);
    			cout <<"\n" <<"The following information is from disk";
                cout <<"\n" <<"**************************************************";
                cout <<"\n \n" <<"Your name : " << Name<< " " <<LName;
               	cout <<"\n";
    			cout <<"Your payment is : ";
    			cout <<payment;
                cout <<"\n""\n""\n""\n";
    			while (choice !=1)
    			{
    				cout<<"Enter 1 to return to the menu : ";
    				cin >> choice;
    					if (choice == 1)
    					{
    						clear_screen();
    					}
    			}
    
    			
    		 }
    	
        
     }
    
     else if (choice ==3)
     {
    	 cout <<"The installation path at the moment is : ";
    	 cout <<FileName;
    	 cout <<"Enter new installation path please :";
    	 cin >> FileName;
    	 cout <<"\n" "\n" "\n";
    	 cout <<"The path has been changed to : ";
    	 cout << FileName;
     }
     
     
      }
      while (choice !=9);
    	 
      return;
    }
    
    
    void enter_password()
    {
    	clear_screen();
    
    		cout << "Enter a password - max 10 characters";
    
    		c=getch();
    
    		while (c!='\r' && c!='\n' && counter!=10)
    		{
    
    			cout <<"*";
    			c=getch();
    			pass[counter] = c;
    			counter++;
    		}
            strcpy(x.password,pass);
    
    		f1.open(FileName.c_str(),ios::in);
    		f1.read((char*)&x,sizeof(x));
    
    		Isthere=false;
    
    		while(!f1.eof() || !Isthere==true)
    		{
    			if ((strcmp(x.fname,Name) == 0) && (strcmp(x.lname,LName) ==0))
    			{
    				Isthere=true;
    				ios::pos_type mark = f1.tellg();
    				f1.open(FileName.c_str(),ios::app);
    				f1.write((char*)&x,sizeof(x));
    				f1.clear();
    				f1.seekg(mark);
    				f1.close();
    
    			}
    		}
    
    
    }
    
    void clear_screen()
    {
    	system("CLS");
      
    	return;
    }

    [ADMIN] Added code tags...
    Last edited by Andreas Masur; May 14th, 2003 at 11:37 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured