Run-time error

I am getting a run-time error with this program and can't figure out what is wrong with my code. I basically created a class called bookType and created a program to test it out. The run-time error is occuring at lines 60-63, because the for loop isn't executing when it seems it should. Any advice on what has gone wrong is welcomed.
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <string>

using namespace std;

const int MAX_ARRAY_SIZE=4;

class bookType
{
public:
	void printTitle() const; //member functions dealing with the title
	void setTitle(string bookTitle);
	bool compareTitle(string compTitle) const;

	void amountOfCopies() const; //member functions dealing with the copies of book
	void setCopies(int a);
	void incrementCopies();
	void decrementCopies();
	int returnCopies() const;

	void setPublisher(string pub); //member functions dealing with publisher of book
	void printPublisher() const;
	bool comparePublisher(string compPublisher) const;

	void setIsbn(string isbn); //member functions dealing with ISBN of book
	void printIsbn() const;
	bool compareIsbn(string compIsbn) const;

	void setPrice(double bookPrice); //member functions dealing with price of book
	void printPrice() const;
	bool comparePrice(double compPrice) const;

	void setAuthors(string authorList[], int size); //member functions dealing with author of book
	void printAuthors() const;
	bool compareAuthor(string compAuthor) const;
	int returnAuthors() const;

private:
	string title;
	string author[4]; //objects of type bookType can contain up to 4 authors
	string publisher;
	string ISBN;
	double price;
	int numOfCopies;
	int numOfAuthors;
};

int main()
{

	bookType myBook;
	int amountOfAuthors;
	string title;
	string listOfAuthors[MAX_ARRAY_SIZE];
	
	cout << "How many authors does your book have?";
	cin >> amountOfAuthors;

	cout << "Enter the author(s) of your book: ";
	for(int index=0;index<amountOfAuthors;index++)
	{
		getline(cin,listOfAuthors[index]);
	}

	myBook.setAuthors(listOfAuthors, amountOfAuthors);
                
                cout << endl;

	cout << "Enter the title of your book: ";
	getline(cin,title);
	myBook.setTitle(title);

	cout << "The authors of ";
	myBook.printTitle();
	cout << "are ";
	myBook.printAuthors();

	return 0;
}

void bookType::printTitle() const
{
	cout << title << endl;
}

void bookType::setTitle(string bookTitle)
{
	title=bookTitle;
}

bool bookType::compareTitle(string compTitle) const
{
	if(title==compTitle)
		return true;
	return false;
}

void bookType::amountOfCopies() const
{
	cout << numOfCopies << endl;
}

void bookType::setCopies(int a)
{
	numOfCopies=a;
}

void bookType::incrementCopies()
{
	numOfCopies++;
}
void bookType::decrementCopies()
{
	numOfCopies--;
}

int bookType::returnCopies() const
{
	return numOfCopies;
}

void bookType::setPublisher(string pub)
{
	publisher=pub;
}

void bookType::printPublisher() const
{
	cout << publisher << endl;
}

bool bookType::comparePublisher(string compPublisher) const
{
	if(publisher==compPublisher)
		return true;
	return false;
}

void bookType::setIsbn(string isbn)
{
	ISBN=isbn;
}

void bookType::printIsbn() const
{
	cout << ISBN << endl;
}

bool bookType::compareIsbn(string compIsbn) const
{
	if(ISBN==compIsbn)
		return true;
	return false;
}
void bookType::setAuthors(string authorList[], int size)
{
	numOfAuthors=size;
	for(int index=0;index<numOfAuthors;index++)
		author[index]=authorList[index];
}

void bookType::printAuthors() const
{
	for(int index=0;index<numOfAuthors;index++)
		cout << author[index] << " ";
}

bool bookType::compareAuthor(string compAuthor) const
{
	for(int index=0;index<numOfAuthors;index++)
		if(author[index]==compAuthor)
			return true;
	return false;
}
Last edited on
This is not a run-time error. The code is doing what you told it to; it's just not what you expect.

Try changing all your uses of getline to cin >> instead (e.g. cin >>listOfAuthors[index];)
Oh, I just fixed it right now by adding cin.get(ch)
1
2
3
cout << "How many authors does your book have?";
	cin >> amountOfAuthors;
	cin.get(ch);

where ch is a char variable. I wasn't thinking about the newline character after hitting enter.
Last edited on
What happens if there are more than 9 authors?
Thanks...that just made me realize I need to add a loop..

1
2
3
4
5
6
7
8
cout << "How many authors does your book have?";
	cin >> amountOfAuthors;
	while(amountOfAuthors<=0 || amountOfAuthors >4)
	{
		cout << "Invalid amount of authors. Please reenter the number of author(s): ";
		cin >> amountOfAuthors;
	}
	cin.get(ch);
Last edited on
Topic archived. No new replies allowed.