Thread: Quick help needed on simple example

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    16

    Quick help needed on simple example

    Code:
    char c=0;
    int letter [3];
    
    letter[0]=0; letter[1]=0; letter[2]=0;
    
    printf("please enter a letter");
    scanf("%c", &c);
    
    switch (c) {
    case 'a': letter[0]++;
    case 'b': letter[1]++;
    default: letter[2]++;
    }
    printf("%d %d %d", letter[0], letter[1], letter[2]);
    Can someone please explain what line2 is actually doing. Is it there to define how many "letter" variations there are (i.e letter[0]...), or does it do something else?

    Also because letter[0]=0 for example isnt given a value type, but it follows on from an int being defined, is it reasonable to assume letter[0] is being defined as an int as well?

  2. #2
    Registered User
    Join Date
    May 2009
    Location
    Slovenia
    Posts
    5
    Hello.

    Line 2 says: "letter is an array of 3 integers.". There is nothing more to it.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    16
    Is this even being used in the code though. I can see letter[0], letter[1] and letter[2], but no mention of just letter.

    Sorry if im being dumb.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    letter[0] is the first element of the array declared on line 2. letter[1] is the second element of the array, etc.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    16
    Ah i understand now, thank you.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    41
    Quote Originally Posted by hiven View Post
    Code:
    char c=0;
    int letter [3];
    
    letter[0]=0; letter[1]=0; letter[2]=0;
    
    printf("please enter a letter");
    scanf("%c", &c);
    
    switch (c) {
    case 'a': letter[0]++;
    case 'b': letter[1]++;
    default: letter[2]++;
    }
    printf("%d %d %d", letter[0], letter[1], letter[2]);
    Can someone please explain what line2 is actually doing. Is it there to define how many "letter" variations there are (i.e letter[0]...), or does it do something else?

    Also because letter[0]=0 for example isnt given a value type, but it follows on from an int being defined, is it reasonable to assume letter[0] is being defined as an int as well?
    Your code is really ate up. I'm not really sure what your trying to acheive.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       char c;
       int letter [3] = {0};
    
    
       printf("please enter \"a\" or \"b\": ");
       scanf("%c", &c);
    
       switch (c) 
       {
          case 'a': 
             ++letter[0];
             break;
          case 'b': 
             ++letter[1];
             break;
          default: 
             ++letter[2];
       }
       printf("\nletter[0] contains: %d\nletter[1] contains:"
                " %d\nletter[2] contains: %d", letter[0], letter[1], letter[2]);
       
       fflush(stdin);
       getchar();
       return 0;
    }
    You really need a break in those cases or else all the cases will be performed.
    edit: unless its your intention to add one to letter[1] and letter[2] when you enter a to add
    one to letter[0]
    Last edited by strickyc; 05-12-2009 at 03:58 PM.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    do not fflush stdin - read FAQ
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 12-31-2007, 06:26 AM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. help needed in a simple prob!
    By o0o in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 07:55 AM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. "break" - quick response needed
    By luke in forum C Programming
    Replies: 4
    Last Post: 04-23-2002, 08:57 AM