Thread: Help in Code

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    Help in Code

    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;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, let's start with what gcc gives us, shall we?
    Code:
    $ gcc -Wall -Wextra -o cds cds.c
    cds.c:9: warning: type defaults to ‘int’ in declaration of ‘trimnewline’
    cds.c:9: warning: data definition has no type or storage class
    cds.c:10: warning: type defaults to ‘int’ in declaration of ‘enter’
    cds.c:10: warning: data definition has no type or storage class
    cds.c:13: warning: type defaults to ‘int’ in declaration of ‘read_string’
    cds.c:13: warning: data definition has no type or storage class
    cds.c:14: warning: type defaults to ‘int’ in declaration of ‘output’
    cds.c:14: warning: data definition has no type or storage class
    cds.c:24:22: error: Database.h: No such file or directory
    cds.c:26: warning: return type defaults to ‘int’
    cds.c: In function ‘trimnewline’:
    cds.c:27: warning: suggest parentheses around assignment used as truth value
    cds.c: At top level:
    cds.c:45: warning: return type defaults to ‘int’
    cds.c:57: warning: return type defaults to ‘int’
    cds.c:89: warning: return type defaults to ‘int’
    cds.c: In function ‘main’:
    cds.c:157: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long unsigned int’
    cds.c:193: warning: unknown conversion type character ‘D’ in format
    cds.c:193: warning: too many arguments for format
    So the first eight lines state that you have four function prototypes that are missing return types. Granted you are consistent and don't give return types to the functions either, but yet return something, you naughty person. Technically, you're allowed, but we shake our finger at you all the same. Line 27 notes that you have an assignment being used as a truth-value -- and I doubt that you intended that to be an assignment. The %d warning on 157 is probably ok; the one on 193 with %D is not.

    And since I couldn't compile it, I wasn't able to check whether your title and artist are actually on the same line. In the output function, they won't be, but I didn't look to see whether you don't print something out somewhere else.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    yah but the some whish dont have return values dunt have to return anything i tried everything still printed artist on same line but for the %D is just print error fixed that alrdy but i still dunno why artist gets in new line :S i made \n for it on the output

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM