-
October 21st, 2012, 05:22 PM
#1
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.
-
October 21st, 2012, 05:26 PM
#2
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.
-
October 21st, 2012, 07:37 PM
#3
Re: Declaring Array at runtime
Originally Posted by Alex_Brazil
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
-
October 22nd, 2012, 07:28 PM
#4
Re: Declaring Array at runtime
Vector worked very well. Thanks for the help.
-
October 22nd, 2012, 08:05 PM
#5
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();
-
October 22nd, 2012, 08:16 PM
#6
Re: Declaring Array at runtime
Originally Posted by Alex_Brazil
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|