Thread: Pointer error

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    114

    Pointer error

    Hi, I am learning C++ from jumping into C++. There is a practice problem in pointer chapter "Write a program that lets users keep track of the last time they talked to each of their friends.
    Users should be able to add new friends (as many as they want!) and st
    ore the number of days ago that they last talked to each friend. Let users update this value (but don't let them put in
    bogus numbers like negative values). Make it possible to display the list sorted by the names of
    the friends of by how recently it was since they talked to each friend.
    "
    but the program is crashing sometimes giving segmantation fault. most of the time.also it can realocate memory only for the first time.it dont realocate if I again call for more.I am also confused about how and where should I use delete.
    my code is
    Code:
     #include <iostream>
    #include <string>
    using namespace std;
    void allocatemore(int *&p,string *&s,int x,int n)
    {
        string *s1=new string[x+n];
        int *p1=new int[x+n];
        for(int i=0;i<x;++i)
        {
            *(s1+i)=*(s+i);
            *(p1+i)=*(p+i);
        }
        for(int i=x;i<n;++i)
        {
            cout<<"Enter the of your new friends\n";
            cin>>*(s1+i);
            cout<<"Enter the last meeting date of your friend\n";
            cin>>*(p1+i);
        }
        p=p1;
        s=s1;
    }
    
    int main()
    {
    cout<<"Intially how many friends do you want?\n";
    int x; cin>>x;
    string *names=new string[x];
    int *days=new int[x];
    for(int i=0;i<x;++i)
    {
        cout<<"Enter the name of your friend\n";
        cin>>names[i];
        cout<<"Enter the last date of meeting\n";
        cin>>days[i];
    
    }
    while(1)
    {
        cout<<"If you want to add more friends,type more\n"
        <<"If you want to view the last date of meeting of any friend type friend\n"
        <<"And q to quit\n";
        string s;
        cin>>s;
        if(s=="more")
        {
            cout<<"How many more friends do you want?\n";
            int n;
            cin>>n;
            allocatemore(days,names,x,n);
            x+=n;
        }
        if(s=="friend")
        {
            cout<<"What is the name of your friend?\n";
            string sfr;
            cin>>sfr;
            for(int i=0;i<x;++i)
            {
                if(names[i]==sfr)
                {
                    cout<<"You have meet "<<sfr<<" "<<days[i]<<"days ago.\nDo you want to update the date?y or N\n";
                    char con; cin>>con;
                    if(con!='n'||con!='N')
                    {
                    int j; cin>>j;
                    days[i]=j;
                    }
                    break;
                }
            }
        }
        if(s=="q")
        break;
    
    }
    
    }
    Last edited by Tamim Ad Dari; 04-05-2013 at 09:12 AM.

  2. #2
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Do yourself a massive favor and learn how to use your debugger. It exists for exactly this purpose. It'll save you tons of time.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is a memory leak in that code. Multiple usage of operator new, with no corresponding operator delete. There is no in-built garbage collection.

    It would be easier to simply use the standard vector class and a few standard algorithms. No need to expose the user to the fact of memory reallocation occurring either. And it would be possible to achieve equivalent functionality (minus errors) in a fraction of the lines of code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Why do you do this:
    void allocatemore(int *&p,string *&s,int x,int n) instead of this: void allocatemore(int &p,string &s,int x,int n).

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Yap. I know vector class that would be easy. But the chapter in the book was about dynamically allocating more space using pointers. I want to have a clear idea about that,..So what should I do?

  6. #6
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Maybe you can declare an array of strings,a pointer to a string then make the pointer to point to the array,..that way you can avoid using the keyword 'new'.
    Last edited by Aslaville; 04-05-2013 at 10:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer Error
    By stefanturk in forum C Programming
    Replies: 6
    Last Post: 11-12-2012, 04:28 PM
  2. Pointer Error
    By abhishekcoder in forum C Programming
    Replies: 4
    Last Post: 02-08-2012, 03:44 PM
  3. Replies: 4
    Last Post: 10-31-2009, 07:18 PM
  4. Pointer error
    By Gaming in forum C++ Programming
    Replies: 8
    Last Post: 05-03-2008, 03:55 AM
  5. Pointer(s) Error
    By gsoft in forum C Programming
    Replies: 4
    Last Post: 11-25-2004, 06:25 AM