This code executes fine as long as I keep the member lengthofvideo of structure Videocollection of type string. When I change it to type int I get an error. I want to change it to type int so that I can use the modulus operator on it's components.
Here are two code segments from my program that are most relevant to my question. The entire code is below it.
Code:
struct Videocollection
{
string nameofmovie[8];
string castmember1[8];
string castmember2[8];
string yearreleased[8];
int lengthofvideo[8]; //changing this to type int caused errors
// on a line in the for loop where
// I made comments
string blackandwhiteorcolor[8];
string moviedescription[8];
};
for(i=0; i<=7;i++)
{
getline(inmoviedata,eightmoviedatabase.nameofmovie[i]);
getline(inmoviedata,eightmoviedatabase.castmember1[i]);
getline(inmoviedata,eightmoviedatabase.castmember2[i]);
getline(inmoviedata,eightmoviedatabase.yearreleased[i]);
getline(inmoviedata,eightmoviedatabase.lengthofvideo[i]);// error
getline(inmoviedata,eightmoviedatabase.blackandwhiteorcolor[i]);
//getline(inmoviedata,eightmoviedatabase.moviedescription[i]);
cout<<setw(16)<<eightmoviedatabase.nameofmovie[i];
cout<<setw(17)<<eightmoviedatabase.castmember1[i]<<", ";
cout<<setw(17)<<eightmoviedatabase.castmember2[i];
cout<<setw(7)<<eightmoviedatabase.yearreleased[i]<<" ";
cout<<setw(4)<<eightmoviedatabase.lengthofvideo[i]<<" ";
cout<<setw(3)<<eightmoviedatabase.blackandwhiteorcolor[i]<<" ";
// cout<<eightmoviedatabase.moviedescription[i];
cout<<endl;
}
Code:
/*
Purpose: This program prints a table with data about 8 movies along with the average movie
length, maxiumum movie length, and minumum movie length.
*/
#include<iostream> // For I/O
#include<fstream> //For input file stream; movie data will be extracted from a file
#include<string> // For string
#include<iomanip>//For setw()
using namespace std;
int main()
{
ifstream inmoviedata; //file containg movie data
inmoviedata.open("C:/moviedata.txt"); //opening the file
//Datatype Videocollection declared and members are data elements of the movie, each
//being declared with appropriate datatype for that data element and array with the
//appropriate index, which corresponds to the number of data elements in the moviedata
//file of each member.
struct Videocollection
{
string nameofmovie[8];
string castmember1[8];
string castmember2[8];
string yearreleased[8];
int lengthofvideo[8];
string blackandwhiteorcolor[8];
string moviedescription[8];
};
Videocollection eightmoviedatabase;//Declaring variable of struct type Videocollection
//Program description for the reader
cout<<"This program prints out a table containing data about 8 movies.\n"
<<"Just below the table you can find the average movie length, \n"
<<"maximum movie length, and minumum movie length. The data in the\n "
<<"table was taken from \"The Internet Movie Database\" website\n "
<<"(http://www.imdb.com). Here is the table:\n"
<<endl<<endl;
//Formatting table
cout<<" Title"<<" "<<" Actors/Actresses"<<" "<<" Year"
<<" L(min)"<<" C?"<<endl;
cout<<"_________________________________________________________________________";
cout<<endl;
int i; //loop-control variable
//Note: The moviedata.txt file contains contains 56 lines of data. Lines 1-8 contain
//information for the first movie in the order it's members were declared in the struct.
//So the name of the first movie is on line one, a cast member on line two, and so on...
//Line 9 has the name of the second movie and the trend continues.
//This loop gets data for a movie and prints that infomration out on oneline and then
//gets information for another and so on.
for(i=0; i<=7;i++)
{
getline(inmoviedata,eightmoviedatabase.nameofmovie[i]);
getline(inmoviedata,eightmoviedatabase.castmember1[i]);
getline(inmoviedata,eightmoviedatabase.castmember2[i]);
getline(inmoviedata,eightmoviedatabase.yearreleased[i]);
getline(inmoviedata,eightmoviedatabase.lengthofvideo[i]);
getline(inmoviedata,eightmoviedatabase.blackandwhiteorcolor[i]);
//getline(inmoviedata,eightmoviedatabase.moviedescription[i]);
cout<<setw(16)<<eightmoviedatabase.nameofmovie[i];
cout<<setw(17)<<eightmoviedatabase.castmember1[i]<<", ";
cout<<setw(17)<<eightmoviedatabase.castmember2[i];
cout<<setw(7)<<eightmoviedatabase.yearreleased[i]<<" ";
cout<<setw(4)<<eightmoviedatabase.lengthofvideo[i]<<" ";
cout<<setw(3)<<eightmoviedatabase.blackandwhiteorcolor[i]<<" ";
// cout<<eightmoviedatabase.moviedescription[i];
cout<<endl;
}
//Making notes to follow up the asterik after Ace Ventura and Naked Gun 2 1/2
cout<<endl<<endl;
cout<<"* Note about Items with this symbol:"<<endl;
cout<<"The full name of the 2nd movie in the table is \"Ace Ventura: Pet Dectective\"";
cout<<endl<<"The full name of the 3rd movie is \"Naked Gun 2 1/2: The Smell of Fear\"";
return 0;
}