Jump to content
  • Advertisement
Sign in to follow this  
Basket Case

Address Book Program - A Few Errors

This topic is 4885 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have attempted this program to learn streams, and I have finally finished the coding, but I'm afraid there are a few runtime errors during the program. It seems to be all one error though, so maybe it won't be very hard. Everytime ChangeFirstName, ChangeLastName, ChangeEmail,ChangePhone, or DisplayContact is called there is a runtime error. Now when I debug there is an access violation(segmentation fault), but I'm not sure what that means.
//Header Files & Namespace Declarations
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

//Function Prototypes
void Menu();
void DisplayContactList();
void ChangeFirstName(string Change, int contactNumber);
void ChangeLastName(string Change, int contactNumber);
void ChangeEmail(string Change, int contactNumber);
void ChangePhone(string Change, int contactNumber);
void DisplayContact(int ContactNumber);
void ContactDelete(int contactNumber);
bool AddContact();
bool ChangeContact();
bool DeleteContact();

//Address Struct
struct address{
       string firstName;
       string lastName;
       string email;
       string phoneNumber;
       };

int main()
{
    cout<<"Welcome to Personal Address Book Beta v1.0\n";
    
    int userInput;
    
    do
    {  
           Menu();
           cin>>userInput;
           cout<<"\n\n";
           
           switch(userInput)
           {
                            case 1:
                                 DisplayContactList();
                                 break;
                            case 2:
                                 AddContact();
                                 break;
                            case 3:
                                 DeleteContact();
                                 break;
                            case 4:
                                 ChangeContact();
                                 break;
                            default:
                                 break;
           }                              
    }while(userInput != 5);//While user doesn't want to quit.
    
    return 0;
}

void Menu()
{
     //Menu Output
     cout<<"1. Display the entire contact list\n";
     cout<<"2. Add a new contact\n";
     cout<<"3. Delete a personal contact\n";
     cout<<"4. Change a personal contact info\n";
     cout<<"5. Quit\n";
}

void DisplayContactList()
{
        char str[2000];
        fstream file_op("address.txt",ios::in);
        int i = 1;
        int s = 1;
        while(file_op >> str)
        {
                      if(i == 1)
                          cout<<s<<") "; 
                      cout << str <<endl;
                      if(i == 4)
                      {
                           i = 1;
                           cout<<endl;
                           s++;
                      }
                      else
                        i++;
        }
        file_op.close();
        cout<<endl;
}
bool AddContact()
{
     cout<<"What is the first name of your contact?\n";
     string FirstName;
     cin>>FirstName;
     cout<<"\nWhat is the last name of your contact?\n";
     string LastName;
     cin>>LastName;
     cout<<"\nWhat is the email of your contact?\n";
     string Email;
     cin>>Email;
     cout<<"\nWhat is the phone number of your contact?\n";
     string PhoneNumber;
     cin>>PhoneNumber;
     cout<<"\nIs this info correct?\n";
     cout<<FirstName<<" "<<LastName<<endl;
     cout<<Email<<endl;
     cout<<PhoneNumber<<endl<<endl;
     cout<<"Yes(Y) or No(N)"<<endl;
     string yesOrNo;
     cin>>yesOrNo;
     if(yesOrNo == "N")
                return 0;
     ofstream File("address.txt",ios::app);
     File<<FirstName<<endl<<LastName<<endl<<Email<<endl<<PhoneNumber<<endl;
     File.close();
     return 1;
}

bool DeleteContact()
{
     cout<<"Enter the number of the contact you want to delete.\nIf you do not know"
     <<" the number of contact then enter 0.\nYou can get the number by displaying the"
     <<" contacts from the main menu.\n\n";
     int number;
     cin>>number;
     if(number == 0)
               return 0; 
     ContactDelete(number);
}
bool ChangeContact()
{
     cout<<"Enter the number of the contact you want to delete.\nIf you do not know"
     <<" the number of contact then enter 0.\nYou can get the number by displaying the"
     <<" contacts from the main menu.\n\n";
     int number;
     cin>>number;
     if(number == 0)
               return 0;
     DisplayContact(number);             
     cout<<"Are you sure you want to edit this contact?(Y or N)\n";
     string userInput2;
     cin>>userInput2;
     if(userInput2 == "N")
            return 0;
     cout<<"Which part would you like to change?\n"
     <<"1) First Name\n2) Last Name\n3) Email\n4) Phone Number\n";
     int changeNumber2;
     cin>>changeNumber2;
     switch(changeNumber2)
     {
     case 1:
     {
          cout<<"What would you like it changed to?\n";
          string change6;
          cin>>change6;
          ChangeFirstName(change6, number);
     }
     break;
     case 2:
     {
          cout<<"What would you like it changed to?\n";
          string change7;
          cin>>change7;
          ChangeLastName(change7, number);
     }
     break;
     case 3:
     {
         cout<<"What could you like to change it to?\n";
         string change8;
         cin>>change8;
         ChangeEmail(change8, number);
     }
     break;
     case 4:
     {
         cout<<"What would you like to change it to?\n";
         string change9;
         cin>>change9;
         ChangePhone(change9, number);
     }
     break;
     default:    
                 return 0;
     break;
     }           
     return 1;      
}

void ChangeFirstName(string Change, int contactNumber)
{
     vector<address> Contacts;
     char str[2000];
     int s = 1;
     int i = 0;
     fstream file("address.txt",ios::in);
     while(file >> str)
     {
                if(s == 1)
                {
                     Contacts.push_back(address());
                     Contacts.firstName = str;
                }
                if(s == 2)
                     Contacts.lastName = str;
                if(s == 3)
                     Contacts.email = str;
                if(s == 4)
                {
                     Contacts.phoneNumber = str;
                    s = 1;  
                    i++;   
                }
     }
     file.close();
     contactNumber--;
     Contacts[contactNumber].firstName = Change;
     fstream file_op("address.txt", ios::trunc | ios::out);
     for(int l = 0; i < Contacts.size(); l++)
     {
             file_op<<Contacts[l].firstName<<endl;
             file_op<<Contacts[l].lastName<<endl;
             file_op<<Contacts[l].email<<endl;
             file_op<<Contacts[l].phoneNumber<<endl;
     }
     file_op.close();
}

void ChangeLastName(string Change, int contactNumber)
{
     vector<address> Contacts;
     char str[2000];
     int s = 1;
     int i = 0;
     fstream file("address.txt",ios::in);
     while(file >> str)
     {
                if(s == 1)
                {
                     Contacts.push_back(address());
                     Contacts.firstName = str;
                }
                if(s == 2)
                     Contacts.lastName = str;
                if(s == 3)
                     Contacts.email = str;
                if(s == 4)
                {
                     Contacts.phoneNumber = str;
                    s = 1;  
                    i++;   
                }
     }
     file.close();
     contactNumber--;
     Contacts[contactNumber].lastName = Change;
     fstream file_op("address.txt", ios::trunc | ios::out);
     for(int l = 0; i < Contacts.size(); l++)
     {
             file_op<<Contacts[l].firstName<<endl;
             file_op<<Contacts[l].lastName<<endl;
             file_op<<Contacts[l].email<<endl;
             file_op<<Contacts[l].phoneNumber<<endl;
     }
     file_op.close();
}
void ChangeEmail(string Change, int contactNumber)
{
     vector<address> Contacts;
     char str[2000];
     int s = 1;
     int i = 0;
     fstream file("address.txt",ios::in);
     while(file >> str)
     {
                if(s == 1)
                {
                     Contacts.push_back(address());
                     Contacts.firstName = str;
                }
                if(s == 2)
                     Contacts.lastName = str;
                if(s == 3)
                     Contacts.email = str;
                if(s == 4)
                {
                     Contacts.phoneNumber = str;
                    s = 1;  
                    i++;   
                }
     }
     file.close();
     contactNumber--;
     Contacts[contactNumber].email = Change;
     fstream file_op("address.txt", ios::trunc | ios::out);
     for(int l = 0; i < Contacts.size(); l++)
     {
             file_op<<Contacts[l].firstName<<endl;
             file_op<<Contacts[l].lastName<<endl;
             file_op<<Contacts[l].email<<endl;
             file_op<<Contacts[l].phoneNumber<<endl;
     }
     file_op.close();
}
void ChangePhone(string Change, int contactNumber)
{
     vector<address> Contacts;
     char str[2000];
     int s = 1;
     int i = 0;
     fstream file("address.txt",ios::in);
     while(file >> str)
     {
                if(s == 1)
                {
                     Contacts.push_back(address());
                     Contacts.firstName = str;
                }
                if(s == 2)
                     Contacts.lastName = str;
                if(s == 3)
                     Contacts.email = str;
                if(s == 4)
                {
                     Contacts.phoneNumber = str;
                    s = 1;  
                    i++;   
                }
     }
     file.close();
     contactNumber--;
     Contacts[contactNumber].phoneNumber = Change;
     fstream file_op("address.txt", ios::trunc | ios::out);
     for(int l = 0; i < Contacts.size(); l++)
     {
             file_op<<Contacts[l].firstName<<endl;
             file_op<<Contacts[l].lastName<<endl;
             file_op<<Contacts[l].email<<endl;
             file_op<<Contacts[l].phoneNumber<<endl;
     }
     file_op.close();
}
void ContactDelete(int changeNumber)
{
     vector<address> Contacts;
     char str[2000];
     int s = 1;
     int i = 0;
     fstream file("address.txt",ios::in);
     while(file >> str)
     {
                if(s == 1)
                {
                     Contacts.push_back(address());
                     Contacts.firstName = str;
                }
                if(s == 2)
                     Contacts.lastName = str;
                if(s == 3)
                     Contacts.email = str;
                if(s == 4)
                {
                     Contacts.phoneNumber = str;
                    s = 1;  
                    i++;   
                }
     }
     file.close();
     changeNumber--;
     Contacts.erase((Contacts.begin() + changeNumber));
          fstream file_op("address.txt", ios::trunc | ios::out);
     for(int l = 0; i < Contacts.size(); l++)
     {
             file_op<<Contacts[l].firstName<<endl;
             file_op<<Contacts[l].lastName<<endl;
             file_op<<Contacts[l].email<<endl;
             file_op<<Contacts[l].phoneNumber<<endl;
     }
     file_op.close();
}

void DisplayContact(int contactNumber)
{
     vector<address> Contacts;
     char str[2000];
     int s = 1;
     int i = 0;
     fstream file("address.txt",ios::in);
     while(file >> str)
     {
                if(s == 1)
                {
                     Contacts.push_back(address());
                     Contacts.firstName = str;
                }
                if(s == 2)
                     Contacts.lastName = str;
                if(s == 3)
                     Contacts.email = str;
                if(s == 4)
                {
                     Contacts.phoneNumber = str;
                    s = 1;  
                    i++;   
                }
     }
     file.close();
     contactNumber--;
     cout<<Contacts[contactNumber].firstName<<endl<<Contacts[contactNumber].lastName<<endl<<Contacts[contactNumber].email<<endl<<Contacts[contactNumber].phoneNumber<<endl;
}

Share this post


Link to post
Share on other sites
Advertisement
I ran your code, it seems that whenever you try to delete a contact the program bails out on you.

Looking at your code, and the error the debugger gives you, I'm pretty sure this is some type of fencepost error.

That means you are trying to write off the end of the vector. Remember, arrays and vectors start at element 0 and not element 1. We would say the vector had 7 elements, 1,2,3,4,5,6,7, but the compiler sees a vector of 7 elements as 0,1,2,3,4,5,6. I think that could be your problem, given the manipulations that are taking place.

Hope I've helped, somebody can probably provide a better answer than me.

ukdeveloper.

P.S. If you already knew about arrays starting at 0, I apologise for sounding patronising [smile].

Share this post


Link to post
Share on other sites
Yes, I'm aware that arrays start at zero, and no you didn't sound patronizing. As for that error, I'm nearly positive that isn't the case, because I subtracted their input by one when I try to delete that part of the vector, therefore making it start from zero. Kind of hard to write in english, but I tried.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!