Help - Error printing the solution
Jun 5, 2014 at 10:08am UTC
The program is finished, but it doesn't print correctly the solution, at the screen, can somebody help me ending this code?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
//alumno.h
#include <iostream>
using namespace std;
#include <iomanip> //para el setw()
#include <stdio.h> //para el gets()
class alumno
{
private :
char nombre[20];
double nota;
public :
alumno(char *n = "Si informacion" ,double not = 0.0);
void prt()//imprimir
{
cout<<"Alumno: " <<nombre<<setw(20)<<nota<<endl;
};
void get();//pedir
};
1 2 3 4 5 6 7 8
//alumnota.cpp
#include "alumno.h"
alumno::alumno(char *n, double not)
{
nombre[20] = *n;
nota = not;
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
//main.cpp
#include "alumno.h"
void main()
{
int numalumnos;
cout<<"Numero de alumnos = " ;
cin>>numalumnos;
cout<<endl;
//Objeto dinámico.
alumno *a = new alumno[numalumnos];
cout << "ALUMNOS " << endl;
cout<< "--------" <<endl;
for (int i = 0; i<numalumnos; i++)
{
cout<<endl;
char nom[20];
double nota;
cout<<"Nombre: " ;
cin.ignore();
gets(nom);
cout<<"Nota: " ;
cin>>nota;
a[i] = alumno(nom,nota);
}
for (int i = 0; i<numalumnos; i++)
{
a[i].prt();
}
delete [] a;
cout <<"\nTHE END" <<endl;
}
/*NOTA: En los bojetos dinámicos es mejor manejarse
unicamente con constrctores*/
Jun 5, 2014 at 11:15am UTC
In file alumnota.cpp, this line is an error:
Read what it is saying. The alumno object has a character array named
nombre
, which has a length of 20. Therefore valid subscripts are in the range 0 to 19. So
nombre[20] =
is attempting to assign something to the 21st character, it is outside the array. This will cause corruption of some other memory and undefined results.
You need to use
strcpy()
or better
strncpy()
to copy a c-string.
1 2 3
#include <cstring>
strncpy (nombre, n, 20);
There are a number of other issues.
void main()
is not valid C++. main()
must return an integer,
int main()
Putting
using namespace std;
in a header file while not an error, is usually considered to be bad practice and should be avoided.
Jun 5, 2014 at 1:09pm UTC
Thanks Chervil, really, I've solve the problem and learn to use strncpy in:
http://www.cplusplus.com/reference/cstring/strncpy/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//alumno.h
#include <iostream>
using namespace std;
#include <iomanip> //para el setw()
#include <stdio.h> //para el gets()
class alumno
{
private :
char nombre[20];
double nota;
public :
alumno(char *n = "Si informacion" ,double not = 0.0);
void prt()//imprimir
{
cout<<setw(10)<<nombre<<setw(10)<<nota<<endl;
};
void get();//pedir
};
1 2 3 4 5 6 7 8 9
//alumnota.cpp
#include "alumno.h"
alumno::alumno(char *n, double not)
{
//nombre[20] = *n; MAAAAALLLLLLLLLLLLLLLLLLLLLLLLL
strncpy(nombre,n,20); //BIENNNNNNNNNNNNNNNNNNNNNNN
nota = not;
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
//main.cpp
#include "alumno.h"
int main()
{
int numalumnos;
cout<<"Numero de alumnos = " ;
cin>>numalumnos;
cout<<endl;
//Objeto dinámico.
alumno *a = new alumno[numalumnos];
cout << "ALUMNOS " << endl;
cout<< "--------" <<endl;
for (int i = 0; i<numalumnos; i++)
{
cout<<endl;
char nom[20];
double nota;
cout<<"Nombre: " ;
cin.ignore();
gets(nom);
cout<<"Nota: " ;
cin>>nota;
a[i] = alumno(nom,nota);
}
for (int i = 0; i<numalumnos; i++)
{
a[i].prt();
}
delete [] a;
cout <<"\nTHE END" <<endl;
}
/*NOTA: En los bojetos dinámicos es mejor manejarse
unicamente con constrctores*/
Topic archived. No new replies allowed.