|
-
May 14th, 2003, 05:58 AM
#1
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.
-
May 14th, 2003, 07:21 AM
#2
The problem lies in this line.
fstream f1 (FileName.c_str(),ios::in || ios::app);
When I comment out the rest of the line
fstream f1;// (FileName.c_str(),ios::in || ios::app);
The program runs.
Also
FileName = "c:/hello.dat";
should be
FileName = "c:\\hello.dat";
Last edited by Tom Frohman; May 14th, 2003 at 07:24 AM.
Verere testudinem! (Fear the turtle)
Once you can accept the universe as matter expanding into nothing that is something, wearing stripes with plaid comes easy. -Albert Einstein
Robots are trying to steal my luggage.
-
May 14th, 2003, 07:39 AM
#3
Originally posted by Tom Frohman
Also
FileName = "c:/hello.dat";
should be
FileName = "c:\\hello.dat";
Nope. The filename given by the OP is perfectly valid.
In ANSI C and C++, the forward slash can be used as the directory seperator, regardless of what the underlying OS uses as a directory seperator. For standard I/O functions and stream classes, the forward slash is preferred, since it is universally accepted. Try it and you'll see it works perfectly.
Regards,
Paul McKenzie
-
May 14th, 2003, 08:16 AM
#4
Originally posted by Paul McKenzie
Nope. The filename given by the OP is perfectly valid.
In ANSI C and C++, the forward slash can be used as the directory seperator, regardless of what the underlying OS uses as a directory seperator. For standard I/O functions and stream classes, the forward slash is preferred, since it is universally accepted. Try it and you'll see it works perfectly.
Regards,
Paul McKenzie
Well, you learn something new everyday.
Verere testudinem! (Fear the turtle)
Once you can accept the universe as matter expanding into nothing that is something, wearing stripes with plaid comes easy. -Albert Einstein
Robots are trying to steal my luggage.
-
May 14th, 2003, 09:56 AM
#5
Thanks for your feedback guys. However when I comment out the line the program works but it does not create a file.
I used to use ifstream and ofstream and I got the program to read and write. But I need to manipulate the pointer position of the file and I have seen the bit of code that you had said to comment out in a book.
I could go back to using ifstream and ofstream but how do I manipulate the pointer of the file position because I want to overwrite records in this program.
Thanks in advance for your help.
-
May 14th, 2003, 11:35 AM
#6
I just looked at the mentioned line...you are declaring a global variable named 'FileName'. At this point the variable does not contain anything. Then you are trying to create a global stream object named 'f1' and try to open the file with the filename stored in 'FileName'. However, at this point the filename was not set at this point. Later in your code you are explicitely trying to open the file by using the method 'open()'... 
So...basically I would suggest to skip the first constructor call as mentioned by Tom...
-
May 14th, 2003, 12:19 PM
#7
Hi Andreas when i comment out that code for some reason my program doesn't produce the .dat file
As mentioned in my previous post I used ofstream and ifstream and it read and wrote records but now it doesn't.
Why is this?
-
May 15th, 2003, 07:05 AM
#8
I don't ever use streams but I was playing around with this program.
When I change
f1.open(FileName.c_str(),ios::app);
to
f1.open(FileName.c_str(),ios::out|ios::app);
,
it opens the file and writes to it. If I don't, it doesn't open the file and write to it.
It hangs up soon after this for me.
Like I say, I've never used streams don't know if I'm saying something ignorant or not.
Tom
Verere testudinem! (Fear the turtle)
Once you can accept the universe as matter expanding into nothing that is something, wearing stripes with plaid comes easy. -Albert Einstein
Robots are trying to steal my luggage.
-
May 15th, 2003, 10:09 AM
#9
Originally posted by Tom Frohman
I don't ever use streams but I was playing around with this program.
When I change
f1.open(FileName.c_str(),ios::app);
to
f1.open(FileName.c_str(),ios:  ut|ios::app);
,
it opens the file and writes to it. If I don't, it doesn't open the file and write to it.
It hangs up soon after this for me.
Like I say, I've never used streams don't know if I'm saying something ignorant or not.
Tom
Well...'fstream' is the generic stream class which can be used for input and output files. Therefore you have to specify whether you want to read from a file or write to a file...so nothing ignorant here...
-
May 15th, 2003, 11:39 AM
#10
Thanks Tom I can now write but cannot read from the file now.
I am using :
f1.open(Filename.c_str(),ios::in);
f1.read((char*)&x,sizeof(x);
any ideas?
-
May 15th, 2003, 12:24 PM
#11
When I run it in debug mode it never gets past this loop.
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));
}
because once you find the name it sets isthere to true and doesn't read the next line. When it comes around again x.fname are the same as last time. It is caught in this loop forever.
You need to get rid of the else.
So that will eventually read the last line and f1.eof()=true and isthere=true on which it will exit the loop.
while (!f1.eof() || !Isthere==true)
{
if ((strcmp(x.fname,Name) == 0) && (strcmp(x.lname,LName) ==0)) {
Isthere=true;
}
f1.read((char*)&x,sizeof(x));
}
This happens several times in your code.
Verere testudinem! (Fear the turtle)
Once you can accept the universe as matter expanding into nothing that is something, wearing stripes with plaid comes easy. -Albert Einstein
Robots are trying to steal my luggage.
-
May 15th, 2003, 04:06 PM
#12
Hi Tom,
The reason why it was looping was because there was no EOF - the file was not opened for reading.
I have new code it's still buggy as **** but it's working better.
Thanks for your help on this because you have been very helpful.
new 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 CheckNameOnFile();
void enter_password();
void clear_screen();
void MakeArrayBlank();
char Name[20];
char LName[20];
string FileName = "c:/hello.dat";
float payment;
bool RunMore;
HKEY hKey;
char szBuf[10];
bool RunOnce;
char val[10];
char c;
char pass[10];
int counter;
fstream f1;
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;
}
}
}
f1.open(FileName.c_str(),ios: ut | 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|ios::app); //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: ut | ios::app);
CheckNameOnFile();
cout <<"x.password is : " << x.password;
}
}
if (RunOnce == true)
{
enter_password();
f1.write((char*)&x,sizeof(x));
cout << x.fname;
}
Isthere=false;
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;
// exit(1);
}
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: ut | ios::app);
cout <<"\n" << "Enter pay : " ;
cin >> x.pay;
CheckNameOnFile();
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);
}
void CheckNameOnFile()
{
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: os_type mark = f1.tellg();
f1.open(FileName.c_str(),ios: ut|ios::app);
f1.write((char*)&x,sizeof(x));
f1.clear();
f1.seekg(mark);
f1.close();
}
f1.read((char*)&x,sizeof(x));
}
}
void clear_screen()
{
system("CLS");
return;
}
-
May 19th, 2003, 12:39 PM
#13
Divide and conquer!
Hi there! Just my two cents:
I think you could benefit from structuring your program better instead of just writing it as one big function. "Divide and conquer" as they say" :-). In other words: Break your program up in manageable parts and verify each part before you move on. As a side-benefit you'll remove redundancy along the way. I can imgainge than one of the first things you'd like to be able to do is to print, save and load your structure. Why don't you write small functions like:
[pseudocode]
Code:
void printPayroll()
{
cout << "fname: [" << x.fname << "]\n";
cout << "lname: [" << x.lname << "]\n";
cout << "pay: [" << x.pay << "]\n";
cout << "password: [" << x.password << "]\n";
}
void savePayroll()
{
ofstream ofs(FileName.c_str());
if(!ofs)
{
cerr << "Error saving payroll.\n";
}
ofs.write((char*)&x,sizeof(x));
}
void loadPayroll()
{
ifstream ifs(FileName.c_str());
if(!ifs)
{
cerr << "Error loading payroll.\n";
}
ifs.write((char*)&x,sizeof(x));
}
[/pseudocode]
and use those instead? I'm assuming that this is some sort of assignment, and I realize that you might want to write more than one structure to disk, but you probably get the idea :-)
In fact, the same goes for your menus:
[pseudocode]
Code:
int mainmenu()
{
clear_screen();
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";
int choice;
cin >> choice;
return choice;
}
void do_mainmenu()
{
while(true)
{
switch(mainmenu())
{
case 1:
do_paymenu();
break;
case 2:
do_display_details();
break;
case 3:
do_change_path();
break;
case 9:
return;
default:
break;
}
}
}
[/pseudocode]
Hope this helps.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|