1 Replies - 596 Views - Last Post: 17 May 2012 - 02:08 AM Rate Topic: -----

#1 BeeBree  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-April 12

How do I get my file read into a dynamically allocated array&struc

Posted 17 May 2012 - 12:41 AM

I am writing a code where I need to read in a .txt file that looks like this:
The Avengers
8.2
192797249.00
The Pirates Band of Misfits
7.2
20851050.00
The Five Year Engagement
6.5
27535632.00
The Cabin in the Woods
8.0
23250721.00
The Hunger Games
7.5
54647565.00
How do I get my file read into a dynamically allocated array and structure?


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



struct allMovies
{
	string movieName;
	double rating;
	double profit;
};
void getData(allMovies p[5]);
double findPartRate();
double highestRating();
double highestBox();
void printAll();
int main()
{
	ifstream infile;
	allMovies choice1Name;
	int menuChoice;
	string movieName;


	allMovies *movie = new allMovies[5];

	infile.open("C:\\Users\\Bri\\Downloads\\movies.txt");

	if (!infile)
	{
		cout <<"Unable to open files."<<endl;
		cout <<"End of program"<<endl;
		return 0;
	}
	for( int index=0; index < 15; index++)
	{
		infile >> (*movie).movieName[index];
		infile >> (*movie).rating[index];
		infile >> (*movie).profit[index];

	}






    cout << "\t\tMovie Theatre Option Menu\n\n"
		 << "1. Find a particular rating for a movie\n"
		 << "2. Find the highest movie rating\n"
		 << "3. Find the highest box-office rating\n"
		 << "4. Show every movie playing\n\n"
		 << "Please enter a menu choice of 1, 2, 3 or 4: ";
	cin >> menuChoice;
	switch(menuChoice)
	{
		case 1: 
			cout << "You entered menu choice 1\n"
				 << "Enter the movie name to find a rating: ";
			cin.ignore();
			getline(cin, choice1Name.movieName);
			

			
			break;
		case 2:
			cout << "You entered menu choice 2";
			break;
		case 3:
			cout << "You entered menu choice 3";
			break;
		case 4:
			cout << "You entered menu choice 4"<< endl;
			while (infile >> movieName)
			{ 
				
				cout << movieName;
			}
			break;

		default:
			cout << "The valid menu choices are 1 through 4. Please start again.\n";

	}
	
	







	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: How do I get my file read into a dynamically allocated array&struc

#2 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: How do I get my file read into a dynamically allocated array&struc

Posted 17 May 2012 - 02:08 AM

Look at here:
(*movie).movieName[index]

You are indexing the member of the structure using the pointer rather than indexing the 5 structure you created.You have to index those 5 memory location either
(*(movie+index)).movieName;

Or Simply:
movie[index].movieName;

Also you have created 5 location but your loops iterate 15 times.You need to correct it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1