0

i'm having problems with the edit function on my program. when i enter something, it crashes. it prints out "File not found" endlessly.

#include<iostream.h>
#include<fstream.h>
#include<conio.h>

void info()
{    
     cout << "               **********************************************" << endl;
     cout << "               *                 Case Study - 2             *" << endl;
     cout << "               *                   Phonebook                *" << endl;
     cout << "               *                   Y - 201                  *" << endl;
     cout << "               *                                            *" << endl;
     cout << "               *                                            *" << endl;
     cout << "               *                                            *" << endl;
     cout << "               *                                            *" << endl;
     cout << "               **********************************************" << endl;
}

struct node
{ 
        char name[30];
        char number[11];
        node *nxt;
};

node *start_ptr = NULL;
char option = ' ';

void add()
{
     
     
     node *temp, *temp2;
     temp = new node;
     
     cout << "\n\nPlease enter name: ";
     cin >> temp->name;
     cout << "Please enter number: ";
     cin >> temp->number;
          
     temp->nxt = NULL;
     
     if (start_ptr == NULL)
       { 
           start_ptr = temp;
       }
     else
       { 
           temp2 = start_ptr;
           while (temp2->nxt != NULL)
           {    
               temp2 = temp2->nxt;
           }
           temp2->nxt = temp;
       }
}

void add_to_file()
{
         ofstream outfile("Phone Book.txt");
         node *temp;
         temp = start_ptr;   
         
         while (temp != NULL)
         {
           outfile << temp->name << "\t";
           outfile << temp->number << endl;
           temp = temp->nxt;
         }
}   

void edit()
{
     node *temp;
     temp = start_ptr;
     char z[30];
     
     if(temp==NULL)
		cout << "Phonebook doesn't contain any stored numbers.\n";
     else
     {
         cout << "\nEnter name: ";
         cin >> z;
         
         while(temp!=NULL)
         {
                 if(temp->name == z)
                 {
                      cout << "Name: " << temp->name << "\tNumber: " << temp->number << endl << endl;
                      cout << "Enter new name:";
                      cin >> temp->name;
                      cout << "Enter new number: ";
                      cin >> temp->number;
                      temp = temp->nxt;
                 }
                 else 
				      cout << "File not found." << endl;
         }
     }
}

int main()
{
    info();
    start_ptr = NULL;
    do
	{
     cout << endl;
     system("color 17");
     cout << "What do you want to do?: " << endl;
     cout << "\n1.   Add a contact" << endl; 
     cout << "\n2.   Delete a contact" << endl;
     cout << "\n3.   Edit a contact" << endl;
     cout << "\n4.   Search a contact" << endl;
     cout << "\n5.   Display contacts" << endl;
     cout << "\n6.   Exit" << endl << endl;

      cout << endl << " --->>> ";
      
	  option = getche();
	  
	  switch (option)
	    {
          case '1' : { add(); add_to_file(); break;}
	      //case '2' : del(); break;
	      case '3' : edit(); break;
	      //case '4' : search(); break;
	      //case '5' : display(); break;
          case '6': system("exit"); break;
         //default: cout<<"\nINVALID INPUT!!";
	    }
	    //if (option >= '1' && option <= '6')
           //display();      
	}
     while (option != '6');
    system("pause");
}

thanks

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by silvertooth07
0
void edit()
{
     node *temp;
     temp = start_ptr;
     char z[30];
     int found = 0;
     
     if(temp==NULL)
		cout << "Phonebook doesn't contain any stored numbers.\n";
     else
     {
         cout << "\nEnter name: ";
         cin >> z;
         
         while(temp!=NULL)
         {
                 if(temp->name == z)// i think the problem is in this condition
                 {
                      cout << "Name: " << temp->name << "\tNumber: " << temp->number << endl << endl;
                      cout << "Enter new name:";
                      cin >> temp->name;
                      cout << "Enter new number: ";
                      cin >> temp->number;
                      found = 1;
                 }
                 temp=temp->nxt;
         }
         if (found==0)
            cout << "File not Found";
     }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.