search array

when I choose number 4 from the menu below the search function outputs -1. what am I doing wrong?

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
#include <iostream>
#include<string>
#include<fstream>
using namespace std;

void readFromfile(string filename, string title_arr[], int year_arr[], int& numberOfmovies);
void printScreen(string title_arr[], int year_arr[], int numberOfmovies);
void writeToFile(string filename, string title_arr[], int year_arr[], int numberOfmovies);
void sortNameAscendingBubble (string title_arr[], int year_arr[],int& numberOfmovies );
int search(string title_arr[], string target, int& numberOfmovies);


int main (){

    bool done=false;
    int menu;
    const int MAXSIZE = 20;
    string title[MAXSIZE], target;
    int size=0;
    int year[MAXSIZE];
    while(!done){
        cout<<"Select one of the options below\n";
        cout<<"1. Read in Movies\n";
        cout<<"2. List Titles, years to a screen.\n";
        cout<<"3. List Titles, years to a file\n";
        cout<<"4. Search Movies by Title\n";
        cout<<"5. Sort Movies by Title\n";
        cout<<"4. Exit\n";

        cin>>menu;
        switch(menu){
            case 1:
                cout<<"Read from file\n";
                readFromfile("films.txt", title, year, size);
                break;
            case 2:
                cout<<"Print to the Screen\n";
                printScreen(title, year, size);
                break;
            case 3:
                cout<<"Write to a file\n";
                writeToFile("out.txt",title, year, size);
                break;
            case 4:
                cout<<"Search Movies by Title\n";
                readFromfile("films.txt", title, year, size);
                search(title, target,  size);
                break;
            case 5:
                cout<<"Sort Movies by Title\n";
                sortNameAscendingBubble (title, year, size);
                printScreen(title, year, size);
                break;

            case 6:
                cout<<"You will now exit menu.\n";
                done=true;
                break;
            default:
                cout<<"Invalid option.\n";

        }
    }

    return 0;
}
  int search(string title_arr[], string target, int& numberOfmovies) {

    char reply;
    string ans;
    reply = toupper(reply);
    do {
        cout << "Which movie are you looking for?\n";
        cin.ignore();
        getline(cin, target);
      
        for(int i=0; i<numberOfmovies; i++){
            if (target== title_arr[i]){
                cout<<"Title exists in list and is located at index: "<<i;
            }
            else
                cout<<-1;        }

        cout << "Would you like to search for another title?\n";
        cin>>reply;

    } while (reply != 'n');
}
Last edited on
1
2
3
4
5
6
7
8
9
input file films.txt:
Wizard of Oz
1939
Deadpool
2016
Top Gun
1986
Oscar
1991
readFromfile:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

void readFromfile(string filename, string title_arr[], int year_arr[], int& numberOfmovies){
    string name, y;
    int year;
    ifstream infile(filename);
    while(getline(infile, name)){
        if (name.back() =='\r'){
            name.pop_back();
        }

        getline(infile, y);
        year=stoi(y);
        title_arr[numberOfmovies]=name;
        year_arr[numberOfmovies]=year;
        numberOfmovies++;
    }cout<<"movies read into program\n";
    cout<<endl;
}
Change line 77-82 in your search() by:

1
2
3
4
5
6
7
8
9
bool titleFound = false;
for(int i=0; i<numberOfmovies; i++){
    if (target== title_arr[i]){
        cout<<"Title exists in list and is located at index: "<<i;
        titleFound = true;
        break;
     }
}
if( !title_found ) cout<<-1;

thanks nudero,

when I put that in I get
1
2
3
Which movie are you looking for?
Deadpool
-1Title exists in list and is located at index: 1Would you like to enter another title:
.

not sure why
ahh I figured it out. get rid of the ! before titlefound and it works. thanks a ton nudero!

cheers
Topic archived. No new replies allowed.