CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Nov 2010
    Posts
    6

    PhoneBook Phonenumber bug

    Hello,
    I was wondering if I could get a little help fixing my issue. The project we were given in class was to create a phonebook app that asks the user to input the first and last name, number, and email of a contact. Then it saves to a file (book.txt). The repeats the action until the user ends the loop. Everything works if you only input 1 contact. But if you enter 2 contacts the phone number messes up when the user cin >> phonenumber[count] but everything (name and email) turns out fine. I knew the problem is i ask the user to cin the phonenumber like ###-###-#### and the "-" throw everything off because when I enter it withought the "-" it works fine.
    What happens is if i enter 555-555-5555 for phonenumber[0] and then 555-000-0000 (or any number) phonenumber[0] becomes 555-555-5555555-000-0000 and phonenumber[1] stays 555-000-0000. and so on as i add more contacts.
    [int main()
    {
    int count, number = 0;
    char Fname[100][NAMES], Lname[100][NAMES], PhoneNumber[100][NUMBER], email[100][MAIL], selected;

    //Call all data from file

    ifstream inFile;
    inFile.open("book.txt");
    inFile >> count;
    while (inFile >> Fname[number] >> Lname[number] >> PhoneNumber[number] >> email[number])
    {
    number++;
    }
    inFile.close();

    displayMenu(Fname, Lname, PhoneNumber, email, count);

    return 0;
    }

    void displayMenu(char Fname[][NAMES], char Lname[][NAMES], char PhoneNumber[][NUMBER], char email[][MAIL], int count )
    {
    cout << endl << setw(5) << left << "A)" << "Add Contact" << endl;
    cout << setw(5) << left << "D)" << "Delete Contact" << endl;
    cout << setw(5) << left << "F)" << "Find and Display Contact" << endl;
    cout << setw(5) << left << "L)" << "List Contacts" << endl;
    cout << setw(5) << left << "S)" << "Save Contacts" << endl;
    cout << setw(5) << left << "E)" << "Exit" << endl << endl;
    selection(Fname, Lname, PhoneNumber, email, count);

    }

    void selection(char Fname[][NAMES], char Lname[][NAMES], char PhoneNumber[][NUMBER], char email[][MAIL], int count )
    {
    \
    char selected;
    bool valid = false;
    while (valid != true)
    {
    cin >> selected;
    switch(selected)
    {
    case 'A':
    case 'a':valid = true, addContact(Fname, Lname, PhoneNumber, email, count);
    break;
    case 'D':
    case 'd': valid = true, deleteContact(Fname, Lname, PhoneNumber, email, count);
    break;
    case 'F':
    case 'f': valid = true, displayContact();
    break;
    case 'L':
    case 'l': valid = true, listContacts();
    break;
    case 'S':
    case 's': valid = true, saveContacts(Fname, Lname, PhoneNumber, email, count);
    break;
    case 'E':
    case 'e': valid = true, exitPhonebook();
    break;
    }
    }
    }

    void addContact(char Fname[][NAMES], char Lname[][NAMES], char PhoneNumber[][NUMBER], char email[][MAIL], int count )
    {
    cout << count;
    if (count < 100)
    {
    cout << "First Name of Contact: ";
    cin >> Fname[count];

    cout << "Last Name of Contact: ";
    cin >> Lname[count];

    cout << "Phone Number of Contact (###-###-####): ";
    cin >> PhoneNumber[count];

    cout << "Email of Contact: ";
    cin >> email[count];
    count++;
    }

    else
    {
    cout << "Phone Book is full. Please delete a contact if you wish to add a new one." << endl;
    }
    displayMenu(Fname, Lname, PhoneNumber, email, count);]

    I was hoping i could get some help. Thanks in advance :-)

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: PhoneBook Phonenumber bug

    Please fix this post by using code tags, proper indentation, and posting code that compiles.

  3. #3
    Join Date
    Nov 2010
    Posts
    6

    Re: PhoneBook Phonenumber bug

    I apologize,
    I was wondering if I could get a little help fixing my issue. The project we were given in class was to create a phonebook app that asks the user to input the first and last name, number, and email of a contact. Then it saves to a file (book.txt). It repeats the action until the user ends the loop.
    Everything works if you only input 1 contact. But if you enter 2 contacts the phone number messes up when the user cin >> phonenumber[count] but everything (name and email) turns out fine. I knew the problem is i ask the user to cin the phonenumber like ###-###-#### and the "-" throw everything off because when I enter it withought the "-" it works fine.
    What happens is if i enter 555-555-5555 for phonenumber[0] and then 555-000-0000 (or any number) phonenumber[0] becomes 555-555-5555555-000-0000 and phonenumber[1] stays 555-000-0000. and so on as i add more contacts.

    Code:
    #include <iostream>
    #include <cstring>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    
    // Constants inside the program
    const int NAMES = 32, NUMBER = 12, MAIL = 50;
    
    // Prototypes of the functions
    void displayMenu(char[][NAMES], char[][NAMES], char[][NUMBER], char[][MAIL], int);
    void selection(char[][NAMES], char[][NAMES], char[][NUMBER], char[][MAIL], int);
    void addContact(char[][NAMES], char[][NAMES], char[][NUMBER], char[][MAIL], int);
    void deleteContact(char[][NAMES], char[][NAMES], char[][NUMBER], char[][MAIL], int);
    void displayContact();
    void listContacts();
    void saveContacts(char[][NAMES], char[][NAMES], char[][NUMBER], char[][MAIL], int);
    void exitPhonebook();
    void findContact(char[][NAMES], int, int);
    
    int main()
    {
    	int count, number = 0;
    	char  Fname[100][NAMES], Lname[100][NAMES], PhoneNumber[100][NUMBER], email[100][MAIL], selected;
    	
    	//Call all data from file
    	
    	ifstream inFile;
    	inFile.open("book.txt");
    	inFile >> count;
    	while (inFile >> Fname[number] >> Lname[number] >> PhoneNumber[number] >> email[number])
    	{
    		number++;
    	}
    	inFile.close();
    
    	displayMenu(Fname, Lname, PhoneNumber, email, count);
    	
    	return 0;
    }
    
    void displayMenu(char Fname[][NAMES], char Lname[][NAMES], char PhoneNumber[][NUMBER], char email[][MAIL], int count )
    {
    	cout << endl << setw(5) << left << "A)" << "Add Contact" << endl;
    	cout << setw(5) << left << "D)" << "Delete Contact" << endl;
    	cout << setw(5) << left << "F)" << "Find and Display Contact" << endl;
    	cout << setw(5) << left << "L)" << "List Contacts" << endl;
    	cout << setw(5) << left << "S)" << "Save Contacts" << endl;
    	cout << setw(5) << left << "E)" << "Exit" << endl << endl;
    	selection(Fname, Lname, PhoneNumber, email, count);
    
    }
    
    void selection(char Fname[][NAMES], char Lname[][NAMES], char PhoneNumber[][NUMBER], char email[][MAIL], int count )
    {
    \
    	char selected;
    	bool valid = false;
    	while (valid != true)
    	{			
    		cin >> selected;
    		switch(selected)
    		{
    			case 'A':
    			case 'a':valid = true, addContact(Fname, Lname, PhoneNumber, email, count);
    				break;
    			case 'D':
    			case 'd': valid = true, deleteContact(Fname, Lname, PhoneNumber, email, count);
    				break;
    			case 'F':
    			case 'f': valid = true, displayContact();
    				break;
    			case 'L':
    			case 'l': valid = true, listContacts();
    				break;
    			case 'S':
    			case 's': valid = true, saveContacts(Fname, Lname, PhoneNumber, email, count);
    				break;
    			case 'E':
    			case 'e': valid = true, exitPhonebook();
    				break;
    		}
    	}
    }
    
    void addContact(char Fname[][NAMES], char Lname[][NAMES], char PhoneNumber[][NUMBER], char email[][MAIL], int count ) 
    {
    	cout << count;
    	if (count < 100)
    	{
    		cout << "First Name of Contact: ";
    		cin >> Fname[count];
    		
    		cout << "Last Name of Contact: ";
    		cin >> Lname[count];
    
    		cout << "Phone Number of Contact (###-###-####): ";
    		cin >> PhoneNumber[count];
    
    		cout << "Email of Contact: ";
    		cin >> email[count];
    		count++;
    	}
    
    	else
    	{
    		cout << "Phone Book is full. Please delete a contact if you wish to add a new one." << endl;
    	}
    	displayMenu(Fname, Lname, PhoneNumber, email, count);
    }
    
    void deleteContact(char Fname[][NAMES], char Lname[][NAMES], char PhoneNumber[][NUMBER], char email[][MAIL], int count ) 
    {
    
    }
    
    void displayContact() 
    {
    	cout << "Yes it works" << endl;
    }
    
    void listContacts() 
    {
    	cout << "Yes it works" << endl;
    }
    
    void saveContacts(char Fname[][NAMES], char Lname[][NAMES], char PhoneNumber[][NUMBER], char email[][MAIL], int count ) 
    {
    	int tracking;
    	ofstream outFile;
    	outFile.open ("book.txt");
    	outFile << count << endl;
    	for (tracking = 0; tracking < count; tracking++)
    	{
    		outFile << Fname[tracking] << " " << Lname[tracking] << endl << PhoneNumber[tracking] << endl << email[tracking] << endl;
    		cout << PhoneNumber[tracking] << endl;
    	}
    	outFile.close();
    
    	cout << "You contacts have been saved successfully!" << endl;
    
    	displayMenu(Fname, Lname, PhoneNumber, email, count);
    }
    
    void exitPhonebook() 
    {
    	cout << "Have a wonderful day!" << endl;
    }
    
    void findContact(char Lname[][NAMES], int count, int find)
    {
    	
    }
    Thanks in advance and sorry for the first post. I appreciate it.

  4. #4
    Join Date
    Nov 2010
    Posts
    6

    Re: PhoneBook Phonenumber bug

    Also in book.txt I put a 0 so count would be accurate at the start.

    Is there an "edit post"? I didn't see one. I apologize if there is one.

    Thanks in advance!

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: PhoneBook Phonenumber bug

    Much better.

    The problem is you didn't leave enough room in your phone number for the NULL terminator. With dashes, your phone number is 12 characters long, the same size as your buffer. Make NUMBER 13 and I believe it will work.

  6. #6
    Join Date
    Nov 2010
    Posts
    6

    Re: PhoneBook Phonenumber bug

    Quote Originally Posted by GCDEF View Post
    Much better.

    The problem is you didn't leave enough room in your phone number for the NULL terminator. With dashes, your phone number is 12 characters long, the same size as your buffer. Make NUMBER 13 and I believe it will work.
    Thank you so much! Works perfectly now.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: PhoneBook Phonenumber bug

    Quote Originally Posted by Pyropenguin View Post
    Thank you so much! Works perfectly now.
    You're welcome.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured