Thread: problems with function

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    5

    problems with function

    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: ");
        }
    Last edited by kills101; 12-09-2012 at 06:15 AM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I would suggest you to start learning using the class string of C++ .

    Could you please paste what the compiler says?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    i would suggest the thread moves to C
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I would suggest that the OP should stop using google to search for answers, then pestering us to fix it.
    Erreur à la compilation

    If you really want to learn (go up to the bathroom, look in the mirror, and convince yourself that you really do want to learn), then you'll make an honest attempt at solving the homework through your own efforts. Simply copy/pasting/nagging your way through college is just a big waste of time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    haha, i wonder if any of these copy and paste frankenstein coders ever actually consider the idea of 'maybe if i learn it will be easier to pass?'
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    haha, i wonder if any of these copy and paste frankenstein coders ever actually consider the idea of 'maybe if i learn it will be easier to pass?'
    Doubtful lol. Is funny that some of us enjoy coding and push to learn all this stuff on our own without college. And these guys in college copy paste for credits. Kind of scary really, but I have always wondered why I have waiters working for me at 2.13 with a bachelors degree. This might point to why. You can buy and cheat your way into a degree but its a lot harder to fool a competent employer.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    It's hilarious, because obviously this assignment is in C++, so the OP thought he could just #include <iostream> et voilà, C++!

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    mate i think that would have gone right over the op's head - that kind of thinking suggests a grasp of programming
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But perhaps we should also ask what language the OP really wants to learn - C or C++?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > This might point to why. You can buy and cheat your way into a degree but its a lot harder to fool a competent employer.
    Indeed - the magic piece of paper at the end of the course might get you a job, but it won't help you to keep it.

    Actually, the degree certificate is only good for one thing - and that's getting the invitation to your first job interview. Once you're at the interview (and later, on the job), you'd better actually know your stuff if you want to progress any further. If at the end of the course you can't "walk the walk", it's a hell of an expensive piece of paper if you're not up to holding down a relevant job.

    Student homework answers can be readily found on the net, but work assignments are unique and original. If work answers could be found using google, there would be no point hiring expensive programmers.

    See my sig-link for more details.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function problems
    By dahvid in forum C Programming
    Replies: 2
    Last Post: 05-07-2007, 08:56 AM
  2. Problems with a function...please help
    By Vireyda in forum C++ Programming
    Replies: 1
    Last Post: 03-25-2004, 08:15 PM
  3. function problems
    By c++amateur in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2004, 06:10 AM
  4. function problems
    By money in forum C++ Programming
    Replies: 5
    Last Post: 07-16-2003, 11:40 AM
  5. function problems
    By $0.05$ in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2003, 11:03 PM