Thread: Arrays & functions

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    6

    Talking Arrays & functions

    This Lab has to parts.I am done with the first part.The second parts which is the arrays and function ,I can't get the to work.Please some one help.

    Second part. Include the product description, the name of each item (Wilson Golf balls, Soloman Skis, HO Water Ski, etc.) Create a getstring() function to get the description and uppercase every letter of the entire description. You must create your own function to do this. You cannot use the strupr() function if it is available on your compiler. If someone enters: penn tennis balls, it is altered to: PENN TENNIS BALLS. (As a bonus, uppercase the first letter only of each word in the description, instead). Also modify the show() function to display the uppercased product descriptions with the other data.

    Sierra Sporting Goods

    Enter the product number: __
    Enter the product type : __
    Enter the description : ____________________
    Enter the quantity : __
    Enter the unit cost : __
    Enter the unit price : __

    The product number is --> ????
    The product type is ----> ?
    The description is -----> ??????????????????
    The quantity is --------> ???
    The cost is ------------> ???.??
    The price is -----------> ???.??

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    So, what's your question?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    I can't get the function for arrays to work.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The call should look like this:

    getstring ( desc );

    The functions should look like this:
    Code:
    /* Very bad code, but that's life. ;) */
    void getstring(char desc[])
    {
      puts("\tEnter the description : ");
      fflush(stdin);
      gets(desc);
      upcase(desc);
    }
    
    void upcase(char *s1)
    { 
      while(*s1)
        *s1++ = toupper(*s1);
    }
    Since you are obviously using a compiler that fflush works for input streams, it will suffice for your homework. But keep in mind that the C standard defines fflush for output streams, so fflush ( stdin ) is undefined whether it works or not. And you should consider using fgets instead of gets, it will check your array boundaries instead of overwriting memory you don't own. I won't bother detailing the flaws in your code since your instructor should be doing that, if she/he doesn't then come back and I'll be glad to give you a detailed description as well as ways to improve.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    @Shila:
    Well, there are arrays everywhere. Please, tell us the name of the function!

    Code:
    switch (key)
    {
        case '1': case 'A': add(); break;
        case '2': case 'R':
        case '3': case 'D':
        case '4': case 'C': ("\nNot Implemented"); break;
        case '5': case 'Q': more = FALSE; break;
        default : ("\nError in selection\a");
    }
    Is the code above a valid switch/case construct?
    And btw never forget to end each 'case' statement with break;.
    And in line 96 delete the second semicolon at the end of the line.
    Last edited by Sargnagel; 10-20-2002 at 11:35 AM.

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    The switch code. Those are from my instructor..By the way my instructor sucks I am a beginner in C ,I had to take this class because it was requirement.the getstring function and upcase function are my problems.

    Thanks

  7. #7
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Line 84 should look like this:
    Code:
    getstring(desc);
    This is my suggestion for your nemesis functions:
    Code:
    void getstring(char desc[])
    {
        puts("\tEnter the description : ");
        gets(desc);
        upcase(desc);
        puts(desc);
    }
    void upcase(char s1[])
    { 
        while(s1 && (*s1 != '\0'))
        {
            *s1 = toupper(*s1);
            s1++;
        }
    }
    I didn't test the code ... it may not work.

    @Prelude:
    Oh, I am very sorry! You already did the code for the two functions above!
    Last edited by Sargnagel; 10-20-2002 at 01:14 PM.

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    Hello;
    I did'nt work ,But thank you very much for trying.

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    I really appreciate everyone's, help you all have been great ,I got my messy codes to work.
    Thanks very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  2. Passing arrays of pointers into functions
    By ashley in forum C Programming
    Replies: 5
    Last Post: 01-13-2007, 06:48 PM
  3. storage class, arrays, functions and good layout
    By disruptivetech in forum C Programming
    Replies: 4
    Last Post: 12-02-2005, 02:34 PM
  4. functions and arrays
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-14-2002, 09:57 AM
  5. elements of arrays; functions
    By sballew in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 01:48 AM