0

I'm 15 and I moved to Sweden. I don't have time to study c++, because I need to learn the language, but later (few months probably), I'll continue with 300 page book (it's 500, but in 3 years I learned (through YouTube mostly) about 200 pages of that book). I'm not willing to learn right now about vector, so if you give me answer with vector, if you could explain to me how it works, or give me link where it "simply" explains vector.

Anyway, being all said and done, here's my problem: I'm building in c++ program that on the start, it has 1 object(NAobj) where it will store your name and age, and then you can select option to continue to create another account, and so on. When you first run that program it makes Aobj array object and expands it every time when you want create new account. But I just can't get it to work.

I found how I can expand array of objects, but, as I said, it's not realy working...

How to expand an array dynamically in C++? {like in vector }

Here's my code:

#include <iostream>
#include <string>
#include <Windows.h>

using namespace std;
int AccountFinder(string findName, string accountNames);



class Account
{
public:
    Account()
    {

    }

    ~Account()
    {

    }

    string name = "-ERROR NAME-";
    int age = 18;


private:

};

Account NAobj;
int Asize = 1;

string AName;
string Iname;

int ASelector;

int main() {
    Account *Aobj = new Account[Asize];
    while (Iname.empty()) {
        cout << "Name: #";
        getline(cin, Iname);
        if (Iname.empty())
        {
            cout << '\a' << endl;
            MessageBox(NULL, "No name found\nPlease enter name", "Error 01", MB_ICONERROR);
            system("cls");
        } 
        else if (Iname == " ") {
            cout << '\a' << endl;
            MessageBox(NULL, "Non suported name", "Error 02", MB_ICONERROR);
            system("cls");
            Iname.clear();
        }
        else {
            string a;
            cout << "Crete account? y\\n #";
            getline(cin, a);
            if (a == "y") {
                AName += Iname;
                ////generatin account
                NAobj.name = Iname;
                Account *TAobj = new Account[Asize];
                if (Asize == 1) {
                    copy(Aobj, Aobj + Asize - 1, TAobj);
                    delete[] Aobj;
                    Aobj = TAobj;
                    copy(Aobj, Aobj + Asize - 1, &NAobj);
                }
                else {
                    copy(Aobj, Aobj + Asize - 1, TAobj);
                    copy(&NAobj, &NAobj, (TAobj + Asize - 1));
                    delete[] Aobj;
                    Aobj = TAobj;
                }

                ////
                AName += " ";
                cout << "Want to continue? y\\n #";
                getline(cin, a);
                if (a == "y") {
                    system("cls");
                    Iname.clear();
                }
                else {
                    AName.resize(AName.size() - 1);
                }
                Asize++;
            }
            else {
                cout << "Want to continue? y\\n #";
                getline(cin, a);
                if (a == "y") {
                    system("cls");
                    Iname.clear();
                }
                else {
                    AName.resize(AName.size() - 1);
                }
            }
        }
    }
    Iname.clear();
    cout << ">" << AName << "<" << endl;
    cout << AName << endl;
    string name;
    cin >> name;
    int ANumber = AccountFinder(name, AName);
    if(ANumber != -1)
        cout << ANumber << endl;
    else
        cout << "no account name: >" << name << "< found" << endl;*/
    cout << endl;
    system("pause");
}



int AccountFinder(string findName, string accountNames) {

    string compareName;
    int Account = 0;
    bool space = TRUE;
    int startPos = 0;
    int a = 0;

    while (1) {
        while (1) {
            if (accountNames.at(a) == ' ') {
                break;
            }
            else
                a++;
            if (a == accountNames.size() - 1)
                return -1;
        }
        compareName.resize(a - startPos);
        for (int b = startPos; b < a; b++) {
            compareName.at(b - startPos) = accountNames.at(b);
        }
        if (findName == compareName) {
            return Account;
        }
        a++;
        startPos = a;
        if (Account > 0) {
            1;
        }
        Account++;
    }
}

Asize - Account Size (indicate size of array)

Aobj - Account Object (array of account objects)

NAobj - New Account Object ("temporary" storage for account name, probably won't use that later...)

Program Output

p.s. Sorry for my bad english and i know my program is a mess, but i'm lazy to tidy it up.

danijel1023
  • 142
  • 1
  • 7
  • Hi Danijel and welcome to StackOverflow. Your English is very good ;) Have a look at this question: [Creation of dynamic array of dynamic objects in C](https://stackoverflow.com/questions/20303820/creation-of-dynamic-array-of-dynamic-objects-in-c) which explains how to use `std::vector`. – Ian Sep 28 '18 at 21:34

1 Answers1

0

The simplest solution to your problem is using a vector to store references to objects of the class. I recommend going to the "trouble" of learning how to use vectors. They are not as difficult to understand as you might fear, and are definitely one of the simplest dynamic data structures. This might help: https://www.geeksforgeeks.org/vector-in-cpp-stl/

DimK
  • 301
  • 4
  • 16
  • Thanks for quick response, i looked it up and it's actually easy. Changed few lines and now it's working great. That site goes to my -yes yes- list. – danijel1023 Sep 29 '18 at 09:06