array function call problem

can't figure out why my case 2 will not simply print the arrays i read into from a text file. any suggestions?
movies.txt
Wizard of oz
1939
Deadpool
2016

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
  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);


int main() {
    bool done=false;
    int menu;
    const int MAXSIZE = 100;
    string title[MAXSIZE];
    int size;
    int year[MAXSIZE];
    while(!done){
        cout<< "Select one of the options below\n"
               "1: Read in Movies\n"
               "2: List titles, years to a screen\n"
               "3: List titles, years to a file\n"
               "4: Exit";
        cin>>menu;
        switch (menu){
            case 1:
                cout<<"Read from file\n";
                readFromfile("movies.txt", title, year, size );
                break;
            case 2:
                cout<<"Print to the screen\n";
                printScreen(title,year,size);
                break;
            case 3:
                cout<<"Read to a file\n";
                break;
            case 4:
                cout<<"You will now exit the menu.\n";
                done=true;
                break;
            default:
                cout<<"Invalid selection\n";
        }

    }

    return 0;
}

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;

        cout<<title_arr[numberOfmovies]<<" "<<year_arr[numberOfmovies]<<endl;
        numberOfmovies++;}
    }
void printScreen( string title_arr[], int year_arr[],  int numberOfmovies){

    for(int i=0; i<numberOfmovies;i++){
        cout<<"Title:"" "<<title_arr[i]<<", year:" " "<<year_arr[i];
    }cout<<endl;


   }
void writeToFile(string filename, string title_arr[], int year_arr[], int numberOfmovies){
    ofstream outfile(filename);
    outfile<<"Total number of movies: "<<numberOfmovies<<endl;
    for (int i = 0; i<numberOfmovies; i++){
        outfile<<"output.txt:"<<title_arr[i];
    }cout<<endl;
}

Hello bigskit13,

Maybe while I finish what you left out you could edit your OP and put in the "#include" files?

From what you posted it will not work because it will not compile.

Andy
I literally just rewrote the entire thing and this worked
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
#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);

int main (){

    bool done=false;
    int menu;
    const int MAXSIZE = 100;
    string title[MAXSIZE];
    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. Exit\n";

        cin>>menu;
        switch(menu){
            case 1:
                cout<<"Read from file\n";
                readFromfile("movies.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("movies_result.txt",title, year, size);
                break;
            case 4:
                cout<<"You will now exit menu.\n";
                done=true;
                break;
            default:
                cout<<"Invalid option.\n";

        }
    }



    return 0;
}

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;
        cout<<title_arr[numberOfmovies]<<" "<<year_arr[numberOfmovies]<<endl;
        numberOfmovies++;
    }cout<<endl;
}
void printScreen(string title_arr[], int year_arr[], int numberOfmovies){
    cout<<"Total number of movies: "<<numberOfmovies<<endl;

    for(int i=0; i<numberOfmovies; i++){
        cout<<"Movie Title: "<<title_arr[i]<<"Year: "<<year_arr[i]<<endl;
    } cout<<endl;
}
void writeToFile(string filename, string title_arr[], int year_arr[], int numberOfmovies){
    ofstream outfile(filename);
    outfile<<"Total number of movies: "<<numberOfmovies<<endl;

    for(int i=0; i<numberOfmovies;i++){
        outfile<<"Movie Title: "<<title_arr[i]<<", year: "<<year_arr[i]<<endl;
    }
    cout<<endl;
}

Hello bigskit13,

I am glad it works for you because it does not work in my VS2017 even when I changed the input file to have a "\r" at the end of the lines.
The code:
1
2
3
if (name.back() == '\r') {
	name.pop_back();
}
Did not help because of the way the file was read.It left to many "\r"s in the string and the string contained to much information.But since it works for you there is nothing more to say except that you need to fix the input file.
When it comes to the read function I will ask again: how do you know the file is open and usable?

Andy
Topic archived. No new replies allowed.