I did this project in my instructor told me about but problem is
everytime title and Artist keep going on same line they dunt
make new line
they show as Title: Artist: and i did \n for both in my output function please help thanks
Code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define NO_CDS 100
#define TITLE_SIZE 60
#define ARTIST_SIZE 60
#define IWANTARTIST
#define NOARTIST
trimnewline(char string[]);
enter(char prompt[]);//prototype of enter function
int YesnoFUNCTIOn(char prompt[]);
float read_float(char prompt[]);
read_string(char prompt[],char answer[],int MAX);
output(char Title[],char Artist[],int Tracks,int Album,float Price);
int Read_Intfunction(char prompt[]);
/*
TRIM_NEWLINE()
*
*Remove THE NEW line character from a string (if there is one)
*IT is assumed that if there is a new line on the string,
*it will be the last character in the string(Take that and replace it with A
NULL
*/
#include "Database.h"
trimnewline(char string[])
{
if(string[strlen(string)-1]='\n')/*here we doing our testing to see if last
*last line is A new Line
*take that line and replace it with a NULL
*/
string[strlen(string)-1]='\0';//here we do the replacing
return 0;
}
/*
Enter Function
*GIVE THE USER a piece of information and ask them to press ENTER
*/
enter(char prompt[])/*it FPUTS the prompt the of the user choice then it shows it
*then fflush stdin
*/
/*
* -------------------------
------------------------------
---------- */
{
fputs(prompt,stdout);
fflush(stdin);
getchar();
return 0;
}
/*
*output()
*
*DISPLY (USING printf) the detal of one cd
*/
output( char Title[] , char Artist[], int Tracks, int Album, float Price)
{
printf("Title: %s \n",Title);
#ifdef NOARTIST
printf("Artist: %s \n",Artist);
#endif
printf("Number of tracks is: %d\n",Tracks);
puts(Album? "Album": "Single");
printf("Price is %f\n",Price);
return 0;
}
/*
*read_int()
*Ask the user a question and prompt for an integer answer
*
*/
int Read_Intfunction(char prompt[])
{
int answer;
fputs(prompt,stdout);
fflush(stdin);
scanf("%d",&answer);
return answer;
}
/*
*Read String Function
*ASK The user the qeustion and prompt with the answer
*MAX=SIZE OF ANSWER INCLUDING null terminating character
*Note: The answer is parameter modified by function
*
*/
read_string(char prompt[],char answer[],int MAX)//dont forget to do the introuduction to me tommrow duffes
{
fputs(prompt,stdout);
fgets(answer,MAX,stdin);
trimnewline(answer);
return 0;
}
/*read_float*/
float read_float(char prompt[])
{
float answer;
fputs(prompt,stdout);
fflush(stdin);
scanf("%f",&answer);
return answer;
}
/*
*yesno()
*Ask the user a question and prompt for A y or n lower or upper case>>answer
*No Other answers will be acceptable we can also stick it in a forever loop(MY Fav)
*IF y/Y is given the function returns 1. n/N gives 0
*/
int YesnoFUNCTIOn(char prompt[])/*Remeber here we made char prompt coz we will build our fputs in
*THE main program like we can change question as we like
*WHISH IS very intelgent MOVE Made by mowa :D*
*/
{
char answer;
/*
*LOOP if the user put BAD ANSWER
*/
for(;;)
{
fputs(prompt,stdout);/*DISPLAYS THE QUESTION*/
fputs(" (Y/N)? ",stdout);/* I puted a ? asker
to help them
*/
fflush(stdin);//flushing away unwanted characters
scanf("%c",&answer);
/*
*Process the answer
*/
answer=toupper(answer);//convert to uppercase
if(answer=='Y')/*if Answer is Y return 1
to continue ongoing for to continue saving
up the CD
*/
return 1;
if(answer=='N')/*If answer iS N return 0
it will exit the program
*/
return 0;
else
printf("Error - Only 'y/Y' Or 'n/N' are allowed\n");
}
}
int main(void)
{
char Title[NO_CDS][TITLE_SIZE+1];
#ifdef NOARTIST
char Artist[NO_CDS][TITLE_SIZE+1];
#endif
int Tracks[NO_CDS];/*Number Of tracks of the Cd*/
int Album[NO_CDS];/*Boolean-Is the Cd an ALBUM?*/
float Price[NO_CDS];
int count=0;/*Check how many Cds are being Tracked*/
int i;/*Loop counter*/
puts("Welcome To CD DATABASE.");
printf("You can store maximum of %d CDs\n",sizeof Price
/ sizeof Price[0]);
/*
*Loop untill they no longer whish to Enter anymore CDs
*/
for(;;)
{
if(!YesnoFUNCTIOn("\n Have you anymore CDS to enter"))/*HERE we made if its not = yesno
then Break*/
break;
printf("\nPlease Enter the detail of CD %d...\n\n",count+1);
/*
*READ ALL CD DETAILS
*/
read_string("Title: ",Title[count],sizeof Title[count]);
fflush(stdin);
#ifdef NOARTIST
read_string("Artist: ",Artist[count],sizeof Artist[count]);
#endif
Tracks[count]=Read_Intfunction("Number of Trackss? ");
Album[count]=YesnoFUNCTIOn("Is the Cd an album");
Price[count]=read_float("Retail price (E.G. 4.65? ");
if(++count==NO_CDS)/*NOTE:
*Increment happens before the
*TEST
*/
{
enter("You have reached the limits of this Program\n"
"Press Enter to Continue: ");/*in C its perfectly okay to have
*alots of space or 1 space But in C if u have to getgars C literal
*and there Next to each Other and it will treat that it will join together
*them for you*/
break;}
}
for(i=0;i<count;i++)
{
printf("\n Details of CD %D is: \n",i+1);
output(Title[i],Artist[i],Tracks[i],Album[i],Price[i]);
if(i<count-1)/*only do this if there any more cds to SEE*/
enter("\nPress Enter To see next set of details: ");
}
enter("\nPress Enter to Exit the program: ");
return 0;
}