Thread: Beginner needs help with arrays.

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    Question Beginner needs help with arrays.

    Hello all. Could anyone please give me a little assistance? I have finally gotten my code to compile but it gives me wierd output. I am included sample output of what the output should be. I get the first character to go into the array but it stops there for some reason... any help would be grateful.....

    Code:
    #include <stdio.h>
    #define LIMIT 20
    void add_char(char*, char, int*);                          
    
    int main()  {
       int z, count=0; 
       char ch, A[21];   
      
       printf("Enter up to 20 unique characters; quit with two control-Ds: ");
       scanf(" %c", &ch);
       add_char(A,ch, &count); 
       
       for (z=0; z<LIMIT; z++)  
         printf("%c",A[z]);
       printf("\n");
     }
    
    
    
    void add_char(char A[], char ch, int *count)  {
    int z,p=0;
    
       for(*count=0; *count<LIMIT; (*count)++)  {
         for(z=0; z<LIMIT; z++)  {
           if (ch==A[z])  
             p=-10;
           }
         if (p!=-10)  {
            A[*count]=ch;
            p=0;
            } 
         else  
            p=0;
       }
    }
    SAMPLE OUTPUT:

    Enter up to 20 unique characters; quit with two control-Ds:
    The author are Jeri Hanly and Elliot D. Koffman.
    TheautorsJiHnlydED.K


    Thanks.

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    I got very lost with what you wanted to do, and how you wanted to do it. (But I'm still just a dabler). That having been said, I think this code does what it appears you're trying to do with your code. I just don't quite get why you're trying to re-invent the wheel. Maybe someone will come along that better understands what you're trying to do, in the meantime, see if this helps...

    Code:
    #include <stdio.h>
    
    int main(void)  
    {
       int z=0;
       char A[21]={0};
    
       printf("Enter up to 20 unique characters\n");
    
       fgets(A, 20,stdin);
    
       for (z=0; z<sizeof(A); z++) 
         printf("%c",A[z]);
       printf("\n");
    
       return 0;
    }
    Last edited by Azuth; 10-25-2002 at 09:03 PM.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    Clarification....

    A user inputs several sentences / phrases. My program is supposed to take the input character by character and store them in an array that can only hold 20 characters. Before the character can be placed in the array, the program must first check the character and make sure it is not already in the array, if it is, it is simply discarded; if it isn't, it is place inside the array and then the checking goes to the next character and so on until either the array is full or there is no more input (EOF). In the function however, I cannot use fgets or anything other than loops. Does this help??? can anyone help me fix my code/??? Thanks for your input... i am still too new to this programming thing, so if you would, please dumb down to my level... i thank anyone and everyone who attempts to help me. again thanks

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use fgetc in a loop:

    c = fgetc( stdin );
    ...test conditions...
    ... deal with results...

    Alternately you could use something like scanf, getc, getchar, etc etc.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    53
    I am not allowed to use fgetc or stdin ... only loops with scanf. I don't even know what the other is...... ???? now I am really confused.

  6. #6
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Then I think you'e going to have issues with your approach. From my understanding of scanf (maybe flawed)

    scanf ("%c" &ch) will read the first character on the line after the user hits enter

    scanf ("%s" &ch) would read a string (assuming ch allowed for it), but only up to the next white space. (otherwise I'd suggest you read in a full string first, then have your function work with that string on a character level) Will your users be putting spaces in their input?
    Demonographic rhinology is not the only possible outcome, but why take the chance

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    space .....

    yes... if you use scanf(" %c", &name); instead of scanf("%c",&name); it will not accept white spaces as an input....

  8. #8
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    K. Well you need some sort of loop in order to use your scanf statement to grab the remaining characters from the buffer.

    Code:
    #include <stdio.h>
    #define LIMIT 20
    
    int main(void)  
    {
    
       int z=0, x=0;
    
       char ch;
       char A[21]={0};
    
       printf("Enter up to 20 unique characters\n");
    
       scanf("%c", &ch);
    
       while (z < LIMIT && ch != '\n')
       {
          A[z] = ch;
          for (x=0; x < z; x++)
          {
            if (ch == A[x])
                    z--; // Moves our pointer back one (we already had that char)
          }
          z++;
          scanf("%c", &ch); //Called inside a loop to get next char
       }
    
       for (z=0; z<LIMIT; z++)
         printf("%c",A[z]);
    
       printf("\n");
    
    
    return 0;
    }
    Last edited by Azuth; 10-27-2002 at 09:12 PM.
    Demonographic rhinology is not the only possible outcome, but why take the chance

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Help with returning a string of arrays
    By yuliang11 in forum C Programming
    Replies: 8
    Last Post: 12-10-2005, 03:05 PM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  3. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  4. arrays and functions (beginner q)
    By eazhar in forum C++ Programming
    Replies: 4
    Last Post: 07-13-2002, 05:39 AM
  5. Beginner needs help w/ran. nums. in arrays - URGENT!
    By madhouse199 in forum C++ Programming
    Replies: 3
    Last Post: 12-12-2001, 07:12 AM