This is what I have.
Code:
#include <stdio.h> // includes standard in out library
#include <stdlib.h> // includes standard library
#include <time.h> // includes system time library
#include <string.h> // includes the string library
#define ARRAYSIZE 15 // constant for the array size
#define BLANK ' ' // constant for blanks
#define QUITCHOICE 4 // constant to quit
#define STRINGSIZE 50 //constant for string sizes
// DATA TYPES
// assigns a data type cdName
typedef char CDName;
// assigns a data type cdName
typedef char String[STRINGSIZE];
// Assigns a data structure for file contents
typedef struct CDInfo
{
String artist;
String album;
int numTracks;
} CDInfo;
// FUNCTION PROTOTYPES
void PrintMenu(int*);
void ReadMusicData(CDInfo*);
void PlayInOrder( );
void PlayRandomOrder( );
void PlayOneTrack( );
void PrintClosing( );
/*==========================================================================*/
/* MAIN FUNCTION */
int main()
{
// data type declarations for program arrays
CDInfo info;
// integer variable for menu choice
int menuChoice = 0;
PrintMenu(&menuChoice);
// Error checks for 4
while (menuChoice != 4)
{
// Call to function that reads in the array
ReadMusicData(&info);
// Switch to process user input
switch (menuChoice)
{
// if 1 call to Play Function
case 1: //PlayInOrder( );
break;
// If 2 call to Play Random Fuction
case 2: //PlayRandomOrder( );
break;
// If 3 call to Play One Fuction
case 3: //PlayOneTrack( );
break;
}
// Call to print menu function
PrintMenu(&menuChoice);
}
// Checks for a 4 to quit
while (menuChoice == QUITCHOICE)
{
// Call to print closing function
PrintClosing();
// reset the menu choice to exit loop
menuChoice = 0;
}
// end of main returning 0
return 0;
}
/* ========================================================================== */
/* MENU PRINTING FUNCTION */
// This function recieves a variable integer from main and returns it by
// simulated reference using pointers. The value is then processed by main
// to determine what the user wants to do.
void PrintMenu(int *menuChoice)
{
// prints message
printf ("\n************************************************************\n"
" WELCOME TO THE JLH05G MUSIC PLAYER!\n"
"************************************************************\n\n");
// prints menu
printf ("Please Choose a Menu Choice 1, 2, 3, or 4.\n"
"Then You Will Be Asked What File To Open.\n"
"1: Play Current Playlist In Order\n"
"2: Play Randomized Playlist\n"
"3: Play One Song\n"
"4: Leave the Program\n");
// inputs menu choice
scanf("%d", menuChoice);
// makes sure menu choice is valid
while ((*menuChoice < 1) || (*menuChoice > 4))
{
// prints menu
printf ("\nInvalid Menu Choice!\n"
"Please Choose a Menu Choice 1, 2, 3, or 4\n"
"1: Play Current Playlist In Order\n"
"2: Play Randomized Playlist\n"
"3: Play One Song\n"
"4: Exit the Program\n");
// re inputs value
scanf("%d", menuChoice);
}
// echo prints users input
printf("You chose %d\n\n", *menuChoice);
// end of function return to main
return;
}// Function PrintMenu
/*==========================================================================*/
/* MUSIC FILE READING FUNCTION */
// This function reads from the file in to the array playList then reads from
// the file in to the typeCode to process what to store in the second array.
// It returns the arrays to main by simulated reference.
void ReadMusicData(CDInfo* info)
{
// int lcv;
// type for holding the file name
CDName fileName[STRINGSIZE];
// declares a file stream
FILE* inFile;
// print the message
printf("Please Enter The File Name You Would Like To Open\n");
// input users file
scanf("%s", fileName);
// echoprint user input
printf("You Entered %s\n", fileName);
// opens the file for read only
inFile = fopen(fileName, "r");
// test to make sure file opens
while (!fopen(fileName, "r"))
{
// Prints Message
printf("File Opening Failed!\n");
// print the message
printf("Please Enter The File Name You Would Like To Open\n");
// input users file
scanf("%s", fileName);
// echoprint user input
printf("You Entered %s\n", fileName);
// opens the file for read only
inFile = fopen(fileName, "r");
} //while statement
// prints the table
printf("Your File Contents # Title\n"
"------------------------------------------------\n");
// read in the title, author and then strip EOLNS
fgets ( info->artist, STRINGSIZE, inFile );
fgets ( info->album, STRINGSIZE, inFile );
info->artist [strlen (info->artist) - 1] = '\0';
info->album [strlen (info->album) - 1] = '\0';
fscanf(inFile, "%d", info->numTracks);
printf("Artist: %s\n", info->artist);
printf("Album: %s\n", info->album);
printf("Number of tracks: %d\n", info->numTracks);
while(info->numTracks > ARRAYSIZE)
{
printf("Too Many Tracks!!\n");
}
// for ( lcv = 0; lcv < ARRAYSIZE; lcv++ )
// {
//
// }
}// Function ReadMusicData
Now I am getting a debug error. It crashes when it goes to input with fgets. I am new to structs can someone tell me what I am doing wrong or point me to a good tutorial?
Also if the number of tracks is bigger than 15 can I just call to the function again like this?
Code:
while(info->numTracks > ARRAYSIZE)
{
printf("Too Many Tracks!!\n");
ReadMusicData(&info);
}
Here is a sample data file:
Code:
Rolling Stones
A Very Short CD to Begin Testing Your Program
1
Start Me Up
3:32