1 Replies - 316 Views - Last Post: 03 April 2013 - 10:28 PM Rate Topic: -----

#1 BobSacamento  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 27-February 13

Problems with structures and file streaming

Posted 03 April 2013 - 08:57 PM

Hello super intelligent internet folk!

Being new to structures and file streaming, I keep on getting errors saying my structure variable (m) is undeclared.

Also I have the data field "movies.txt" in a notepad file right now. Do I have to make a separate C++ program so I can input that data?

Here is my code. Thanks again for any advice

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

struct movie
{
	int rank;
	string title;
	int year;
	string rating;
	string genre;	
	string director;
};

int loadMovies(movie []);

int main()
{
	movie films[600];
	int oPtion;
	int numMovies;
	unsigned found;
	
	numMovies = loadMovies(films);
	cout << "Loaded " << numMovies << " movies..." << endl << endl;

	cout << "What would you like to do?\n1. See movies for a year range\n2. See movies for a rating\n3. See movies for a director\n4. Exit" << endl;
	cin >> oPtion;

	if (oPtion == 1)
	{
		int startYear, endYear;
		cout << "What year to begin search?: ";
		cin >> startYear;
		cout << endl << "What year to end search?: ";
		cin >> endYear;
		
		for (int i=0; i<numMovies; i++)
		{
			if (startYear <= films[i].year <= endYear)
			{		
					cout << setw(4) << right << m.rank << " ";
					cout << m.title << " ";
					cout << m.year << " " << m.rating << " " << m.director << " " << m.genre << end;
				
			}
		}
	}
	else if (oPtion == 2)
	{
		string ratingString;
		cout << "What ratings would you like to see? (PG-13, PG, G, R)  : ";
		cin >> ratingString;

		for (int i=0; i<numMovies; i++)
		{
			found = films[i].rating.find(ratingString);
			
			if (found != string::npos)
			{
				cout << setw(4) << right << m.rank << " ";
				cout << m.title << " ";
		}		cout << m.year << " " << m.rating << " " << m.director << " " << m.genre << end;
			}
	}
	else if (oPtion == 3)
	{
		string directorString;
		cout << "What director would you like to see? (Type First intial and last name) : ";
		getline(cin, directorString);
		
		for (int i = 0; i <numMovies; i++)
		{
			found = films[i].director.find(directorString);

			if (found != string::npos)
			{
				cout << setw(4) << right << m.rank << " ";
				cout << m.title << " ";
				cout << m.year << " " << m.rating << " " << m.director << " " << m.genre << end;	
			}

		}
	
	   }

system("pause");
}


int loadMovies(movie m[])
{
	int count = 0;	
	string junk;

	ifstream inFile;
	inFile.open("movies.txt");
	
	if(!inFile)
		{
			cout << "Could not open file" << endl;
		}
	else
	{
		cout << "File Opened" << endl;
			
		

		inFile >> m[count].rank;
		
		while (!inFile.eof())
		{
			getline(inFile, junk, '|');
			getline(inFile, m[count].title, '|');
			inFile >> m[count].year;
			getline(inFile, junk, '|');
			getline(inFile, m[count].rating, '|');
			getline(inFile, m[count].genre, '|');
			getline(inFile, m[count].director, '|');

			count++;
			inFile >> m[count].rank;
		}

	inFile.close();
	}
	
	return count;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Problems with structures and file streaming

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1396
  • View blog
  • Posts: 4,858
  • Joined: 19-February 09

Re: Problems with structures and file streaming

Posted 03 April 2013 - 10:28 PM

I don't see any object/variable called m either in main, you probably want to use the films[] object. You have an m parameter in the loadmovies function,

When variables are created in a function they are only available in the function this is called scope, local scope as opposed to global scope.

This probably won't work as you intend, one part will resolve to a boolean which will be compared to a year.

if (startYear <= films[i].year <= endYear)

This post has been edited by #define: 03 April 2013 - 10:34 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1