I'm new to arrays and getchar, help!

This is a discussion on I'm new to arrays and getchar, help! within the C Programming forums, part of the General Programming Boards category; While making a simple code for the popular Yahtzee game, while I don't know how to assign random values for ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    15

    I'm new to arrays and getchar, help!

    While making a simple code for the popular Yahtzee game, while I don't know how to assign random values for dice, i guess user input is fine enough. lol.

    I thought of reading user input like this:


    Code:
    int main (int argc, char **argv) {
        
        int diceVal[5];
        int n;
        int next;
        
        
    
        
        printf ("Please enter dice values: \n");
        scanf ("%d %d %d %d %d", &diceVal[0], &diceVal[1], &diceVal[2], &diceVal[3]...etc.
        
              
        
    
    
              
              
              return (0);
              
              }
    I just thought if there were a 1000 values, no one is going to sit there assigning values a 1000 times, also is it better to use getchar instead of scanf to recognize the values? And instead of typing a million times over the diceVal, do I put it in some kind of loop? I'm not sure how to put arrays into loops, when I tried, it always gave some bizarre answer or it failed to compile

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    the loop is an obvious solution for such kind of problems
    something like
    Code:
    for(i = 0;i< 1000;i++)
       if(scanf("&#37;d",&diceVal[i]) !=  1)
          break;
    printf("Read %d values", i);
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read the FAQ. There are many topics that should be of interest, such as random numbers, and how to get a number from the user.


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

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    Oh sorry, I'm doing an assignment you see, and I'm just finding out what is the best way to go about it, and we can't use for loops or break, and plus I don't know how to implement break.

    Thanks

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    Oh i figured out a loop!!!

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    #define RETURN '\n'
    
    int main (int argc, char **argv) {
        
        int diceVal[5];
        int n;
        int next;
        int c= getchar();
        
        printf ("Please enter dice values: \n");
        
     n = 0;
    
       while (getchar () !=RETURN) {
    
    
             if (n < 10) {
    
             diceVal[n] = next;
    
             n = n + 1;
    
                 }
    
               }
    printf ("&#37;d values read into array \n", n);
              
              return (0);
              
              }
    But the thing is, the first printf statement doesn't come until i enter values!!! Which is redundant! And also it's scanning the spaces as characters, and i don't want it to do that, how do i fix that?
    Last edited by whowho; 04-03-2007 at 06:54 AM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I take it you didn't bother reading the FAQ then as I suggested.


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

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    Quote Originally Posted by quzah View Post
    I take it you didn't bother reading the FAQ then as I suggested.


    Quzah.


    Yes i did, except i don't understand what fgets is and they don't elaborate on get char...

    the example was like getchar (const * bla bla bla


    I had no clue what it mean, and there were asterisks everywhere

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    Argh, wtf, screw programming, nothing ........ing ever works.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well at least you had sense enough to quit now instead of wasting everyone's time trying to help you with something you don't really want to do.


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

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    Quote Originally Posted by quzah View Post
    Well at least you had sense enough to quit now instead of wasting everyone's time trying to help you with something you don't really want to do.


    Quzah.


    Quit? Rofl, major assignment, im just venting my frustration thanks. And that's right I don't want to do it, but I have to, there are many things we have to do, but we don't want to do, this happens to be one the things I don't want to. Sif i like to sit down and program a Yahtzee game ffs.

    But I will do it, or try to do whatever I can so I don't fail as badly or maybe just pass so I don't have to redo semester 1 of computing again.

    But whatever!

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
     int c= getchar();
        
        printf ("Please enter dice values: \n");
        
     n = 0;
    
       while (getchar () !=RETURN) {
    
    
             if (n < 10) {
    
             diceVal[n] = next;
    
             n = n + 1;
    
                 }
    
               }
    printf ("&#37;d values read into array \n", n);
    getchar() wait for a key to be pressed. You read a character before you print anything. Therefore, your program will wait for characters to be entered before printing anything. I suggest you get rid of the first getchar().

    And also it's scanning the spaces as characters, and i don't want it to do that, how do i fix that?
    Code:
    if(c != ' ') { /* ... */ }
    I suggest you look at vart's code again, it could be useful.
    Code:
    for(i = 0;i< 1000;i++)
       if(scanf("%d",&diceVal[i]) !=  1)
          break;
    printf("Read %d values", i);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Mar 2007
    Posts
    3
    Quote Originally Posted by whowho View Post
    major assignment, im just venting my frustration thanks. And that's right I don't want to do it, but I have to, there are many things we have to do, but we don't want to do, this happens to be one the things I don't want to. Sif i like to sit down and program a Yahtzee game ffs.

    But I will do it, or try to do whatever I can so I don't fail as badly or maybe just pass so I don't have to redo semester 1 of computing again.

    But whatever!
    Doesn't sound like you have any interest in learning C. I tell you, reevaluate what you want to do with your life before it's too late and you waste money on a degree you have no plans on using.

    You can breeze through college without really learning anything by having others do your work for you, In the end, You only rob yourself the education you paid for.

    Doing those silly little assessments actually help you understand the material. Itís learned by trial & error. Ití sounds like you need to take semester 1 over again if you have any interest in learning C

  13. #13
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    Quote Originally Posted by Floyd View Post
    Doesn't sound like you have any interest in learning C. I tell you, reevaluate what you want to do with your life before it's too late and you waste money on a degree you have no plans on using.

    You can breeze through college without really learning anything by having others do your work for you, In the end, You only rob yourself the education you paid for.

    Doing those silly little assessments actually help you understand the material. Itís learned by trial & error. Ití sounds like you need to take semester 1 over again if you have any interest in learning C


    I won't ever be programming in C or not much. I'm doing electrical engineerings and programming is a component of the first year.

    Thanks to all who helped , I will try to implement it properly!

  14. #14
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    Anyways, I kind of started liking programming in C :P

    ...Not too much though!!

    Meh, ill keep trying

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by whowho View Post
    I won't ever be programming in C or not much. I'm doing electrical engineerings and programming is a component of the first year.
    FWIW, I got my BSEE in 1994. I've been doing embedded systems which has largely consisted of C programming since 1995.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Page 1 of 2 12 LastLast
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, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21