Function using enumerated values and arrays in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ab12
    New Member
    • Aug 2008
    • 17

    Function using enumerated values and arrays in C

    I want this function (which is a part of a larger program) to read a bunch of letters (they will all be different letters) and assign the index of that letter in the array to carry the value TRUE. there are two arrays i want to do this do: set_A and set_B.
    actually, i made an enumeration type earlier in the program which defines all lowercase letters as type letters. it was necessary that i use the enum type.
    This is not working properly at all. It compiles but doesnt do what i want it to. i think the main problem is in the bold parts.

    Code:
    void getArray(bool set_A[],bool set_B[])
    {
         letters let;
         for (let=a; let<=z; let++)
         {
             set_A[let]=FALSE;
             set_B[let]=FALSE;
         }
         printf("Enter the string of letters for Set A: ");
    	 [B]while((let=getchar()) != EOF)
    	 {
             set_A[let]=TRUE;
             }[/B]
         printf("\nEnter the string of letters for Set B: ");
    	 [B]while(( let=getchar()) != EOF)
    	 {
            set_B[let]=TRUE;
             }[/B]
    }
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Why does it need to be a enumeration?
    Have you defined EOF too?

    Does it run infinitely inside the while loop?
    If not,what is the output you are getting?

    PS:Please be more descriptive when posting questions. Our posting guidelines are of great help there.

    Comment

    • ab12
      New Member
      • Aug 2008
      • 17

      #3
      No, it doesn't run infinitely. rather, i input the string of letters and then i press ENTER and nothing happens. i thought i would use EOF so when i pressed ENTER i could stop the input. i have to use enumeration types because that is a requirement for this assignment.
      what i want is the following: for example if i input 'acde' for set_A, i first want let=a so set_A[a]=TRUE, and then let=c so set_A[c]=TRUE and so on.

      Comment

      • ab12
        New Member
        • Aug 2008
        • 17

        #4
        Originally posted by Savage
        Why does it need to be a enumeration?
        Have you defined EOF too?

        Does it run infinitely inside the while loop?
        If not,what is the output you are getting?

        PS:Please be more descriptive when posting questions. Our posting guidelines are of great help there.
        No, it doesn't run infinitely. rather, i input the string of letters and then i press ENTER and nothing happens. i thought i would use EOF so when i pressed ENTER i could stop the input. i have to use enumeration types because that is a requirement for this assignment.
        what i want is the following: for example if i input 'acde' for set_A, i first want let=a so set_A[a]=TRUE, and then let=c so set_A[c]=TRUE and so on.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          If you inout an A you get 65. I assume then, that you set the bool in element 65 to true? Yes? So your array must be 96 to catch any character you can enter

          You see, from inside a function you can't tell the number of elements in an array. Due to decay of array, you don't even know there is an array.

          If you have a 26 element array for letters only and you enter A and you want the first element (which is element 0) of the array to be true, then you have to convert the A (a 65) to 0 using an algorithm that will convert B (66) to 1. Then the algorithm shoud also conver a (97) to 0 likewise. If you see what I mean.

          Comment

          • ab12
            New Member
            • Aug 2008
            • 17

            #6
            Originally posted by weaknessforcats
            If you inout an A you get 65. I assume then, that you set the bool in element 65 to true? Yes? So your array must be 96 to catch any character you can enter

            You see, from inside a function you can't tell the number of elements in an array. Due to decay of array, you don't even know there is an array.

            If you have a 26 element array for letters only and you enter A and you want the first element (which is element 0) of the array to be true, then you have to convert the A (a 65) to 0 using an algorithm that will convert B (66) to 1. Then the algorithm shoud also conver a (97) to 0 likewise. If you see what I mean.
            Thanks i got it to work. to make things simpler, i also changed the EOF to '\n' so i can just hit the RETURN key to end the input

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Glad to be of help.

              Be sure to let us know if you get stuck again.

              Comment

              Working...