strcmp problem

Pages: 12
the save function is working pretty well..but I still havnt got the load function.
pardon me if I am getting on your nerves. :D
Well, I think the load function is a little bit more tricky than the save. Though it's still only a few lines of code.

It will do something similar to the add() function, with two differences:
1. it takes its input from the ifstream object infile, instead of from cin.
 
ifstream infile("phone.txt");

2. I was going to say, you won't know how many items there are - which is true at present. So you can use a while loop to just keep reading from the file until everything has been read.

(as an alternative, you could change the save() function to first output the size, then when reading the file, first read that number and use a for loop to read that many items).
Last edited on
so am i to write infile>>contact[i]>>number[i]; in the load function?
so am i to write infile>>contact[i]>>number[i]; in the load function?

Almost. That's definitely the right sort of idea. But, if either the contact name or the phone number contain spaces, then it won't behave correctly. It's safer to use getline, as that reads the entire line, including spaces.
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
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<fstream.h>
const int capacity = 100;  // mximum number of entries
const int namewide = 25;   // number of characters in name
const int numwide  = 12;   // number of characters in number

class phonebook
{
    int size;              // how many enries are currently used
    char contact[capacity][namewide];
    char number[capacity][numwide];

public:
    phonebook()
    {
	cout<<"\t\t\t\t";
	textcolor(RED);
        
	cprintf("WELCOME TO SJ'S PHONEBOOK ");

    }
    void add();
    void search();
    void edit();
    void display();
    void load();
    void save();
};
void phonebook::add()
{
    int n;
    textcolor(YELLOW);
    cprintf( "\nhow many numbers do you want to add\n");
    cout<<endl;
    cin >> n;
    cin.ignore(1000, '\n'); // discard newline from input buffer

    for (int i=0; i<n && size<capacity; i++)
    {      textcolor(BLUE);
	cprintf( "\nenter the name");cout<<'\t'<< i+1 << endl;
	cin.getline(contact[size], namewide);
	cprintf("enter the number");
	cout<<endl;
	cin.getline(number[size], numwide);
	size++;
    }
}
void phonebook::search()
{ int flag;
char j[25];

cout<<"\nenter the contact\n";
gets(j);
for(int i=0; i<=size; i++)
{if(strcmp(j,contact[i])==0)
{cout<<"here is\t"<<contact[i]<<"'s number\n"<<number[i];flag=1;break;} else flag=0;}
 if (flag==0) cout<<"\nno match found for\t"<<j;  }
 void phonebook::edit()
 {cout<<"\nwhose number do you wantt to edit?\n";
 char k[25];
 gets(k);
 for(int i=0;i<size;i++)
 {if(strcmpi(k,contact[i])==0)
  {cout<<"enter the edited number\n";
  gets(number[i]);
  cout<<"here is the edited contact\n"<<contact[i]<<':'<<number[i];break;}}}


void phonebook::display()
{    cout<<"\ndisplaying all numbers\n";
    for (int i=0; i<size; i++)
	{cout <<endl<<contact[i] << ':' << number[i] << endl;
        
}}
void phonebook::save()
{
    ofstream fout("phone.txt");
int a;
a=size;
    for (int i=0; i<size; i++)
       fout << contact[i] << ':' << number[i] << '\n'<<a;
          
}
void phonebook::load()
{
ifstream infile("phone.txt",ios::in);

infile>>a;
for (int i=0;i<a;i++)
{infile.getline(contact[i],25);
 infile.getline(number[i],12);
}a=size;}
int main()
{ 


clrscr ();

fstream myfile;
myfile.open ("phone.txt", ios::out | ios::app | ios::in);
 
 phonebook t;

t.load();
  cout<<endl<<endl;


  
 for(int i=0;i<=10;i++)
 {  
cout<<"\n  what do you want to \n (add[a],search[s],edit[e],display numbers[d],quit[q])\n";
  char o;
  cin>>o;

  switch(o)
  {
  case 's' :t.search();break;
  case 'a' :t.add();break;
  case 'e' :t.edit();break;
  case 'd' :t.display();
           case 'q' :t.save();exit(0);
  default:cout<<"\nwrong entry\n.Try once more\n";  } }
    myfile.close();

return 0;
   }
Last edited on
still wont compile
What is the error message from the compiler? The exact text will be useful.
Line 93: variable a is undefined.
This is a very straightforward error. There is no reason you should not have been able to resolve this yourself.

Line 125: You're missing a break in case 'd':

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
never mind that code..i just started getting the hang of things...please explain how i can load each and every thing in the text file as seperate stuffs...in other words how can i load each contact from text files as contact[i]'s?
Last edited on
Let's assume that your output file (created by the save() function) looks like this:
3
Fred Bloggs
123 456
Bill
789
Suzy
98765

First read the number, and get rid of any trailing newline.
1
2
3
4
    ifstream infile("filename.txt");
    int count;
    infile >> count;
    infile.ignore(100, '\n'); // ignore up to 100 characters until newline is found. 

Then loop count times, using getline() two times, to read each pair of values.
finally (assuming all went well), update size to correctly indicate how many numbers were read.
Last edited on
thank you so much chervil...the program cant get any better than this ..
Topic archived. No new replies allowed.
Pages: 12