Help with this car reviwer - CLASS

This is a exam of 2nd of Telecomunications Engeniering, I've done the exam of another year, but I can't find the solution to te following two mistakes:

1st)
error C2248: 'revision::dni' : cannot access private member declared in class 'revision' line 46


2nd)
error C2248: 'revision::fecha' : cannot access private member declared in class 'revision' line 60


//taller.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
#include <stdio.h>

class revision
{
	long dni;//Almacena el número de DNI del cliente sin letra.
	char fecha[15];//Se almacena como cadena de caracteres la fecha de la revisión
	int kilometros;//Kilometros recorridos hasta la fecha de revisión
	double coste;//Almacena el precio de la revisión.
public:
	void pedirdatos(void);
	void visualizar(void)
	{
		cout<<"DNI = "<<dni<<endl;
		cout<<"Fecha ="<<fecha<<endl;
		cout<<"Kilometros = "<<kilometros<<endl;
		cout<<"Coste = "<<coste<<endl;
	}	
};


//taller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "taller.h"

//Técnica Nº2 de agregar datos (función amiga con void)
void revision::pedirdatos(void)
{
	cout<<"DNI = ";
	cin>>dni;
	cout<<"Fecha = ";
	gets(fecha);/////////////////
	cout<<"Kilometros = ";
	cin>>kilometros;
	cout<<"Coste = ";
	cin>>coste;
}


//main.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "taller.h"

void main()
{ 
	int option = 0;

	int n = 0;
	cout<<"Numero de revisiones = ";
	cin >> n;
	revision *lista = new revision[n];

	int pasada= 0;

	while(1)
	{
		cout<<"\n\t\t***MENU***\n";
		cout<<"1)Agregar nueva revision"<<endl;
		cout<<"2)Buscar por DNI"<<endl;
		cout<<"3)Buscar por fecha"<<endl;
		cout<<"4)Mostrar base de datos"<<endl;
		cout<<"5)Salir\n";

		cout<<"->OPCION ==> ";
		cin>>option;

		if((option != 1)&&(option != 2)&&(option != 3)&&(option != 4)&&(option != 5))
		{
			system("cls");
			cout<<"No se contempla esa opción";
			exit(0);
		}
		else if((option == 1))
		{
			system("cls");
			lista[pasada].pedirdatos();
			pasada += 1;
		}
		else if((option == 2))
		{
			system("cls");
			int DDNNII;
			cout<<"Introduzca DNI del cliente = ";
			cin>>DDNNII;
			for (int i =0;i<n;i++)
			{
				if (lista[i].dni == DDNNII)
				{
					lista[i].visualizar();	
				}
			}
		}
		else if((option == 3))
		{
			system("cls");
			char FFEECCHHAA[15];
			cout<<"Introduzca fecha de revision = ";
			gets(FFEECCHHAA);
			for (int i =0;i<n;i++)
			{
				if (lista[i].fecha[15] == FFEECCHHAA[15])//////////////
				{
					lista[i].visualizar();	
				}
			}
		}
		else if((option == 4))
		{
			system("cls");
			for (int i = 0;i<n;i++)
			{
				lista[i].visualizar();
			}
		}
		else if((option == 5))
		{
			system("cls");
			cout<<"EXIT";
			exit(0);
		}
	}
}

/*NOTA: LA CLAVE ESTÁ EN ENTENDER QUE ESTAMOS TRABAJANDO 
EL RECUADRO ROJO DE LA  5ª CARA DEL RESUMEN.*/

dni is a private data member and can't be accessed outside the class object, i.e. Main() cannot access it. If you need access to it outside the class you would need to create getters and setters.

For example..
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
class revision
{
	long dni;//Almacena el número de DNI del cliente sin letra.
	char fecha[15];//Se almacena como cadena de caracteres la fecha de la revisión
	int kilometros;//Kilometros recorridos hasta la fecha de revisión
	double coste;//Almacena el precio de la revisión.
public:
	void pedirdatos(void);
	void visualizar(void)
	{
		cout << "DNI = " << dni << endl;
		cout << "Fecha =" << fecha << endl;
		cout << "Kilometros = " << kilometros << endl;
		cout << "Coste = " << coste << endl;
	}
	// setters/getters
	void setDni(long);
	long getDni();
};

void revision::setDni(long val)
{
	dni = val;
}

long revision::getDni()
{
	return dni;
}


// .... reset of code

it's a fantastic idea!! thanks really!!, but pleas can you expline something more about how to work with those functions in the "main()" ?
a
Last edited on
Ok!!! I've solve this problem, but now I've a problem ; it doesn't ask me to geve the text, and I don't know what more to do with this:

This is the code that needs only to be answered to the question that I ask previously:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//taller.h
#include <iostream>
using namespace std;
#include <stdio.h>

class revision
{
	long dni;//Almacena el número de DNI del cliente sin letra.
	char fecha[15];//Se almacena como cadena de caracteres la fecha de la revisión
	int kilometros;//Kilometros recorridos hasta la fecha de revisión
	double coste;//Almacena el precio de la revisión.
public:
	void pedirdatos(void);
	void visualizar(void)
	{
		cout<<"DNI = "<<dni<<endl;
		cout<<"Fecha ="<<fecha<<endl;
		cout<<"Kilometros = "<<kilometros<<endl;
		cout<<"Coste = "<<coste<<endl;
	}
	long getDni();
	char getFecha();
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//taller.cpp
#include "taller.h"

//Técnica Nº2 de agregar datos (función amiga con void)
void revision::pedirdatos(void)
{
	cout<<"DNI = ";
	cin>>dni;
	cout<<"Fecha = ";
	gets(fecha);/////////////////
	cout<<"Kilometros = ";
	cin>>kilometros;
	cout<<"Coste = ";
	cin>>coste;
}
long revision::getDni()
{
	return dni;
}
char revision::getFecha()
{
	return (fecha[15]);////////////////////
}


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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//main.cpp
#include "taller.h"

int main()
{ 
	int option = 0;

	int n = 0;
	cout<<"Numero de revisiones = ";
	cin >> n;
	revision *lista = new revision[n];

	int pasada= 0;

	while(1)
	{
		cout<<"\n\t\t***MENU***\n";
		cout<<"1)Agregar nueva revision"<<endl;
		cout<<"2)Buscar por DNI"<<endl;
		cout<<"3)Buscar por fecha"<<endl;
		cout<<"4)Mostrar base de datos"<<endl;
		cout<<"5)Salir\n";

		cout<<"->OPCION ==> ";
		cin>>option;

		if((option != 1)&&(option != 2)&&(option != 3)&&(option != 4)&&(option != 5))
		{
			system("cls");
			cout<<"No se contempla esa opción";
			exit(0);
		}
		else if((option == 1))
		{
			system("cls");
			lista[pasada].pedirdatos();
			pasada += 1;
		}
		else if((option == 2))
		{
			system("cls");
			int DDNNII;
			cout<<"Introduzca DNI del cliente = ";
			cin>>DDNNII;
			for (int i =0;i<n;i++)
			{
				if (lista[i].getDni() == DDNNII)
				{
					lista[i].visualizar();	
				}
			}
		}
		else if((option == 3))
		{
			system("cls");
			char FFEECCHHAA[15];
			cout<<"Introduzca fecha de revision = ";
			gets(FFEECCHHAA);
			for (int i =0;i<n;i++)
			{
				if (lista[i].getFecha() == FFEECCHHAA[15])//////////////
				{
					lista[i].visualizar();	
				}
			}
		}
		else if((option == 4))
		{
			system("cls");
			for (int i = 0;i<n;i++)
			{
				lista[i].visualizar();
			}
		}
		else if((option == 5))
		{
			system("cls");
			cout<<"EXIT";
			exit(0);
		}
	}
}

/*NOTA: LA CLAVE ESTÁ EN ENTENDER QUE ESTAMOS TRABAJANDO 
EL RECUADRO ROJO DE LA  5ª CARA DEL RESUMEN.*/
Last edited on
HELP please I don't know what else I can do

If you are referring to it skipping when asking for Fecha, then its because cin leaves the newline character in the stream - adding cin.ignore() to the next line clears/ignores the newline from the stream and will correct the problem.

i.e.

Add cin.ignore(1, '\n'); after cin >> dni;
Thaks REALLY softrix, you have help me a lot REALLY !!!
Your welcome :)
Topic archived. No new replies allowed.