#include <iostream>
#include <string>
#include <string.h>
#include <cstdlib>
#define N 1
usingnamespace std;
struct index {
shortint ID;
string title;
string author;
shortint avn;
shortint lon;
};
index vet[N];
char wh[3];
void load_index(index vet[]);
void print_index(index vet[]);
void print_avbooks(index vet[]);
void update_index(index vet[], bool sc);
void find_autbooks(index vet[]);
void find_popularbook(index vet[]);
void print_menu();
// main
int main(int argc, char *argv[]) {
load_index(vet);
do {
print_menu;
cout << "Exit? (yes/no): ";
cin >> wh;
} while (strcmp(wh, "no") == 0);
}
void load_index(index vet[]) {
int i;
for (i = 0; i < N; i++) {
cout << "Insert title: ";
getline(cin, vet[i].title); // http://www.cplusplus.com/reference/string/getline/
cout << "Insert author: ";
getline(cin, vet[i].author);
vet[i].ID = i + 1;
vet[i].avn = 3;
vet[i].lon = 0;
}
}
void print_index(index vet[]) {
int i;
cout << "ID TITLE AUTHOR AV.COPIES" << endl;
for (i = 0; i < N; i++) {
cout << vet[i].ID << " " << vet[i].title << " "
<< vet[i].author << " " << vet[i].avn << endl;
}
}
void print_avbooks(index vet[]) {
int i;
for (i = 0; i <= N; i++) {
if (vet[i].avn >= 1) {
cout << vet[i].ID << " " << vet[i].title << " "
<< vet[i].author << " " << vet[i].avn << endl;
}
}
}
void update_index(index vet[], bool sc) {
string tmptit, tmpaut;
cout << "Insert title: ";
cin >> tmptit; cout << endl;
cout << "Insert author: ";
cin >> tmpaut; cout << endl;
if (sc == true) {
int i;
for (i = 0; i <= N; i++) {
if ((tmptit == vet[i].title) && (tmpaut == vet[i].author) && (vet[i].avn < 3)) {
vet[i].avn += 1;
} elseif (vet[i].avn == 3) {
cout << "You cannot return a book that you haven't request."
<< endl;
i = N - 1;
}
}
} else {
int i;
for (i = 0; i <= N; i++) {
if ((tmptit == vet[i].title) && (tmpaut == vet[i].author) && (vet[i].avn > 0)) {
vet[i].avn -= 1;
vet[i].lon += 1;
} elseif (vet[i].avn == 0) {
cout << "Book not available." << endl;
}
}
}
}
void find_autbooks(index vet[]) {
string tmpaut;
cout << "Insert author name: ";
cin >> tmpaut; cout << endl;
cout << endl;
int i;
for (i = 0; i <= N; i++) {
if (vet[i].author == tmpaut) {
cout << vet[i].ID << " " << vet[i].title << " "
<< vet[i].author << " " << vet[i].avn << endl;
} else {
cout << "Match not fund.\n";
}
}
}
void find_popularbook(index vet[]) {
int max;
shortint tID;
max = 0;
tID = 0;
int i;
for (i = 0; i <= N; i++) {
if (vet[i].lon > max) {
max = vet[i].lon;
tID = vet[i].ID;
}
}
cout << "The most popular book is: " << vet[tID].title << endl
<< "di: " << vet[tID].author << endl;
}
void print_menu() {
char m;
bool s;
cout << "Choose one of this option.\n"
<< "- Press 's' to print the index.\n"
<< "- Press 'f' to print the available books list.\n"
<< "- Press 'a' to find all books of the same author.\n"
<< "- Press 'u' to find the most popular book.\n"
<< "- Press 'r' to give back a book.\n"
<< "- Press 'p' to request a book.\n";
cin >> m;
switch (m) {
case's': print_index(vet);
case'f': print_avbooks(vet);
case'a': find_autbooks(vet);
case'u': find_popularbook(vet);
case'r': update_index(vet, true);
case'p': update_index(vet, false);
}
}
The bold line give me this error:
When I execute the program, and press "no" to exit, the program keeps asking to exit, in an endless round, like a loop.
Can you help me?
P.S. If you found some other errors or inaccuracies or if you want to give me some advice, please tell me :)
You're right. I'm used to Pascal :P
However the program continues give me errors yet.
During the execution, (in the function update,_index()), the system give me a message box with this error:
The instruction at "0x0042005c" has referred to the memory "0xfffffff4". The memory could not be "read".
I compiled the code but failed to get this error. Such error usually means that you're using uninitialized pointers. in this case, I think it is because you go out of bounds int your for loops (i <= N should be i < N).
Several other things I hadn't noticed:
1.Your functions have a local variable vet, but there is a global one too. there is no undefined behavior with this, it's just flawed design. You only need one of them, but your functions (theoretically) have access to both.
2.Your cases don't have breaks, therefore falling through happens a lot. (you might have noticed, that when you enter 's', other cases happen too)
Thank you very much. :)
I have correct all '<=' with '<' (is an error because the arrays, in C++, starts from the position 0, on the contrary, in Pascal, it starts from the position 1).
1. So, you think is better if I change the name of the local variables in the functions with another name?
2. Yes you're right, I have already correct it :P
In Pascal arrays start from wherever you want them to, if I recall correctly..
Wile it is better when global and local variables don't share one name, my idea wasn't to rename parameters. Since vet is a global variable, you don't need to pass it to anything at all. On the other hand, passing things to functions is often better than having globals (you'll see when you have a large project). If you want to do that, you should make vet a local array in main and pass it to print_menu(), or maybe a static variable in print_menu().
Though I suppose having a global would be a more simple way.