C++ Data Structures problems

Hi! I'm having problems with a program which should store information for a max. of 50 books (Option 1) and then show them (Option 2).

However it goes on loop on the line " cin >> libros [contador] . precio; " and I have no idea why. Any help?


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
#include <iostream>
#include <windows.h>
using namespace std;

struct Libro
{
	string titulo;
	string autor;
	int precio;
	string genero;
};

const int MAX = 50;
typedef Libro Prueba [MAX];

void PedirLibro (Prueba, int);
void MostrarLibro (Prueba, int);
int main ()
{
	Prueba libros;

	int selector;
	int contador = 0;

	cout << “Desea:” << endl;

	do { 

		cout << “Introducir un libro (1)” << endl;
		cout << “Mostrar los libros (2)” << endl;
		cin >> selector;
		
		if (selector == 1)
		{
			PedirLibro (libros, contador);

			contador ++; 
		}	
	 } while (selector != 2);

	MostrarLibro (libros, contador);

	system (“PAUSE”);
	return 0;
}

void PedirLibro (Prueba libros, int contador)
{
	getline (cin, libros [contador] . titulo);
	getline (cin, libros [contador] . autor);
	cin >> libros [contador] . precio;
	cin >> libros [contador] . genero;
	
	if (libros [contador] . genero == “1”)

		libros[contador].genero = “Narrativa”;

	else if (libros [contador] . genero == “2”)
		
		libros[contador].genero = “Poesia”;

	else if (libros[contador].genero == “3”)

		libros[contador].genero = “Ensayo”;
}

void MostrarLibro (Prueba libros, int contador)
{
	int i;

	for (i = 0; i <= contador; i++)
	{
		cout << libros [i] . titulo << endl;
		cout << libros [i] . autor << endl;
		cout <<libros [i] . precio << endl;
		cout << libros [i] . genero << endl;
	}
}
closed account (18hRX9L8)
A couple of errors (hopefully these will solve your problem):
1. Put prompts on lines 49-52 so the user knows what to enter.
2. std::cin tends to leave '\n's in the stream, so you have to ignore it using std::cin.ignore();
3. Change i <= contador (line 71) to i < contador because you increment 1 before you leave.
4. Change the do-while loop into a for loop from 0-50 so you don't overwrite your array.
5. Using system is evil. See: http://www.cplusplus.com/forum/articles/11153/ .
6. Get rid of lines 54-64 and add a prompt instead.
7. Change precio's type from int to float because it is used for price.

Overall, here is the new program with said changes:
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
#include <iostream>

struct Libro {
	std::string titulo, autor, genero;
	float precio;
};

static const int MAX = 50;
typedef Libro Prueba [MAX];

void PedirLibro (Prueba, int);
void MostrarLibro (Prueba, int);

int main (void) {
	Prueba libros;
	int selector, contador = 0;

	std::cout << "Desea:\n";

	for(int i = 0; i < MAX; i++) {
		std::cout << "\nIntroducir un libro (1).\nMostrar los libros (2).\n: ";
		std::cin >> selector;
		std::cin.ignore();
		
		if(selector == 1) {
			PedirLibro (libros, contador);
			contador ++; 
		} else {
			break;
		}
	}

	MostrarLibro(libros, contador);

	std::cin.ignore();
	return 0;
}

void PedirLibro (Prueba libros, int contador) {
	std::cout << "Titulo: ";
	getline(std::cin, libros[contador].titulo);
	std::cout << "Autor: ";
	getline(std::cin, libros[contador].autor);
	std::cout << "Precio: $";
	std::cin >> libros[contador].precio;
	std::cin.ignore();
	std::cout << "Genero (Narrativa, Poesia, o Ensayo): ";
	getline(std::cin, libros[contador].genero);
}

void MostrarLibro (Prueba libros, int contador) {
	for (int i = 0; i < contador; i++) {
		std::cout << "\nBook #" << i + 1 << ":\n";
		std::cout << libros[i].titulo << "\n";
		std::cout << libros[i].autor << "\n";
		std::cout << "$" << libros[i].precio << "\n";
		std::cout << libros[i].genero << "\n";
	}
}
Last edited on
Thanks!

Adding cin.ignore() solved the problem!!!

However I'm curious about why you used std:: for each cin and cout. Isn't it easier to just put using namespace std; at the start of the program? I'm guessing the answer is going to be similar to the system ("pause"); one.
closed account (18hRX9L8)
Good question! See: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice . The first two answers are pretty much the overview of why using using namespace ...; is not the best.

EDIT:
Also, mark the thread as completed if you all your questions are answered.
Last edited on
Topic archived. No new replies allowed.