Please help, and point me in the write direction... okay so I'm basically doing a simple address book. i can create contacts and display them and search for them but... when i return to the main menu and attempt to search again... i cannot open the file... or if i create new contacts old ones get deleted... i dont know what im doing wrong.


sooo this is what i have so far....

#include<iostream>
#include<iomanip>
#include<fstream>
#include<cstdlib>
#include<stdlib.h>
#include<stdio.h>
using namespace std;

void Display();
void Delete();
void Create();
void Modify();
void Search();
void SearchL();
void SearchF();
void Quit(void);

int Choice=0;
char ch;
fstream Data;
struct Contact
    {
        char Address[46];
        int Zip;
        char City[26];
        char State[26];
		char Name[41];
		char Last[41];
    };

int Home;
double debt;
char Filename[101];
Contact hi;


int main()
{
    //The following output statements are the menu.
    cout << "" <<endl;
    cout << setw(40) << "Business Contacts"<<endl;

        //These are the choices.
    cout << endl <<" Select a choice:"<<endl;
    cout <<" 1 - Display all your contacts" << endl;
    cout <<" 2 - Create a new contact" << endl;
    cout <<" 3 - Edit information for a contact\n";
    cout <<" 4 - Search for a contact\n";
    cout <<" 5 - Delete contact\n";
    cout <<" 6 - Quit" << endl;
    cout <<""<<endl;
    
    //The user choice.
    cout << " Enter your choice: ";
    cin >> Choice;

    switch(Choice)
    {        
        case 1:
        {
            Display();
            break;
        }
        case 2:
        {
            Create();
            break;
        }
        case 3:
        {
            Modify();
            break;
        }
        case 4:
        {
            Search();	
            break;
        }
        case 5:
        {
            Delete();
            break;
        }
        case 6:
        {
            Quit();
            break;
        }
		default :
		{
			cout << " That is not possible choice!!!" << endl;

			cout << " To return to the main menu press 7 to exit press 6: ";
			cin >> Choice;
        
			//Clear the screen!
			system("cls");

			if(Choice==7)
			{
			    main();
			}
			else
			{
			    return 0;
			}
		}
    }
    return 0;
}
void Display()
{
  char ch;
  
  Data.open(Filename,ios::in);
  
    
  if(Data.fail())
  {
    cout << " Error: Unable to open file. "<<endl;
    exit(1);
  }
  Data.get(ch);                    
  while(!Data.eof())
  {
    cout << ch;                
    Data.get(ch);                
  }

  Data.close();

   //Choice to return to the menu
   cout << endl << " To return to the main menu press 7 to exit press 6: ";
   cin >> Choice;
        
   //Clear the screen!
   system("cls");

   if(Choice==7)
   {
       main();
   }
   else
   {
       return;
   }
}
void Create()
{
    cout << endl << " New Contact "<<endl;
    cout << endl << " Please enter the Name for the contact."<< endl;
	cout << " Name: ";
    cin >> Filename;
    
    //This is the place where the user enters the information.
    Data.open(Filename,ios::out);

    cout << ""; 
    cin.getline(hi.Name,40);

	cout << " Last Name: ";
	cin.getline(hi.Last,40);

    cout <<" Address: "; 
    cin.getline(hi.Address,45);
    
    cout << " City: "; 
    cin.getline(hi.City,25);
    
    cout << " State: "; 
    cin.getline(hi.State,25);
    
    cout << " Zip: "; 
    cin >> hi.Zip;
    
    cout << " Home number: "; 
    cin >> Home;

    cout << " Debt: "; 
    cin >> debt;
	
    Data << endl << " Name: "<< hi.Last << ", " << Filename << endl; 
    Data << ""<<endl;
    Data << " Address: "<< hi.Address <<endl;
    Data << " City: "<< hi.City <<endl;
    Data << " State: "<< hi.State <<endl;
    Data << " Zip: "<< hi.Zip <<endl;
    Data << endl << " Home Number: "<< Home <<endl;
    Data << " Debt: $ " << debt << endl;
    Data <<""<<endl;
    Data.close();

    //Choice to return to the menu
    cout << " To return to the main menu press 7 to exit press 6: ";
    cin >> Choice;
        
    //Clear the screen!
    system("cls");

    if(Choice==7)
    {
        main();
    }
    else
    {
        return;
    }
}

void Modify()
{
    char ch;
    cout << " Enter the Client you want to edit: ";
    cin >> Filename;

    Data.open(Filename,ios::in);

      if(Data.fail())
  {
    cout << " Error: Unable to open the customer's file. "<<endl;
    system("pause");
    exit(1);
  }
  Data.get(ch);                    
  while(!Data.eof())
  {
    cout << ch;                
    Data.get(ch);                
  }

    Data.close();

	   //Choice to return to the menu
   cout << " To return to the main menu press 7 to exit press 6: ";
   cin >> Choice;
        
   //Clear the screen!
   system("cls");

   if(Choice==7)
   {
       main();
   }
   else
   {
       return;
   }
}
void Search()
{
	cout << "" <<endl;
    cout << setw(40) << "Search Menu"<<endl;

    //These are the choices.
    cout << endl <<" Select a choice:"<<endl;
    cout <<" 1 - Search by First Name " << endl;
    cout <<" 2 - Search by Last Name" << endl;
	cout <<" 3 - Return to the Main Menu" << endl;
	cout << " Enter your choice: ";
    cin >> Choice;

    switch(Choice)
    {        
        case 1:
        {
            SearchF();
            break;
        }
        case 2:
        {
            SearchL();
            break;
        }
		case 3:
		{
			main();
			break;
		}
		default :
		{
			cout << " That is not possible choice!" << endl;

			cout << " To return to the main menu press 7 to exit press 6: ";
			cin >> Choice;
        
		//Clear the screen!
			system("cls");

			if(Choice==7)
			{
			    main();
			}
			else
			{
			    return;
			}
		}
	}
}
void SearchF()
{
    cout << ""<<endl;
    cout << "To start searching the directory follow the instructions."<<endl;
    cout << "Please enter a file name."<<endl;
    cin >> Filename;

    Data.open(Filename,ios::in);
    cout<<"Searching...."<<endl;
    cout<<""<<endl;
    if(!Data)
    {
        cout<<Filename<<" could not be opened."<<endl;
        cout<<"Please check your spelling and try again."<<endl;
        Search();
    }
	// fix loop
    Data.get(ch);
    while(!Data.eof())
    {
        cout<<ch;
        Data.get(ch);
    }
    Data.close();

    cout<<"Do you want to search for another contact?(0 for yes)"<<endl;
    cout<<"Or go back to the main menu?(7 for main menu)"<<endl;
    cin>>Choice;
    if(Choice==7)
    {
        main();
    }   
    else 
    {
        Search();
    }
}

void SearchL()
{
	cout << ""<<endl;
    cout << "To start searching the directory follow the instructions."<<endl;
    cout << "Please enter a file name."<<endl;
    cin >> hi.Last;

    Data.open(hi.Last,ios::in);
    cout<<"Searching...."<<endl;
    cout<<""<<endl;
    if(!Data)
    {
        cout<< hi.Last <<" could not be opened."<<endl;
        cout<<"Please check your spelling and try again."<<endl;
        Search();
    }
    Data.get(ch);
    while(!Data.eof())
    {
        cout<<ch;
        Data.get(ch);
    }
    Data.close();

    cout<<"Do you want to search for another contact?(0 for yes)"<<endl;
    cout<<"Or go back to the main menu?(7 for main menu)"<<endl;
    cin>>Choice;
    if(Choice==7)
    {
        main();
    }   
    else 
    {
        Search();
    }
}
void Delete()
{
    cout << " Please type the name of the client you want to delete: ";
    cin >> Filename;    

    if (remove(Filename) !=0)
          cout << "Remove operation failed" << endl;
    else
          cout << " " << Filename << " has been removed." <<endl;

    //Choice to return to the menu
    cout << " To return to the main menu press 7 to exit press 6: ";
    cin >> Choice;
        
    //Clear the screen!
    system("cls");

    if(Choice==7)
    {
        main();
    }
    else
    {
        return;
    }
}
void Quit(void)
{
    cout << "Goodbye!" << endl;
    system("pause");
    exit(0);
}

Dani AI

Generated

The behavior reported by (searches failing after returning to the menu and newly created contacts replacing older ones) matches two common causes: files being opened in truncating write mode, and fragile program flow/input handling. ’s note about using an ifstream for reads is on the right track, and ’s observation that only the last contact prints is exactly what truncation looks like.

Recommended changes and quick fixes:

  • Use a single persistent file (for example "contacts.txt") and open it in append mode when adding records so earlier entries are not erased. Example write pattern:

    std::ofstream out("contacts.txt", std::ios::app);
    out << last << ',' << first << ',' << address << ',' << zip << '\n';
  • Read the whole contacts file with ifstream + std::getline to display all entries:

    std::ifstream in("contacts.txt");
    std::string line;
    while (std::getline(in, line)) std::cout << line << '\n';
  • Avoid mixing formatted extraction (operator>>) and getline without clearing the input buffer; leftover newlines cause empty reads. Clear with:

    #include <limits>
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  • Replace recursive calls to main() with a simple menu loop (show menu, switch on choice, repeat). Recursive main() calls lead to confusing state and stack growth.

For edit/delete operations, read all records into a container (vector of strings or structs), apply the modification or filter out the deleted entry, then write the container back to a temporary file and atomically replace the original (remove + rename). Always check file open results and print diagnostic messages during testing. These changes explain why older contacts vanished (truncation) and why display showed only the last contact (file content contained only the latest write).

Recommended Answers

All 3 Replies

tried this?

ifstream Data

tried this?

ifstream Data

well i just tried that rite now and got 105 errors sooo i dont know....

Were you able to output all contacts when you selected display all contacts? I tried your code and it would only output the last contact entered.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.