works, allows entering a user selected amount of strings, however when i try to display, it displays only the last string, not the whole 'database', whatever you'd call it :)
the main question is what is wrong and also, is there any way to not to ask a user to enter a number? maybe theres a way to auto expand the strings?

#include <iostream>
#include <string>
using namespace std;

class list
{
public:
    int iMemberCount;
    string *sName;
    string *sAge;

    void task();
    void newInfo();
    void preview();
    void memCount(int iTotalMemb);
    list();
};
//========================================================
list::list()
{   iMemberCount=0; }
//========================================================
void list::memCount(int iTotalMemb)
{   sName=new string[iTotalMemb];
    sAge=new string[iTotalMemb];
    iMemberCount = iTotalMemb;  }
//========================================================
void list::task()
{   cout<< "1. new profile \n";
    cout<< "2. preview \n";
    int iChoice;    cin>> iChoice;

    switch(iChoice)
    {    case 1: newInfo(); break;
         case 2: preview(); break;  }
}
//========================================================
void list::newInfo()
{   cout<< "name- "; cin>> *sName;
    cout<< "age- "; cin>> *sAge;
    cout<< endl;     }
//========================================================
void list::preview()   //these lines are responsible for display
{   for (int i=0; i<iMemberCount; i++)
    cout<< sName[i] << " " << sAge[i];  }
//========================================================
int main()
{
    list people;
    int iChc;
    cout<< "max number of people in the list- "; 
     cin>> iChc; //user must enter a number, any way to avoid this?
    people.memCount(iChc); 
    for (;;)
    {   int index=0; index++;
        people.task();
        if (index==iChc) break;
    }
    return 0;
}

Recommended Answers

All 5 Replies

lines 9 and 10: why are those pointers? The whole purpose of the std::string class is to remove get rid of pointers and memory allocations from coder's obligations. There is no need at all for those two pointers

lines 23 and 24: again, why? If you don't use the pointers in lines 9 and 10 you don't need to dynamically allocate them here.

If what you are doing with those two pointers is an attempt at making arrays of strings, then a better way is to use vectors, which again do not require you to do memory allocation stuff.

class list
{
public:
    int iMemberCount;

    vector<string> sName;
    vector<string> sAge;

    void task();
    void newInfo();
    void preview();
    void memCount(int iTotalMemb);
    list();
};

line 54: why ???? just code like this: int index = 1; line 56: how is index incremented ? Its impossible for index to anything other than 1 because of line 54.

accidently copied the wrong version of the code. ending should be

int main()
{
    list people;
    int iChc, index=0;
    cout<< "max number of people in the list- ";
     cin>> iChc;
    people.memCount(iChc);
    for (;;)
    {    index++;
        people.task();
        if (index==iChc) break;
    }
    return 0;
}

ill change strings with chars and see what happens next

you could have just made that a normal for-next loop

for(index = 0; index < iChk; index++)
{
   people.task(); 
}

To answer your question... it is not working because (in your current design) when you say cin>> *sName; it is the same as saying cin>> sName[0]; . The effect is overwriting the first person in the list each time you call newItem(); The quick fix for that problem in your current design would be to add a parameter to both task() and newItem() to pass along the index from the main loop.
Now some criticism/suggestions on your project in general: Firstly, I think you have a logic error in task() because index is incremented when you view the list, which
wastes" an entry in the list. I think you would benefit from crreating a Person class with the members name and age and maintain a single array of Person objects. Better yet, use vectors like that dragon person suggested. Also, if you continue to use dynamic memory, every new/new [] should have a matching delete/delete []. Your class should have a destructor, virtual ~list(); where you should delete[] your object's dynamic memory. Your switch should have a default: case where you can inform the user of an invalid choice. I would eliminate void list::task(); and handle the menu in main() . I would add a quit option to the menu. If you do not use vectors, you can eliminate the user input of the array size and dynamically size the array. You can do this by picking an initial size (32, 64, etc) and when the array is full: create a new array with double the size, copy the list from the old array to the new, delete [] the old array and point the old pointer to the new array. To do this you would need another int to keep track of the current list capacity in addition to the item count. Again, vector takes care of all of this stuff for you.

commented: brief comment with lots of useful info +1

Thanks for the brief explanation tymk. Did as you suggested (passing index) and it worked.
Now onto your criticism/suggestion list :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.