Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Using structs and functions with pointers and dynamically allocate.

$
0
0
I have to write a program that reads songs from a file and we don't know how many songs there are going to be in the file but we can have more than 25 mb of the size. So we have to dynamically allocate the struct. I'm stuck because I tried just checking if I'm calling the functions right before int main() and int main. It won't read it. and then when I try to dynamically allocate in the functions I don't know how to add that in with a pointer.
/*
 * prog2.cpp
 *
 *  Created on: Feb 27, 2013
 *      Author: mh1703
 */

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

const int MEMORY_SPACE = 25;

struct Song
{
	string title;
	string artist;
	float size;
};

void show_playlist(Song * ptr);
int remove_a_song(Song * ptr);
void cleanup(Song * ptr);
void shuffle(Song * ptr);
void exit(Song * ptr);

int main()
{
		int choice;
		Song *songarray;
		
		ifstream fin;
		fin.open("input_song.txt");

		if(!fin.is_open())
		{
			cout << "ERROR: Can not open file." << endl;
			return -1;
		}

		cout << "\t\t Welcome to My Ipod!\t\t" << endl;
		cout << "\t\t Please choose your options: \t\t" << endl;
		cout << "\t\t 1. Show the playlist \t\t" << endl;
		cout << "\t\t 2. Remove a song \t\t" << endl;
		cout << "\t\t 3. Cleanup my ipod \t\t" << endl;
		cout << "\t\t 4. Shuffle the playlist \t\t" << endl;
		cout << "\t\t 5. Exit \t\t" << endl;
		cint >> choice;
		
		if ( choice = "1" )
		{
			songarray = show_playlist();
		}
		else if ( choice = "2" )
		{
			songarray = remove_a_song();
		}
		else if ( choice = "3" )
		{
			songarray = cleanup();
		}
		else if ( choice = "4" )
		{
			songarray = shuffle();
		}
		else ( choice = "5" )
		{
			songarray = exit();
		}
		
	
	return 0;

}

void show_playlist();
{
	Song * my_song;
	getline(fin, my_song->title);
	getline(fin, my_song->artist);
	getline(fin, my_song->size);
	

int remove_a_song();

void cleanup();




Song *shuffle();
{
	Song *array_struct;
	
	array_struct = new Song[ ];
	
	srand();
}
	
void exit();


*** EDIT ***
Please use code tags when posting code
:code:

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>