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);
}

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.

Edited 6 Years Ago by cpeister: typo

This article has been dead for over six months. Start a new discussion instead.