Filling a struct array from a txt file

I am trying to fill a struct array[10] from a text file. Each line (x10) in the text file has 6 strings or int separated by a whitespace whitespace.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct DVDdata{
int rentalID;
string title;
int year;
string director;
string starring;
double rating;
string availability;

};

int main(){
DVDdata DVDlib[10]{

ifstream DVDlib("10DVDS.txt");
while(DVDdata >> rentalID >> title >> year >> director >> starring >> rating >> availability){


cout << "Tile: " << title << endl;
cout << " Released in: " << year << endl;
cout << " directed by: " << director << endl;
cout << " starring: " << starring << endl;
cout << " IMDB rating:: " << rating << endl;
cout << " Available? : " << availability << endl;
}
}
};


I am a beginner and getting stuck trying after setting up the struct and creating the array.

thanks
Last edited on
closed account (SECMoG1T)
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct DVDdata
 {
   int rentalID;
   string title;
   int year;
   string director;
   string starring;
   double rating;
   string availability;
 };

 int main()
 {
    DVDdata DVDlib[10];

   ifstream DVDstream("10DVDS.txt");
   int count=0;

   while(DVDstream >> DVDlib[count].rentalID >>DVDlib[count].title >> DVDlib[count].year >>DVDlib[count]. director >>DVDlib[count]. starring >>DVDlib[count]. rating >> DVDlib[count].availability)
    {
      cout << "Tile: " << DVDlib[count].title << endl;
      cout << " Released in: " <<DVDlib[count]. year << endl;
      cout << " directed by: " << DVDlib[count].director << endl;
      cout << " starring: " <<DVDlib[count]. starring << endl;
      cout << " IMDB rating:: " << DVDlib[count].rating << endl;
      cout << " Available? : " << DVDlib[count].availability << endl;
       ++count.;
    }
  }



Should work.
Last edited on
Thanks!

one last thing i would like to do is to call print all of the variables from one of the struct so i tried modifying the cout part of the code like this:

while(DVDstream >> DVDlib[count].rentalID >>DVDlib[count].title >> DVDlib[count].year >>DVDlib[count]. director >>DVDlib[count]. starring >>DVDlib[count]. rating >> DVDlib[count].availability)
{
cout << "Tile: " << DVDlib[1].title << endl;
cout << " Released in: " <<DVDlib[1]. year << endl;
cout << " directed by: " << DVDlib[1].director << endl;
cout << " starring: " <<DVDlib[1]. starring << endl;
cout << " IMDB rating:: " << DVDlib[1].rating << endl;
cout << " Available? : " << DVDlib[1].availability << endl;
++count.;
}

but I am displaying nothing to the screen
closed account (SECMoG1T)
We'll wait a second that loop shouldn't be used for printing elements, I would advice you to remove all that cout stuff from that loop , and if you want to print the individual objects pass the array to a function defined to do that

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void printall ()
 {
    ///use a loop to print all data;
 }

 void printone (DVDdata array [], int size)
 {
   int index;
   std::cout <<"enter the index you want to print :";
    std::cin>> index;

     if (index>=0&&index <size)
        {
           std::cout <<array [index].title ;.....///others
        }

      else
          std::cout <<"index not found\n";
  }

///BUILD On that
   
Topic archived. No new replies allowed.