file manipulation

i'm having problems with my function: edit.

when i enter a name eventhough it exists, it always prints "File not found".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

#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];
     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)
                 {
                      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";
     }
}

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!^^
if(temp->name == z)
You are comparing two char pointers.

As you are using char arrays for the name - you can use strcmp function.

Better still - if there is no real reason why you have to use char arrays then use C++ strings.

(And while you are at it - update your compiler - any compiler that uses iostream.h is old).

I'm pretty sure you are also going to have problems with your add_to_file function
in the way you are using the "Phone Book.txt" file.
Last edited on
i don't know strcmp function. we didn't discuss it in our class. is there any way on how to search a node?

why? what will be the possible problem? i'm sorry, i'm just a beginner at c++ and i'm new at file manipulation. we didn't even try to do a simple program about file manip. T_T

i'm using dev-c++, that's what we're using in our school. it's just my habit, but we can also use using namespace std.

THANKS!

i don't know strcmp function. we didn't discuss it in our class.


I would have thought that you would have at least done C strings (null terminated array of char)
before something like linked lists.
How do they expect you to search this particular linked list for a particular node using the name value????
Strange.

But for the record try this:

#include <stdio.h> //include this

if(strcmp (temp->name, z) ==0)//change your test line to this and see if that helps
Last edited on
even linked list wasn't discussed thoroughly..

thanks for the strcmp, i will study about it. for now, i just made a new node (integer) so that it will be easier for me to compare and search for it.

i will also try what you suggested. thanks!^^
Topic archived. No new replies allowed.