Hello every one
am in real trouble now. i am in my 1st and last sem of my programing course. i need to write a program that can add delete display and update record. i got this program so far but the real prototype works fine for this program and after i edit it. it makes problem like after i input data it goes mad. and if i put wrong character it also goes mad ( like if in number i put alphabet )........

i don have any idea how to fix this but i think the problem is in

( line 190- line 196)
( line 209- line 212)
(line 80)
(line 84)

this is the code that needed to be fix

#include<iostream>
#include<fstream>
#include<string>
#include <stdlib.h>

using namespace std;

struct emp{
    int eno,date,published;
	string name,author,category,publisher;
 
};
emp e;

void addEmp(); //adds new record to data file
void disEmp(); //displays all records from file
void delEmp(); //asks empNo and removes that record from file
void updEmp(); //asks empno and lets you reenter that data
void cls(); //just clear screen

int main(){
    string dummy;
    int choice;

    cout<<"===== Main Menu ====="<<endl;
    cout<<endl<<endl;
    cout<<"1 -> Add Record "<<endl;
    cout<<"2 -> Display Record"<<endl;
    cout<<"3 -> Delete Record"<<endl;
    cout<<"4 -> Update Record"<<endl;
    cout<<endl;
    cout<<"0 -> Exit"<<endl;
    cout<<endl<<endl<<"Selection ";
    cin>>choice;
    if(choice<0 || choice>4){
        cls();
        main();
    }
    switch(choice){
        case 0:    break;
        case 1:    addEmp();    break;
        case 2:    disEmp();    break;
        case 3:    delEmp();    break;
        case 4:    updEmp();    break;
    }
}
void addEmp(){
    fstream data;
    string dummy;
    emp e;
    int choice;

    do{
        cls();
        cout<<"===== Enter New Record ====="<<endl;
        cout<<endl<<endl;
        cout<<"Eno :\t\t"; cin>>e.eno;
        getline(cin,dummy); //just to clear input buffer
        
		cout<<"\nBook Name :\t\t";
		cin.get();
		getline (cin,e.name);

        cout<<"\nBook Author :\t";
		cin.get();
		getline (cin,e.author);

        cout<<"\nPublisher :\t";
		cin.get();
		getline (cin,e.publisher);

        cout<<"\nCategory :\t";
		cin.get();
		getline (cin,e.category);

		cout<<"\nPublished :\t";
		cin.get();
		cin >> e.published;

        cout<<"\nDate added :\t";
		cin.get();
		cin>> e.date;
		
		cout<<endl<<endl;
        cout<<"1 -> Save  | 2 -> Cancel"<<endl;
        cin>>choice;
    }
	while(!(choice==1 || choice==2));

    if(choice==1){
        data.open("data.dat",ios::out|ios::app);
        data.write((char*)&e,sizeof(e));
        data.close();
        cout<<"\nRecord Saved "<<endl<<endl;
    }else{
        cout<<"\n\nRecord Cancelled"<<endl<<endl;
    }
    cout<<"Enter another record?"<<endl;
    cout<<"1 -> Yes  | 2 -> No"<<endl;
    cin>>choice;
    if(choice==1)
        addEmp();
    else
        return;
    main();
}

void disEmp(){
    fstream data;
    emp e;
    data.open("data.dat",ios::in);
    cls();
    while(data.read((char*) &e, sizeof(e))){
        cout<<"\n\n-----Displaying record for ENO " <<e.eno<<endl;
        cout<<"\nBook Name :\t\t"<<e.name;
        cout<<"\nBook Author :\t"<<e.author;
        cout<<"\nPublisher :\t"<<e.publisher;
        cout<<"\nCategory :\t"<<e.category;
		cout<<"\nPublished :\t"<<e.published;
        cout<<"\nDate added :\t"<<e.date<<endl;
        cout<<endl;
    }
    data.close();
    main();
}

void delEmp(){
    fstream data, temp;
    emp e;
    int eno;

    cls();
    cout<<"===== Delete Record ====="<<endl;
    cout<<endl<<endl<<endl;
    cout<<"Enter ENO to delete record ";cin>>eno;

    rename("data.dat","temp.dat");

    data.open("data.dat",ios::app);
    temp.open("temp.dat",ios::in);

    temp.seekg(0,ios::beg);

    while(temp.read((char*)&e,sizeof(e))){
        if(!(e.eno==eno)){
            cout<<"Record to delete "<<e.eno;
            data.write((char*) &e,sizeof(e));
        }
    }
    data.close();
    temp.close();

    system("Del temp.dat");

    main();
}

void updEmp(){
    fstream data, temp;
    emp e;
    string dummy;
    int eno;

    cls();
    cout<<"===== Update Record ====="<<endl;
    cout<<endl<<endl<<endl;
    cout<<"Enter ENO :_";cin>>eno;

    rename("data.dat","temp.dat");

    data.open("data.dat",ios::app);
    temp.open("temp.dat",ios::in);

    temp.seekg(0,ios::beg);

    while(temp.read((char*)&e,sizeof(e))){
        if(e.eno==eno){
            cout<<"-----Current data for ENO: "<<e.eno;
			cout<<"\nBook Name :\t\t"<<e.name;
			cout<<"\nBook Author :\t"<<e.author;
			cout<<"\nPublisher :\t"<<e.publisher;
			cout<<"\nCategory :\t"<<e.category;
			cout<<"\nPublished :\t"<<e.published;
	        cout<<"\nDate added :\t"<<e.date<<endl;
            cout<<endl<<endl;

            cout<<"-----Enter New Values-----"<<endl;
            cout<<"\nEno :\t\t"; cin>>e.eno;
           // getline(cin,dummy); //just to clear input buffer
           cout<<"\nBook Name :\t\t"<<e.name;
           cout<<"\nBook Author :\t"<<e.author;
           cout<<"\nPublisher :\t" <<e.publisher;
			cout<<"\nCategory :\t" <<e.category;
			cout<<"\nPublished :\t"<<e.published;
			cout<<"\nDate Added :\t" <<e.date;
			cout<<endl<<endl;
			
        }
        data.write((char*) &e,sizeof(e));
    }
    data.close();
    temp.close();

    system("Del temp.dat");

    main();
}

void cls(){
    for (int i=0;i<50;i++)
        cout<<"\n";
}

I really don't see anything wrong with the lines you have mentioned as suspects for the problem. They all appear to be properly-written output statements. Your issue is is actually an input issue. It's caused by the input stream getting corrupted.

What is struct emp intended to represent? It looks like it's intended to represent a book. The problem is most likely that all the members of struct emp are declared as ints.

Do you really want all of the information to be represented in a numeric form? You'll have to change the declarations of the appropriate members of the struct to either C-strings or std::string objects so that they can hold character data instead of numeric data.

Look in the list of "sticky" threads at the top of the forum for the "How to flush the input stream?" thread. That will also be beneficial.

EDIT:
I just noticed Line 37. You have called main() recursively. That is a HUGE no-no, you should never do that. Use a loop instead.

EDIT 2:
Wow... :confused: You've done it at Lines 105, 124, 155 and 206 to end those functions as well. You do not have to do that. In fact, you should NEVER do that. When a function ends, the program's flow of control automatically returns to the line where the function was called from. Once the flow of control returns, execution continues from that point.

Edited 5 Years Ago by Fbody: n/a

I really don't see anything wrong with the lines you have mentioned as suspects for the problem. They all appear to be properly-written output statements. Your issue is is actually an input issue. It's caused by the input stream getting corrupted.

What is struct emp intended to represent? It looks like it's intended to represent a book. The problem is most likely that all the members of struct emp are declared as ints.

Do you really want all of the information to be represented in a numeric form? You'll have to change the declarations of the appropriate members of the struct to either C-strings or std::string objects so that they can hold character data instead of numeric data.

Look in the list of "sticky" threads at the top of the forum for the "How to flush the input stream?" thread. That will also be beneficial.

You must have misread is struct becaues of syntax highlighting, Because I just did the same :) even though it is still wrong

Edited 5 Years Ago by Akill10: n/a

Comments
nice catch

You must have misread is struct becaues of syntax highlighting, Because I just did the same :) even though it is still wrong

Oh yeah. I see that now. :$ The OP's formatting doesn't help much either. It looks like a line-wrapped declaration statement rather than a separate statement.

Looking at it more, I think the problem might be related to their recursive use of main(), but I can't be sure just yet. I don't want to test this code myself until that's corrected.

Edited 5 Years Ago by Fbody: n/a

What is struct emp intended to represent? It looks like it's intended to represent a book. The problem is most likely that all the members of struct emp are declared as ints.

emp here represents as book register. and members of struct are both int and string. i don this thats the problem here.

EDIT:
I just noticed Line 37. You have called main() recursively. That is a HUGE no-no, you should never do that. Use a loop instead.

i deleted main() from all places . and i realized that dosent do any thing.

You must have misread is struct becaues of syntax highlighting, Because I just did the same even though it is still wrong

i donno what are u talking about


all rite people i really suck in this thing. i know u donot help people if they dont help them. but this is as far as i can go i donno what to do next to fix it please help me here. :S

Comments
Chat lingo

i deleted main() from all places . and i realized that dosent do any thing.

I didn't say definitively that calling main() in that way was your problem. I implied that it could be contributing to your issue by executing extraneous commands as the recursion unravels.

You must have misread is struct becaues of syntax highlighting, Because I just did the same even though it is still wrong

i donno what are u talking about

Akill10 was speaking to me. He was correcting an errant observation I made, don't worry about it.

What does your new code look like? We'll have to see it before we can make any more suggestions. If it's still not working properly, there is likely an input that is leaving the stream "dirty" and/or "corrupted".

Edited 5 Years Ago by Fbody: n/a

hey
am really sorry but i cannot get this code anywhere ........ i tried different input using do/if /while ( for restricting the input ) but cannot get anywhere.. can any one help me by giving pointed ans :S i am really bad in this

You can't get it anywhere? Are you trying to tell us you haven't written a line of it yourself?

I asked you to post the current version of your code. Is that so hard?

well new code is the same thing just the main aint there ... and the hard part is i don see anything wrong there . just its not working

I can see why you wanted to use your MAIN() in the first instance.

if(choice<0 || choice>4){
        cls();
        main();
    }

If the selected choice is not valid, just restart the program. Instead of restarting the program, just ask the user to "try again" with a more valid input.

do{
<some code>
while(some input is not valid);    // if invalid, do again; else continue past the while statement

You may want to revisit while,do-while,for loops again. Once you have this working correctly, you can "basically repeat this" for another other main() you had when dealing with invalid input.

#include<iostream>
#include<fstream>
#include<string>
#include <stdlib.h>

using namespace std;

//book structure
struct emp
{
	string eno;								
	string date,published;					
	string name,author,category,publisher;

};
emp e;

void addEmp(); //adds new record to data file
void disEmp(); //displays all records from file
void delEmp(); //asks empNo and removes that record from file
void updEmp(); //asks empno and lets you reenter that data
void cls(); //just clear screen

int main()
{
	string dummy;
	int choice;

	//display the menu
	cout<<"===== Main Menu ====="<<endl;
	cout<<endl<<endl;
	cout<<"1 -> Add Record "<<endl;
	cout<<"2 -> Display Record"<<endl;
	cout<<"3 -> Delete Record"<<endl;
	cout<<"4 -> Update Record"<<endl;
	cout<<endl;
	cout<<"0 -> Exit"<<endl;
	cout<<endl<<endl<<"Selection ";
	//user enters choice
	cin>>choice;
	//choice validation is done
	if(choice<0 || choice>4)
	{
		cls();
	}
	//action according to choice is being performed
	switch(choice)
	{
	case 0:    break;
	case 1:    addEmp();    break;
	case 2:    disEmp();    break;
	case 3:    delEmp();    break;
	case 4:    updEmp();    break;
	}
}

void addEmp()
{
	fstream data;
	string dummy;
	emp e;
	int choice;

	//loop ends on completion of book addition of cancellation
	do
	{
		//clear screen
		cls();
		cout<<"===== Enter New Record ====="<<endl;
		cout<<endl<<endl;
		getline(cin,dummy); //just to clear input buffer
		
		cout<<"Enter Your Eno (exmple: 0001):" ;
		getline (cin, e.eno);
		while (
			( e.eno.size()<=3 ) ||										//if eno is smaller or equal to 3 digits, i.e smaller then 999
			( e.eno.size()>4) ||										//if eno is larger then 4 digits, i.e larger then 9999
			( e.eno.find_first_of( "0123456789" ) == string::npos)		//if is not starting with digit
			)
		{
			//Keep entering eno till conditions meet
			cout << "Invalid ! Please Re-Enter Eno Again !!" << endl;
			cout << "Please Enter Your Eno : " ;
			getline (cin, e.eno);

		}

		//Input book name
		cout<<"\nBook Name :\t\t";
		getline (cin,e.name);

		//Input book author
		cout<<"\nBook Author :\t";
		getline (cin,e.author);

		//Input book publisher
		cout<<"\nPublisher :\t";
		getline (cin,e.publisher);

		//Input book catagory
		cout<<"\nCategory :\t";
		getline (cin,e.category);

		//Input book published
		cout<<"\nPublished :\t";
		cin >> e.published;

		//Input book date added
		cout<<"\nDate added :\t";
		cin>> e.date;

		cout<<endl<<endl;
		//ask if want to save or not
		cout<<"1 -> Save  | 2 -> Cancel"<<endl;
		cin>>choice;
	}
	while(!(choice==1 || choice==2));		//keep looping till user enters 1 or 2

	if(choice==1)
	{
		//save book data
		data.open("data.dat",ios::out|ios::app);
		//write at end of file
		data.write((char*)&e,sizeof(e));
		//close file
		data.close();
		cout<<"\nRecord Saved "<<endl<<endl;
	}
	else
	{
		//cancel book addition
		cout<<"\n\nRecord Cancelled"<<endl<<endl;
	}
	//ask if want to enter another record
	cout<<"Enter another record?"<<endl;
	cout<<"1 -> Yes  | 2 -> No"<<endl;
	cin>>choice;
	//if yes...call this function again
	if(choice==1)
		addEmp();
	else
		return;

}

void disEmp()
{
	fstream data;
	emp e;
	//Opening the file which has all the books data
	data.open("data.dat",ios::in);
	//clearing screen
	cls();

	//loop which ends only when temp.dat file's all data has been read
	while(data.read((char*) &e, sizeof(e)))
	{
		//outputting the data of the read structure on the screen
		cout<<"\n\n-----Displaying record for ENO " <<e.eno<<endl;
		cout<<"\nBook Name :\t\t"<<e.name;
		cout<<"\nBook Author :\t"<<e.author;
		cout<<"\nPublisher :\t"<<e.publisher;
		cout<<"\nCategory :\t"<<e.category;
		cout<<"\nPublished :\t"<<e.published;
		cout<<"\nDate added :\t"<<e.date<<endl;
		cout<<endl;
	}
	//closing file handle
	data.close();
}

void delEmp()
{
	fstream data, temp;
	emp e;
	int eno;

	cls();
	cout<<"===== Delete Record ====="<<endl;
	cout<<endl<<endl<<endl;
	cout<<"Enter ENO to delete record ";cin>>eno;

	//renaming the file to temp because we will be creating a new data.dat
	rename("data.dat","temp.dat");

	//opening the files
	data.open("data.dat",ios::app);		//opening fill with append flag
	temp.open("temp.dat",ios::in);		//opening fill with input flag

	//seeking the pointer to beginning of the file
	temp.seekg(0,ios::beg);
	
	//making the entered int eno to character because we will use string comparison in next loop
	char checkNo[10];
	sprintf(checkNo, "%04d", eno);

	//loop which ends only when temp.dat file's all data has been read
	while(temp.read((char*)&e,sizeof(e)))
	{
		//We check if the entry in the file has eno equal to inputed eno (in checkNo)
		//if it is not equal we write to file, else we "skip" it...so in effect we delete it
		if(!(e.eno == checkNo) )
		{
			cout<<"Record to delete "<<e.eno;
			//writing the data structure of book in file which is not the one to be deleted
			data.write((char*) &e,sizeof(e));
		}
	}
	//closing file handles
	data.close();
	temp.close();

	//deleting temp file
	system("Del temp.dat");
}

void updEmp()
{
	fstream data, temp;
	emp e;
	string dummy;
	int eno;

	cls();
	cout<<"===== Update Record ====="<<endl;
	cout<<endl<<endl<<endl;
	cout<<"Enter ENO :_";cin>>eno;

	//renaming the file to temp because we will be creating a new data.dat
	rename("data.dat","temp.dat");

	//opening the files
	data.open("data.dat",ios::app);		//opening fill with append flag
	temp.open("temp.dat",ios::in);		//opening fill with input flag

	//seeking the pointer to beginning of the file
	temp.seekg(0,ios::beg);

	//making the entered int eno to character because we will use string comparison in next loop
	char checkNo[10];
	sprintf(checkNo, "%04d", eno);

	//loop which ends only when temp.dat file's all data has been read
	while(temp.read((char*)&e,sizeof(e)))
	{
		//We check if the entry in the file has eno equal to inputed eno (in checkNo)
		if(e.eno==checkNo)
		{
			//Showing the previous information of the book
			cout<<"-----Current data for ENO: "<<e.eno;
			cout<<"\nBook Name :\t\t"<<e.name;
			cout<<"\nBook Author :\t"<<e.author;
			cout<<"\nPublisher :\t"<<e.publisher;
			cout<<"\nCategory :\t"<<e.category;
			cout<<"\nPublished :\t"<<e.published;
			cout<<"\nDate added :\t"<<e.date<<endl;
			cout<<endl<<endl;

			//Getting new values from the user
			cout<<"-----Enter New Values-----"<<endl;
			cout<<"\nEno :\t\t"; 
			cin>>e.eno;
			getline(cin,dummy);						//Clearing input buffer
			cout<<"Book Name :\t\t";				//New book name input
			getline (cin,e.name);
			cout<<"Book Author :\t";				//New author name input
			getline (cin,e.author);
			cout<<"Publisher :\t";					//New publisher name input
			getline (cin,e.publisher);
			cout<<"Category :\t";					//New catagory name input
			getline (cin,e.category);
			cout<<"Published :\t";					//New published input
			cin >> e.published;
			cout<<"Date added :\t";					//New date input
			cin>> e.date;
		}
		//Writing the book structure back in file...we are writing the structure even if its not changed
		data.write((char*) &e,sizeof(e));
	}
	//Closing the file handles
	data.close();
	temp.close();

	system("Del temp.dat");
}

void cls()
{
	system("cls");
	////moving all the material on screen down by 50 lines
	//for (int i=0;i<50;i++)
	//	cout<<"\n";
}

this is the new code i just tried to review myself with the explaining . now i need to know how can i get back to the main menu after every job.

All you have to do is put everything from Line 29 thru Line 54 inside some sort of loop structure (not a recursion on main(), a loop).

This question has already been answered. Start a new discussion instead.