0

I decided to make a phonebook in c++ and decided to take input of names, address and number from a file. So I made a class called contact and declared the public variables name, address and number. Using a constructor I initialized these to name="noname"(string), number=0(int), address="no address"(string) Now my body of main goes as:

int main(){

contact *d;

d= new contact[200];

string name,add;

int choice,modchoice;//Variable for switch statement

int phno,phno1;

int i=0;
int initsize=0, i1=0;//i is declared as a static int variable

bool flag=false,flag_no_blank=false;


//TAKE DATA FROM FILES.....
//We create 3 files names, phone numbers, Address and then abstract the data from these files first!

fstream f1;
fstream f2;
fstream f3;

string file_input_name; 
string file_input_address;

int file_input_number;


f1.open("./names");

while(f1>>file_input_name){

  d[i].name=file_input_name;

  i++;

}
initsize=i;


f2.open("./numbers");

while(f2>>file_input_name){

  d[i1].phonenumber=file_input_number;
  i1++;

}


f3.open("./address");

while(f3>>file_input_address){

  d[i1].address=file_input_address;

  i1++;

}

now when I later search for a particular entry by name, the name is displayed correctly but the phoneumber is returned as a garbage value and address as "Noaddress" I dont understand why this is happening... In case u want to look at entire code, do let me know....

This is how i search for a particular entry which returns the name if matched but returns garbage for phone number....

cout<<"\nEnter the name";//Here it is assumed that no two contacts can have same contact number or address but may have the same name.
cin>>name;

int k=0,val;
cout<<"\n\nSearching.........\n\n";

for(int j=0;j<=i;j++){
  if(d[j].name==name){
    k++;            
    cout<<k
        <<".\t"
        <<d[j].name
        <<"\t"<<d[j].phonenumber
        <<"\t"<<d[j].address
        <<"\n\n";
    val=j;                  
  }
}

Thanks in advance

3 Answers 3

2

When your reading the file with the phone numbers

f2.open("./numbers");

while(f2>>file_input_name){

d[i1].phonenumber=file_input_number;
i1++;

}

You store the phone number in the string file_input_name but then you use a different var, file_input_number to store the information in the array d;

0

Hey guys I figured out the problem.... the problem is that i1 should be set to 0 after the second loop and that the file taking input numbers should be f2.open("numbers") and not names....silly mistake!!

1
  • i1? f2? Have you ever considered meaningful identifiers?
    – johnsyweb
    Apr 28, 2011 at 13:02
0

Since you are using C++, and not C, you should take advantage of the things that come with the language. Don't use arrays to store your data, use a std::vector. That way you don't have to remember how many things you have already put into the vector, because you can always ask the vector to tell you the size().

If I had to read in the three files I would go like this:

#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <algorithm>

using std::cin;
using std::cout;
using std::fstream;
using std::string;
using std::vector;

class contact {
public:
  string name;
  string address;
  int phone;
};

void print_contact(const contact &c) {
  cout << "name " << c.name << " address " << c.address << " phone " << c.phone << "\n";
}

int main(int argc, char **argv)
{
  vector<contact> contacts;

  string name;
  string address;
  int phone;

  fstream f1("d:\\names.txt");
  fstream f2("d:\\phones.txt");
  fstream f3("d:\\addresses.txt");

  // note that I am using getline() here.
  while (getline(f1, name) && f2 >> phone && getline(f3, address)) {
    contact c;
    c.name = name;
    c.address = address;
    c.phone = phone;
    contacts.push_back(c);
  }

  for_each(contacts.begin(), contacts.end(), print_contact);

  // for the Windows console window
  cout << "Press return to continue ...";
  string s;
  getline(cin, s);
  return 0;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.