Declaring Array at runtime
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Declaring Array at runtime

  1. #1
    Join Date
    Oct 2012
    Posts
    11

    Declaring Array at runtime

    I can not set the size of my array while running porgrama. Is there any way to do this in C + +?

    ---- code ------

    #include <iostream>
    #include <string>

    using namespace std;

    int MAX = 80;

    class aluno
    {
    private:
    char nome[80];
    int serie, grau;
    static int contador;
    public:
    aluno();
    aluno(char n[], int s, int g);
    void quantidade() {cout << "\nQuantidade de alunos: " << contador;}
    void imprime();
    };

    int aluno::contador;

    aluno::aluno(){
    cout << "\nNome: "; cin >> nome;
    cout << "\nSerie: "; cin >> serie;
    cout << "\nGrau: "; cin >> grau;
    contador++;

    }
    aluno::aluno(char n[], int s, int g)
    {
    strcpy(nome,n);
    serie = s;
    grau = g;
    contador++;
    }

    void aluno::imprime()
    {
    cout << "\nNome do Aluno: " << nome;
    cout << "\nNSerie do Aluno: " << serie;
    cout << "\nGrau do Aluno: " << grau;
    }

    int main()
    {
    int nr_alunos = 1;
    cout << "\nQuantos Alunos voce quer Cadastrar? "; cin >> nr_alunos;
    if (nr_alunos <= 0)
    exit;
    else
    aluno a[nr_alunos];

    return 0;
    }
    Last edited by Alex_Brazil; October 21st, 2012 at 05:25 PM.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,987

    Re: Declaring Array at runtime

    You can use new like
    char* nome = new char[80];

    or better yet just use the string class for string data or vector for other types.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Declaring Array at runtime

    Quote Originally Posted by Alex_Brazil View Post
    I can not set the size of my array while running porgrama. Is there any way to do this in C + +?
    Stop using arrays, as they cannot be resized. Use std::vector<T>, as that is what is designed for. The vector is a dynamic array class.
    Code:
    #include <vector>
    //...
    int main()
    {
        int nr_alunos = 1;
        cout << "\nQuantos Alunos voce quer Cadastrar? "; cin >> nr_alunos;
        if (nr_alunos <= 0)
           return;
        std::vector<aluno> a(nr_alunos);   // sets the size of the array to nr_alunos
       // Now you can use "a" just like an array.
    }
    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Oct 2012
    Posts
    11

    Re: Declaring Array at runtime

    Vector worked very well. Thanks for the help.

  5. #5
    Join Date
    Oct 2012
    Posts
    11

    Re: Declaring Array at runtime

    Another doubt. How to use <vector> can not access methods of the class aluno.

    --- Code-------

    std::vector<aluno> a(nr_alunos);
    cout << "\nTotal:" << a[1].quantidade();

  6. #6
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Declaring Array at runtime

    Quote Originally Posted by Alex_Brazil View Post
    Another doubt. How to use <vector> can not access methods of the class aluno.
    First, please use code tags when posting code.

    Second, your issue has nothing to do with vector. If you can't access a method, member, etc. it has everything to do with your class and how you set it up. If you can't access it with vector, you won't be able to access it no matter what you wind up using.

    The problem is that you're returning a void() and using that in cout.
    Code:
    #include <vector>
    #include <iostream>
    
    class aluno
    {
        private:
              char nome[80];
              int serie, grau;
              static int contador;
        public:
              aluno();
              aluno(char n[], int s, int g);
              void quantidade() {std::cout << "\nQuantidade de alunos: " << contador;}
              void imprime();
    };
    
    using namespace std;
    
    int main()
    {
       vector<aluno> a(4);
       cout << "\nTotal:" << a[1].quantidade(); // quantidade returns void!
    }
    What does it mean to output a void? That's why you're getting an error.

    Regards,

    Paul McKenzie

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center