Hi guys Im a newbie at programming and Im trying to accomplish this exercise (A CD DATABASE) but the problem is I can't understand the errors plaguing my code. Any help or advice is greatly appreciated.BTW I'm using visual studio 2012.
Code:
#include <iostream>
#define NO_CDS 100
#define TITLE_SIZE 60
#define ARTIST_SIZE 60
int trim_newline(char string[]);
int enter(char prompt[]);
int yesno(char prompt[]);
int read_int(char prompt[]);
float read_float(char prompt[]);
int read_string(char prompt[], char answer[], int MAX);
int output(char title[], char artist[], int tracks[], int album[], float price);
/*
*trim_newline()
*
*Remove the newline character from the string (if there is one)
*It is assumed that if there is a newline charater on a string,
*it will be the last character in the string (except for the null)
*/
void trim_newline(char string[])
{
if (string[strlen(string)-1] = '\n')
string[strlen(string)-1] = '\0';
}
/*
*enter()
*
*Give the user a piece of information and ask them to press enter
*/
void enter(char prompt[])
{
fputs(prompt, stdout);
fflush(stdin);
getchar();
}
/*
* yesno()
*
* Ask the user a question and prompt for a Y or N (or y or n) answer
* No other answer will be acceptable
* If Y/y
*/
int yesno(char prompt[]) //prompt should not include a question mark
{
char answer;
/*
* loop until a correct answer is given
*/
for(;;)
{
fputs(prompt, stdout);
fputs(" (Y/N)? ", stdout);
fflush(stdin);
scanf_s("%c",&answer);
/*
* proceess the answer
*/
answer = toupper(answer);
if (answer == 'Y')
return 1;
if (answer == 'N')
return 0;
printf("Error - only 'y/Y' or'n/N/ are allowed\n");
}
}
/*
* read_int()
*
*
* Ask the user a question and prompt for an integer answer
*/
int read_int(char prompt[])
{
int answer;
fputs(prompt, stdout);
fflush(stdin);
scanf_s("%d", &answer);
return answer;
}
float read_float(char prompt[])
{
float answer;
fputs(prompt, stdout);
fflush(stdin);
scanf_s("%f", &answer);
return answer;
}
/*
* read_string()
*
* Ask the user a question and prompt for a string answer
* MAX = size of "answer" including NULL terminating character
* Note: The answer parameter is MODIFIED by the function
*
*
*/
void read_string(char prompt[], char answer[], int MAX)
{
fputs(prompt, stdout);
fflush(stdin);
fgets(answer, MAX, stdin);
trim_newline(answer);
}
/*
*
* output()
*
* Display (using printf) the details of one CD
*/
void output(char title[], char artist[], int tracks, int album, float price)
{
puts("====================");
printf("Title: %s\n", title);
#ifndef NOARTIST
printf("Artist: %s\n", artist);
#endif
printf("Number of tracks: %d\n", tracks);
puts (album ? "Album" : "Single");
printf("Price: %.2f\n", price);
puts("====================");
}
int main()
{
char title[NO_CDS][TITLE_SIZE+1];
#ifndef NOARTIST
char artist[NO_CDS][ARTIST_SIZE+1];
#endif NOARTIST
int tracks[NO_CDS]; //number of tracks in the CD
int album[NO_CDS]; //boolean - is the CD an ALBUM?
float price[NO_CDS];
int count = 0; //how many CD's are being tracked
int i; //loop counter
puts("Welcome to the CD database.");
printf("You can store a maximum of %d CDs.\n",
sizeof price/sizeof price[0]);
/*
*Loop until they no longer wish to enter anymore CDs
*/
for(;;)
{
/*
*Ask them if they want to enter another CD
*/
if(yesno("\nHave you anymore CDs to enter"))
break;
printf("\nPlease enter the details of CD %d...\n\n", count + 1);
/*
*Read all the CD details
*/
read_string("Title? ",title[count], sizeof title[count]);
#ifndef NOARTIST
read_string("Artist? ",artist[count], sizeof artist[count]);
#endif NOARTIST
tracks[count] = read_int("Number of tracks? ");
album[count] = yesno("Is the CD an Album");
price[count] = read_float("Retail Price");
/*
* Check if we have filled up the array
*/
if(++count == NO_CDS)
{
enter("You have reached the limits of this program \nPress enter to continue: ");
break;
}
}
/*
* Output the cd details
*/
for ( i = 0; i < count; i++)
{
printf("\nThe details of CD %d are: \n", i+1);
output( title[i], artist[i], tracks[i], album[i], price[i]);
if(i<count - 1)
enter("\nPress ENTER to see the next set of details: ");
}
enter("\nPress ENTER to exit the program: ");
}