Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8442369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:49:44+00:00 2026-06-10T08:49:44+00:00

Hi I am working on a school project and I am having a hard

  • 0

Hi I am working on a school project and I am having a hard time with a particular function.
I have been working on it for a while, I would appreciate any type of input.

We have to use this function:

bool movieLibrary::readMovieInfo(ifstream& inFile)
{
    inFile>>rank>>year>>votes>>nationality;
    getline(inFile,nameMovie);

    if (rank < 1)
        return false;
    else
        return true;
}

My main function keeps giving the wrong output:

#include "movieLibrary.h"
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
movieLibrary myMovie[5];
ifstream inFile("myMovieLibrary.txt");

int i =0;

//my issue is here
while (myMovie[i].readMovieInfo(inFile))
{       
    i++;
    myMovie[i].readMovieInfo(inFile);   
}

for (int i=0;i<5;++i)
{
    myMovie[i].printMovieInfo("printList.txt");
}

return 0;
}

Here is the output,which should be the same as the input, but here is what I am getting:

3 2000 24446 b  Snatch
2 2008 1902 b  RocknRolla
5 2007 25510 a  American Gangster
-1 -858993460 -858993460 Ì 
-858993460 -858993460 -858993460 Ì 

Here is the input: myMovieLibrary.txt

3 2000 24446 b Snatch
2 2004 2872 b Layer Cake
2 2008 1902 b RocknRolla
4 1999 7661 b Lock,Stock and Two Smoking Barrels
5 2007 25510 a American Gangster
-1
rank year votes Nationality (b:british; a:american) name

Here is the MovieLibrary specification file:

#include <string>

class movieLibrary
{
public:
movieLibrary();
~movieLibrary();

//void readMovieInfo(std::ifstream&);
bool readMovieInfo(std::ifstream&);
void printMovieInfo(char*);
char getNationality();
int getRank();
bool operator>=(movieLibrary) const;
bool operator<(movieLibrary) const;

private:
int rank; //rank I gave to the movie in my library
int year; //year the movie came out
int votes; //the number of votes that yahoo users gave the movie
std::string nameMovie; //the name of the movie
char nationality; //nationality of movie: b for british and a for american
};

and the implementation class for MovieLibrary:

#include "movieLibrary.h"
#include <fstream>
#include <string>

using namespace std; // here you can use that.


movieLibrary::movieLibrary()
{
}

movieLibrary::~movieLibrary()
{
}

bool movieLibrary::readMovieInfo(ifstream& inFile)
{
    inFile>>rank>>year>>votes>>nationality;
    getline(inFile,nameMovie);

    if (rank < 1)
        return false;
    else
        return true;
}

void movieLibrary::printMovieInfo(char* outFileName)
{
std::ofstream outFile;
if(!outFile.is_open()) 
    outFile.open(outFileName, std::ios::app);
outFile<<rank<<" "<<year<<" "<<votes<<" "<<nationality<<" "<<nameMovie<<std::endl;

}
int movieLibrary::getRank()
{
return rank;
}

char movieLibrary::getNationality()
{
return nationality;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:49:46+00:00Added an answer on June 10, 2026 at 8:49 am

    Since you call readMovieInfo in your loop condition, and also in the body of your loop, you’ll call it twice for each index except 0.

    Since you increment i before the second call in each iteration, you’ll overwrite the second element with the third, the fourth with the fifth, and so on: you’ll lose every other movie from the input file. This is reflected in your output: the second and fourth movies disappear.

    Remember that your loop condition is tested at each iteration, so you need to be careful of any side-effects from the condition test.

    You could address this by removing one or the other calls to readMovieInfo (as an exercise try to do it both ways), but I’d prefer to see you write your while condition without calling readMovieInfo; once you’ve done this, consider whether a do/while loop might be better, and why.

    Finally, note that you should also check that you’re not reading more elements than your array can hold. When you do this, your loop condition becomes a more complicated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

while working on a school project i ran into a problem using javascript to
Im working on a school project and it's my first time on android development.
In a school project, I have implemented a working syncadapter for syncing contacts with
My team working on a php/MySQL website for a school project. I have a
greeting all i am working on school project and i have a problem with
I'm currently working on a school project in Eclipse (We have just started using
I'm working on my computing project for school, and I'm having trouble getting anything
I'm working on a school project, and I have to create a simple presentation
I'm working on a school project in which we would like to analyze the
I have a project I'm working on that's actually a school project that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.