4 Replies - 196 Views - Last Post: 01 December 2011 - 08:36 AM Rate Topic: ***** 1 Votes

#1 kamirusen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 29-November 11

Avoiding to have a same ID number in file handling

Posted 30 November 2011 - 05:50 AM

I'm trying to avoid having the same id number in a program where in if the user had inputted an id number that already exist, it will prompt for a message that says "ID no. Already exist" and let the user to retype an id number again....
i have done different combination and different arrangement of this code for i think that will solve my problem but i only got different errors.....

i'm not having a problem with the first part and the last part of the code.... i think the error occurs in the part when i try to check if the ID number exist......
i'm not sure if a got some wrong syntax..... but i already make sure that there is not...

so, do anyone knows whether this is a syntax problem or an inappropriate arrangement? if it is, can you tell me which line so i can fix it...... :hang:

here is the function definition of the code.....

void addRec(){
    fstream data;
    char choice;
	int _id;

    do{
        cls();
        cout<<"===== Enter New Record ====="<<endl;
        cout<<endl<<endl;
		
        do{
				do{
					cin.clear();
					cin.sync();

					cout<<"\nID: ";cin>>_id;
					
					if(cin.fail())
						cout<<"You must input a valid integer for the ID number!\n";
				}while(cin.fail());
		
			data.open("data.dat", ios::in);
			
			data.read((char*)&e,sizeof e);
			if(_id==e.id)
				{data.close();
					
			cout<<"ID no. already exist";}

		
		}while(_id==e.id);
		
		e.id=_id;
		newline();
        cout<<"\nName :\t\t"; gets_s(e.name,50);
        cout<<"\nAddress :\t"; gets_s(e.address,50);
        cout<<"\nBasic Salary :\t";cin>>e.basic;
        cout<<"\nAllowance :\t";cin>>e.allowance;
        cout<<"\nDeductions :\t";cin>>e.deduction;
        cout<<endl<<endl;
		do{
			cout<<"S --> Save  | C --> Cancel"<<endl;
			cin>>choice;
			newline();
		}while(!(choice=='S' || choice=='s' || choice=='C' || choice=='c'));
    }while(!(choice=='S' || choice=='s' || choice=='C' || choice=='c'));

    if(choice=='S' || choice=='s'){
        data.open("data.dat",ios::out|ios::app);
        data.write((char*)&e,sizeof e);
        data.close();
        cout<<"\nRecord Saved "<<endl<<endl;
    }
	else if (choice=='C' || choice=='c')
	{
		cout<<"Record Canceled\n";
    }
	else
		cout<<"Invalid Input\n";
    cout<<"Enter another record?"<<endl;
    cout<<"Y --> Yes "<<endl;
	cout<<"Press any character to return to Menu Options....\n";
    cin>>choice;
	newline();
    if(choice=='Y' || choice=='y')
        addRec();
    main();
}



Is This A Good Question/Topic? 0
  • +

Replies To: Avoiding to have a same ID number in file handling

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Avoiding to have a same ID number in file handling

Posted 30 November 2011 - 06:56 AM

You didn't mention what error you are getting, but I notice that the last line of your code is a call to main();. That's a problem. Your main function is already running, so this addRec function is trying to start another copy of main -- which you can't do. When this function finishes, control is automatically returned to the line immediately following the call to addRec. You should never be making a function call to main. If you are doing this because you want to repeat some portion of the code in main, the way to do it is to put that entire block of code in a loop (in main).

After you correct this, if you're still getting an error, post again, including the exact error message.
Was This Post Helpful? 1
  • +
  • -

#3 kamirusen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 29-November 11

Re: Avoiding to have a same ID number in file handling

Posted 30 November 2011 - 07:29 AM

View Postr.stiltskin, on 30 November 2011 - 06:56 AM, said:

You didn't mention what error you are getting, but I notice that the last line of your code is a call to main();. That's a problem. Your main function is already running, so this addRec function is trying to start another copy of main -- which you can't do. When this function finishes, control is automatically returned to the line immediately following the call to addRec. You should never be making a function call to main. If you are doing this because you want to repeat some portion of the code in main, the way to do it is to put that entire block of code in a loop (in main).

After you correct this, if you're still getting an error, post again, including the exact error message.



the error message i received is actually a pop out windows that say "unhandled exception. access violating writing location" .... these come up when i change some arrangement in the code, so i undo the changes because i think it just make the output worst :sweatdrop: now it don't pop out anymore, but the thing is , my expected output didn't come up :whatsthat:

i tried to remove the call of main() but still the same thing happen....
the program is not prompting for any errors now, but it seems like the code from line 22 - 28 was not functioning.... i'm not sure if i got some wrong syntax.....

void addRec(){
    fstream data;
    char choice;
	int _id;

    do{
        cls();
        cout<<"===== Enter New Record ====="<<endl;
        cout<<endl<<endl;
		
        do{
				do{
					cin.clear();
					cin.sync();

					cout<<"\nID: ";cin>>_id;
					
					if(cin.fail())
						cout<<"You must input a valid integer for the ID number!\n";
				}while(cin.fail());
		
			data.open("data.dat", ios::in); //this part seems not functioning
			
			data.read((char*)&e,sizeof e);
			if(_id==e.id)
				{data.close();
					
			cout<<"ID no. already exist";}

		
		
		}while(_id==e.id);
		
		e.id=_id;
		newline();
        cout<<"\nName :\t\t"; gets_s(e.name,50);
        cout<<"\nAddress :\t"; gets_s(e.address,50);
        cout<<"\nBasic Salary :\t";cin>>e.basic;
        cout<<"\nAllowance :\t";cin>>e.allowance;
        cout<<"\nDeductions :\t";cin>>e.deduction;
        cout<<endl<<endl;
		do{
			cout<<"S --> Save  | C --> Cancel"<<endl;
			cin>>choice;
			newline();
		}while(!(choice=='S' || choice=='s' || choice=='C' || choice=='c'));
    }while(!(choice=='S' || choice=='s' || choice=='C' || choice=='c'));

    if(choice=='S' || choice=='s'){
        data.open("data.dat",ios::out|ios::app);
        data.write((char*)&e,sizeof e);
        data.close();
        cout<<"\nRecord Saved "<<endl<<endl;
    }
	else if (choice=='C' || choice=='c')
	{
		cout<<"Record Canceled\n";
    }
	else
		cout<<"Invalid Input\n";
    cout<<"Enter another record?"<<endl;
    cout<<"Y --> Yes "<<endl;
	cout<<"Press any character to return to Menu Options....\n";
    cin>>choice;
	newline();
    if(choice=='Y' || choice=='y')
        addRec();
    //main();
}


Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Avoiding to have a same ID number in file handling

Posted 30 November 2011 - 07:56 AM

I don't know what you mean by "not functioning". It doesn't open the file? It doesn't input the data? It doesn't give you the result you expected? (And if so, what result did you expect?)

And it's not clear what you are trying to achieve. You get an ID number input from the user, and then try to compare that to the ID number of only the first record in your data file? What does that accomplish?
Was This Post Helpful? 1
  • +
  • -

#5 kamirusen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 29-November 11

Re: Avoiding to have a same ID number in file handling

Posted 01 December 2011 - 08:36 AM

View Postr.stiltskin, on 30 November 2011 - 07:56 AM, said:

I don't know what you mean by "not functioning". It doesn't open the file? It doesn't input the data? It doesn't give you the result you expected? (And if so, what result did you expect?)

And it's not clear what you are trying to achieve. You get an ID number input from the user, and then try to compare that to the ID number of only the first record in your data file? What does that accomplish?


yay... ive solve it now... a while loop in the data.read is what make it work... ^^
thanks for your time.. ^^ :angel:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1