Thread: I'm new to arrays and getchar, help!

  1. #16
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    Quote Originally Posted by Dave_Sinkula View Post
    FWIW, I got my BSEE in 1994. I've been doing embedded systems which has largely consisted of C programming since 1995.
    BSEE is electrical engineering??

    and whats FWIW

  2. #17
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Google is an important tool, one that every good programmer should understand how to use.

    See?

  3. #18
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    Quote Originally Posted by MacGyver View Post
    Google is an important tool, one that every good programmer should understand how to use.

    See?
    [ha]

    SARCASM ALERT

    [\ha]

  4. #19
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    Does C not execute two scanf's in the main function? Because I had my first scanf detecting values entered into an array with a while loop and then did error checks for values entered using multiple if statements.

    Then after the error checking process was complete, I printed out several lines giving options to the user as to what he/she wants to do. So there were 3 options, after these lines were printed out I put a scanf after those lines, but when i execute the program, everything runs fine, except the program terminates after the last printf lines and does not execute the scanf, any ideas?

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    #define RETURN '\n'
    #define SPACE ' '
    
    
    int err (int);
    
    int main (int argc, char **argv) {
        
        int diceVal[5];
        int n;//index of array
        int next;//next value that is read
        //int c;
        int option;//option for what to do with dice
        
        n = 0;
        
        printf ("Please enter dice values: \n");
        
    
    
        while (scanf("&#37;d", &next)){
    
                diceVal[n] = next;
                n = n + 1;
            
          
    
              if ((next <  1) || (next > 6)) {
                       
                       printf ("Value Out of Range \n");
                       
                       exit (0);
                       
                       }
                       }
                       
                           if ((next > 6) && (n > 5)){
    
                                printf ("Value Out of Range \n");
    
                                exit (0);
    
                                }
    
                             
    
                              else if ((next > 6) && (n < 1)){
    
                                  printf ("Value Out of Range \n");
    
                                  exit (0);
    
                                   }
                                   
               
                             else if (n > 6) {
                            
                                printf ("Incorrect Number of Values \n");
                            
                                exit (0);
                            
                                  }
                                     
    
    
                                  printf ("%d values read into array \n", n);
              
              printf ("Please choose: \n");
              printf (" 1 - > Reroll some dice \n");
              printf (" 2 - > Reroll all dice \n");
              printf (" 3 - > Keep dice \n");
              
       
             scanf ("%d", &option);
    
            
                    if ((option < 1) && (option > 3)) {
                          
                          printf ("Invalid choice \n");
                          
                          exit (0);
                          
                          }
              
              
              
              
              return (0);
              
              }

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It's impossible to say what's going on simply from looking at your code because the indentation in a mess.

    Courtesy of indent -kr -nut -ts2 foo.c
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    #define RETURN '\n'
    #define SPACE ' '
    
    
    int err(int);
    
    int main(int argc, char **argv)
    {
        int diceVal[5];
        int n;                      //index of array
        int next;                   //next value that is read
        //int c;
        int option;                 //option for what to do with dice
    
        n = 0;
    
        printf("Please enter dice values: \n");
        while (scanf("&#37;d", &next)) {
            diceVal[n] = next;
            n = n + 1;
            if ((next < 1) || (next > 6)) {
                printf("Value Out of Range \n");
                exit(0);
            }
        }
    
        if ((next > 6) && (n > 5)) {
            printf("Value Out of Range \n");
            exit(0);
        }
        else if ((next > 6) && (n < 1)) {
            printf("Value Out of Range \n");
            exit(0);
        }
        else if (n > 6) {
            printf("Incorrect Number of Values \n");
            exit(0);
        }
    
        printf("%d values read into array \n", n);
        printf("Please choose: \n");
        printf(" 1 - > Reroll some dice \n");
        printf(" 2 - > Reroll all dice \n");
        printf(" 3 - > Keep dice \n");
    
        scanf("%d", &option);
    
        if ((option < 1) && (option > 3)) {
            printf("Invalid choice \n");
            exit(0);
        }
    
        return (0);
    }
    What do you type in to make the first scanf in the while loop exit?
    Because whatever you type in will make the 2nd one fail immediately as well.
    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.

  6. #21
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Your have some ridiculous conditions inside your code.

    Code:
    if ((next > 6) && (n > 5))
    Redundant. You only need one of these two conditions inside your if statement. Even then, you then check the lower and upper bounds after this. Just do them both once using one if statement. Furthermore:

    Code:
    if ((next > 6) && (n < 1))
    ...
    if ((option < 1) && (option > 3))
    These will never be true. I imagine you meant these respective conditions:

    Code:
    if ((next > 6) || (n < 1))
    ...
    if ((option < 1) || (option > 3))

  7. #22
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    Quote Originally Posted by MacGyver View Post
    Your have some ridiculous conditions inside your code.

    Code:
    if ((next > 6) && (n > 5))
    Redundant. You only need one of these two conditions inside your if statement. Even then, you then check the lower and upper bounds after this. Just do them both once using one if statement. Furthermore:

    Code:
    if ((next > 6) && (n < 1))
    ...
    if ((option < 1) && (option > 3))
    These will never be true. I imagine you meant these respective conditions:

    Code:
    if ((next > 6) || (n < 1))
    ...
    if ((option < 1) || (option > 3))

    I agree with the last one, as || and not && I realized that when recompiling. But I do not understand what you mean with the previous ones. You do realize next and n are two different variables right?

    Next is the value(s) entered into the array, n is the index of the array. But I will go through that part and try to make sense of it. THanks!

  8. #23
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    if ((next > 6) && (n > 5)) {
            printf("Value Out of Range \n");
            exit(0);
        }
        else if ((next > 6) || (n < 1)) {
            printf("Value Out of Range \n");
            exit(0);
        }
    The bold black text can be deleted. The bold blue text indicates a change.

    That was my first point.

  9. #24
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    Quote Originally Posted by MacGyver View Post
    Code:
    if ((next > 6) && (n > 5)) {
            printf("Value Out of Range \n");
            exit(0);
        }
        else if ((next > 6) || (n < 1)) {
            printf("Value Out of Range \n");
            exit(0);
        }
    The bold black text can be deleted. The bold blue text indicates a change.

    That was my first point.

    You are right, removing it did not affect the program, I don't know why I put that in the first place.

    Thanks!

  10. #25
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    I had a scanf in a while loop in the program, and everytihng was going fine until I had to use a second scanf in the main function. It just wouldn''t execute the scanf... or get char..

    I highlighted the culprit in red, I think it's to do with the while loop in the first scanf. No joke, I've been trying to figure out what the hell to do for 1.5 hours and I tried so many combinations like puttting that scanf into a second function, using pointers, using get char, and it just doesn't work!!! Any ideas?

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    #define RETURN '\n'
    #define SPACE ' '
    
    
    int keep (int option);
    
    int main (int argc, char **argv) {
        
        int diceVal[5];
        int n;//index of array
        int next;//next value that is read
        int c;
        int option;//option for what to do with dice
        
        int THK;//Three of a kind
        int FOK;//Four of a kind
        int FH;//Full House
        int SS;//Small straight
        int Straight; //Straight score
        int Yahtzee;
        int Chance;
        int sum; //sum of dice
        
        n = 0;
        
        printf ("Please enter dice values: \n");
        
    
    
        while (scanf("%d", &next)){
    
                diceVal[n] = next;
                n = n + 1;
            
          
    
              if ((next <  1) || (next > 6)) {
                       
                       printf ("Value Out of Range \n");
                       
                       exit (0);
                       
                       }
                       }
                       
                             
    
                               if ((next > 6) || (n < 1)){
    
                                  printf ("Value Out of Range \n");
    
                                  exit (0);
    
                                   }
                                   
               
                             else if (n > 6) {
                            
                                printf ("Incorrect Number of Values \n");
                            
                                exit (0);
                            
                                  }
                                     
    
    
                                  printf ("%d values read into array \n", n);
                                  
                                
              printf ("Please choose: \n");
              printf (" 1 - > Reroll some dice \n");
              printf (" 2 - > Reroll all dice \n");
              printf (" 3 - > Keep dice \n");            
              scanf ("%d", &option);
              
              if (option == 3) {
                         
                         keep (option);
                         
                         }
                                                    
                                                   
                         return (0);
                                        
                                        }
     int keep (int option) {                            
              
              printf ("Your score options are: \n");
              printf ("1 - > Three of a Kind \n");
              printf ("2 - > Chance \n");         
             
             return (0);
             }

  11. #26
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well, your indentation is still a mess, you still don't say what you type in to get out of the first while loop.

    > while (scanf("&#37;d", &next))
    The only way to make this exit the loop is to type in something %d doesn't match.
    But as soon as you do that, it won't match scanf ("%d", &option); either.
    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.

  12. #27
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    So your telling me it's because of my first scanf-while loop not exiting that's causing the problem? Now that you've mentioned it, that certainly seems to be true, .........

    Thanks for pointing that out, I hope I don't have to change the whole code again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 08:35 PM
  2. Trying to understand Arrays
    By LaGood in forum C Programming
    Replies: 5
    Last Post: 08-16-2007, 10:07 PM
  3. help with getchar() and arrays
    By theorbb in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 02:53 PM
  4. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  5. Help with arrays again
    By viclaster in forum C Programming
    Replies: 7
    Last Post: 10-08-2003, 09:44 AM