Hello. I thought I was done with this program but It doesn't work like its supposed to. I wrote the program to display the file every time I press one. But it only works the first time. After that it jus keeps sayin cannot open file. Why does it say that? Its just supposed to run the function again!! I have no errors or warnings on my compiler. Please can u take a look at this and tell me what's wrong!! I have another question how do you perform a search in a file for a char variable. Like searching a contact name. is it possible? Thats' it. here is the code.

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

//Globally define varibales
int Choice, Choice3, count, count1, a=100, i, n=100;
char Filename[101], Choice4, Choice6[20];

//Funtion prototypes
void Display(void);
void Delete();
int Create();
void Modify();
int Search();
void Quit(void);
void Restart(int);


fstream Data;
struct Contact
	{
		int Zip;
		char Name[101];
		char Middle[101];
		char Last[101];
		long Area;
		long Area1;
		long Area2;
		long Area3;
		long Home;
		long Business;
		long Cell;
		long Other;
		char Email[101];
		char Address[101];
		char City[101];
		char State[101];
		char Country[101];
		char Job[101];
		char Company[101];
	};
Contact hi[100];
int main()
{
	//The following output statements are the menu.
	cout<<""<<endl;
	cout<<"                         Welcome to Virtual Address Book                    "<<endl;
	cout<<"In this book, you have a variety of features to revolutionize your contact lists"<<endl;
	cout<<""<<endl;
	cout<<"Follow the directions below and chose appropriately from the menu."<<endl;
	cout<<""<<endl;
	
	//These are the choices.
	cout<<"Please type in the following number to perform the following task:"<<endl;
	cout<<" 1 - Display latest contact"<<endl;
	cout<<" 2 - Create a new contact"<<endl;
	cout<<" 3 - Edit information for a contact"<<endl;
	cout<<" 4 - Search for a contact"<<endl;
	cout<<" 5 - Delete contact"<<endl;
	cout<<" 6 - Quit"<<endl;
	cout<<""<<endl;
	cout<<""<<endl;
	cout<<"If you want to return to this screen, just type in '7'."<<endl;
	cout<<" 7 - Returns to main menu"<<endl;
	cout<<""<<endl;
	
	//The user choice.
	cout<<"Enter your choice:"<<endl;
	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;
		}
		case 7:
		{
			Restart(Choice);
			break;
		}
		default:
		{
			cout<<"**********************************************"<<endl;
			cout<<"* Error: You have entered a wrong choice     *"<<endl;
			cout<<"* Please follow menu instructions.           *"<<endl;
			cout<<"* The program will now restart.              *"<<endl;
			cout<<"**********************************************"<<endl;
			main();
		}
	}
}

void Restart(int Choice)
{
	while(Choice==7)
		main();
}
void Quit(void)
{
	cout<<"Goodbye!"<<endl;
	exit(1);
}
int Create()
{
	cout<<""<<endl;
	cout<<"*This will let you create a new contact*"<<endl;
	cout<<"*Please provide the following information*"<<endl;
	cout<<"*When you want to skip a field, just press enter*"<<endl;
	cout<<""<<endl;

	cout<<"Creating..."<<endl;
	cout<<""<<endl;
	
	//This is the place where the user enters the information.
	char hello[20];
	Data.open("Contacts.txt", ios::app);
	int C;
	for(C=count1;C<a;C++)
	{	
		cout<<"Please fill in the the following fields."<<endl;
		cin.getline(hello,10);
		cout<<""<<endl;
		cout<<"First name: "<<endl; 
		cin.getline(hi[count1].Name,65);
		cout<<"Middle name: "<<endl;
		cin.getline(hi[count1].Middle,65);
		cout<<"Last name: "<<endl;
		cin.getline(hi[count1].Last,65);
		cout<<""<<endl;
		cout<<"Job title: "<<endl; 
		cin.getline(hi[count1].Job,85);
		cout<<"Company: "<<endl; 
		cin.getline(hi[count1].Company,85);
		cout<<""<<endl;
		cout<<"Address: "<<endl; 
		cin.getline(hi[count1].Address,100);
		cout<<"City: "<<endl; 
		cin.getline(hi[count1].City,100);
		cout<<"State: "<<endl; 
		cin.getline(hi[count1].State,100);
		cout<<"Zip: "<<endl; 
		cin>>hi[count1].Zip;
		cin.getline(Choice6,10);
		cout<<"Country: "<<endl; 
		cin.getline(hi[count1].Country,100);
		cout<<"E-mail: "<<endl; 
		cin.getline(hi[count1].Email,100);
		cout<<"Area code(Home):"<<endl;
		cin>>hi[count1].Area;
		cout<<"Home: "<<endl; 
		cin>>hi[count1].Home;
		cout<<"Area code(Bus.):"<<endl;
		cin>>hi[count1].Area1;
		cout<<"Business: "<<endl; 
		cin>>hi[count1].Business;
		cout<<"Area code(Cell):"<<endl;
		cin>>hi[count1].Area2;
		cout<<"Cell: "<<endl; 
		cin>>hi[count1].Cell;
		cout<<"Area code(Other):"<<endl;
		cin>>hi[count1].Area3;
		cout<<"Other: "<<endl; 
		cin>>hi[count1].Other;
	
		Data<<""<<endl;
		Data<<"Name: "<<hi[count1].Name<<endl;
		Data<<""<<endl;
		Data<<"Job title: "<<hi[count1].Job<<endl;
		Data<<"Company: "<<hi[count1].Company<<endl;
		Data<<""<<endl;
		Data<<"Address: "<<hi[count1].Address<<endl;
		Data<<"City: "<<hi[count1].City<<endl;
		Data<<"State: "<<hi[count1].State<<endl;
		Data<<"Zip: "<<hi[count1].Zip<<endl;
		Data<<"Country: "<<hi[count1].Country<<endl;
		Data<<""<<endl;
		Data<<"E-mail: "<<hi[count1].Email<<endl;
		Data<<""<<endl;
		Data<<"Home: "<<hi[count1].Area<<hi[count1].Home<<endl;
		Data<<"Business: "<<hi[count1].Area1<<hi[count1].Business<<endl;
		Data<<"Cell: "<<hi[count1].Area2<<hi[count1].Cell<<endl;
		Data<<"Other: "<<hi[count1].Area3<<hi[count1].Other<<endl;
		Data<<""<<endl;
		Data<<C<<endl;
		Data.close();
		
		cout<<"Do you want to return to the main menu?(7=restart)"<<endl;
		cout<<"Or create another contact?(0-contact)"<<endl;
		cin>>Choice;
	    if(Choice==7)
		main();
		else if(Choice==0)
		Create();
		return 1;
		}
return 1;
}
void Display(void)
{
  
char ch, Choice5;
	cout<<""<<endl;
  cout<<"Displaying..."<<endl;
  Data.open("Contacts.txt",ios::in);  
  if(!Data)
  {
    cout<<""<<endl;
	cout<<"Error: Unable to open file."<<endl;
	cout<<"Cannot open file without creating a contact recently."<<endl; 
    exit(1);
  }
	Data.get(ch);                    
	while(!Data.eof())
	{
    cout<<ch;
    Data.get(ch);                
	}
	Data.close();
	cout<<"Press y to go to main menu."<<endl;
	cin>>Choice5;
	if(Choice5=='y'||Choice5=='Y')
		main();
	else
	{
		cout<<"Goodbye!"<<endl;
		exit(1);
	}
}
void Modify()
{
	
}
int Search()
{
	cout<<""<<endl;
	cout<<"To start searching the directory follow the instructions."<<endl;
	cout<<"Please enter the first name of the contact."<<endl;
	cin>>Filename;

	Data.open("Contacts.txt",ios::in);
	cout<<""<<endl;
	cout<<"Searching...."<<endl;
	cout<<""<<endl;
	for(i=0; i<n; i++) 
	{
    if (hi[i].Name == Filename) 
	{
	cout<<hi[i].Name<<endl;
	cout<<hi[i].Middle<<endl;
	cout<<hi[i].Last<<endl;
	cout<<""<<endl;
	cout<<hi[i].Job<<endl;
	cout<<hi[i].Company<<endl;
	cout<<""<<endl;
	cout<<hi[i].Address<<endl;
	cout<<hi[i].City<<endl;
	cout<<hi[i].State<<endl;
	cout<<hi[i].Country<<endl;
	cout<<""<<endl;
	cout<<hi[i].Email<<endl;
	cout<<hi[i].Area<<hi[i].Home<<endl;
	cout<<hi[i].Area1<<hi[i].Business<<endl;
	cout<<hi[i].Area2<<hi[i].Cell<<endl;
	cout<<hi[i].Area3<<hi[i].Other<<endl;
	return i;
	}
	}
	if(!Data)
	{
		cout<<"The Contacts list could not be opened."<<endl;
		cout<<"Please check your spelling and try again."<<endl;
		cout<<"Do you want to return to main menu?(7-Menu,0-search)"<<endl;
		cin>>Choice3;
		if(Choice==7)
		main();
		else if(Choice==0||Choice==0)
		Search();
	}
	Data.close();

	cout<<"Do you want to search for another contact?(0-yes)"<<endl;
	cout<<"Or go back to the main menu?(7-main menu)"<<endl;
	cin>>Choice;
	if(Choice==7)
		main();
	else if(Choice==0||Choice==0)
		Search();
	return 1;
}
void Delete()
{
	char Filename[101];
	char Choice2;
	cout<<""<<endl;
	cout<<"To start deleting follow the instructions."<<endl;
	cout<<""<<endl;
	cout<<"Please enter the contact's first name."<<endl;
	cin>>Filename;
	
	cout<<"Are you sure you want to delete "<<Filename<<"? (Y=yes or N=no)"<<endl;
	cin>>Choice2;
	if(Choice2=='Y'||Choice2=='y')
	{
		Data.open("Contacts.txt",ios::trunc);
		cout<<""<<endl;
		cout<<"Deleting...."<<endl;
		cout<<""<<endl;
		if(!Data)
		{
			cout<<Filename<<" has been deleted."<<endl;
			main();
		}
		Data.close();
	}
	else if(Choice2=='N'||Choice2=='n')
	{
		cout<<"Do you want to return to the main menu?(7-main menu)"<<endl;
		cout<<"Or do you want to delete another item?(0-Yes)"<<endl;
		cin>>Choice;
		if(Choice==7)
		{	
			cout<<""<<endl;
			cout<<"Returning..."<<endl;
			cout<<""<<endl;
			main();
		}
		else if(Choice==0)
			Delete();
	}
}

Recommended Answers

All 2 Replies

while(!Data.eof())
{
    cout<<ch;
    Data.get(ch);                
}
Data.close();

I don't have access to a compiler at this time, but I suspect that the problem has to do with the above snippet. When you find EOF so eof() returns true and ! eof() becomes false, something else happens. In particular I believe the fail bit of the state variable in the stream object (in this case Data) is set. This then voids the stream for any other activities, including close() and open() until the state variable is reset to default values by calling clear(). Therefore, I'd try adding a line like:

Data.clear();

between the closing brace of the above snippet and Data.close().

HTH.

Man, Lerner!
Your a genius. Thanks. I totally should have done that. Thanks buddy.
It works now. :cheesy:

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.