String comparision

Hi people.
I've a problem with a piece of this program:
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
#include <iostream>
#include <string>
#include <string.h>
#include <cstdlib>
#define N 1

using namespace std;


struct index {
    short int ID;
    string title;
    string author;
    short int avn;
    short int 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;
            } else if (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;
            } else if (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;
    short int 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 :)
Last edited on
the bold line is ok. the problem is print_menu; 4 lines above. print_menu is a function, and when you call a function you have to add () at the end.
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".

What does it means?
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

This is the new source code:
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
175
#include <iostream>
#include <string>
#include <string.h>
#include <cstdlib> 
#define N 1

using namespace std;

struct index {
    short int ID;
    string title;
    string author;
    short int avn;
    short int lon;
};

index vet[N];
char wh[3];

void load_index(index vector[]);
void print_index(index vector[]);
void print_avbooks(index vector[]);
void update_index(index vector[], bool sc);
void find_autbooks(index vector[]);
void find_popularbook(index vector[]);
void print_menu();

// main
int main(int argc, char *argv[]) {
    load_index(vet);
    do {
    print_menu();

    cout << "Exit? (yesi/no): ";
    cin >> wh;
    } while (strcmp(wh, "no") == 0);
}

void load_index(index vector[]) {
    int i;
    for (i = 0; i < N; i++) {
        cout << "Insert book's title: ";
        getline(cin, vector[i].title); // http://www.cplusplus.com/reference/string/getline/
        cout << "Insert book's author: ";
        getline(cin, vector[i].author);

        vector[i].ID = i + 1;
        vector[i].avn = 3;
        vector[i].lon = 0;
    }
}

void print_index(index vector[]) {
    int i;
    cout << "ID    TITLE        AUTHOR     AV.COPIES" << endl;
    for (i = 0; i < N; i++) {
        cout << vector[i].ID << " " << vector[i].title << " "
             << vector[i].author << " " << vector[i].avn << endl;
    }
}

void print_avbooks(index vector[]) {
    cout << "Books of which is available at least one copy." << endl;

    int i;
    for (i = 0; i < N; i++) {
        if (vector[i].avn >= 1) {
            cout << vector[i].ID << " " << vector[i].title << " "
                 << vector[i].author << " " << vector[i].avn << endl;
        }
    }
}

void update_index(index vector[], bool sc) {
    string tmptit, tmpaut;

    cout << "Insert book's title: ";
    cin >> tmptit;
    cout << "Insert book's author: ";
    cin >> tmpaut;

    if (sc == true) {
        int i;
        for (i = 0; i < N; i++) {
            if ((tmptit == vector[i].title) && (tmpaut == vector[i].author) && (vector[i].avn < 3)) {
                vector[i].avn += 1;
            } else if (vector[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 == vector[i].title) && (tmpaut == vector[i].author) && (vector[i].avn > 0)) {
                vector[i].avn = vector[i].avn - 1;
                vector[i].lon = vector[i].lon + 1;
            } else if (vector[i].avn == 0) {
                cout << "Book not available." << endl;
            }
        }
    }
}

void find_autbooks(index vector[]) {
    string tmpaut;

    cout << "Insert author's name: ";
    cin >> tmpaut; cout << endl;
    cout << endl;

    int i;
    for (i = 0; i < N; i++) {
        if (vector[i].author == tmpaut) {
            cout << vector[i].ID << " " << vector[i].title << " "
                 << vector[i].author << " " << vector[i].avn << endl;
        } else {
            cout << "Match not found." << endl;
        }
    }
}

void find_popularbook(index vector[]) {
    int max;
    short int tID;

    max = 0;
    tID = 0;

    int i;
    for (i = 0; i < N; i++) {
        if (vector[i].lon > max) {
            max = vector[i].lon;
            tID = vector[i].ID;
        }
    }

    cout << "The most popular book is: " << vector[tID].title << endl
         << "written by: " << vector[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);
            break;
        case 'f':
            print_avbooks(vet);
            break;
        case 'a':
            find_autbooks(vet);
            break;
        case 'u':
            find_popularbook(vet);
            break;
        case 'r':
            update_index(vet, true);
            break;
        case 'p':
            update_index(vet, false);
            break;
    }
}

Can I still improve it?

P.S. The bold lines are those on which I have doubts :S
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.
Ok, thank you very much for help :)
Topic archived. No new replies allowed.