#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Question How to loop/store/print for multiple input?


    Hello. =)

    I'm a newbie to C, and I'm currently taking a class in this language. I was given an assignment where I have to gather multiple lines of data (name, SSN, tuition, etc.), for multiple people.

    The end result would look like this:

    Name: John Doe
    SSN: 111-22-3344
    Tuition: 2400

    Name: Jane Doe
    SSN: 222-33-4455
    Tuition: 600

    Name: Glen Doe
    SSN: 333-44-5566
    Tuition: 3000
    ....


    That said, I know I'm going to need a loop. And I have to use a sentinel value because my teacher never said how long it should loop.

    However, my textbook only shows loops for averaging out grades. It doesn't show ANYTHING about multiple types (and multiple lines) of input. So now I have two major problems:

    1. How do I store and print data for multiple input? I don't want to create 20+ values called "name" (name1, name2, etc), "SSN", and "tuition".

    2. Is there a way to enter the sentinel value for any piece of data? I was going to have people enter "-1", on the "Name" line (which is the first prompt they'll see), if they want to quit. But what if they enter a name anyway and then decide to quit on the "SSN" line? Can I use a sentinel value of "-1" for the "SSN" line too?

    Please help if you can. Thanks in advance!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    I can't think of a reason to use a data type other than characters. Hence, a hyphen first character is a sufficient indicator.
    Code:
    /* char name[256];  I am an array.  You already know me! */
    
    #define GROUP struct conglomeration
    
    #define RECORDS 131072
    #define DIM(A) (sizeof(A)/(sizeof(*A)))
    
    GROUP {
      char
        name[256],
        ssn[16],
        tuition[65536];		/* enough digits to store tuition */
    } home_work[RECORDS];		/* array of grouped information */
    
    for (i = 0; i < RECORDS; ++i) {
      GROUP*student = home_work+i;
      fgets(student->name,DIM(student->name),stdin);
      if ('-' == *(student->name)) break;
      fgets(student->ssn,DIM(student->ssn),stdin);
      if ('-' == student->ssn[0]) break;
      fgets(student->tuition,DIM(student->tuition),stdin);
      if ('-' == *(student->tuition)) break;
      /* verify the ssn is valid */
      /* verify the tuition is valid */
    }
    printf("You just registered %d students\n",i);
    
    /* write a pretty display loop for pretty data here */
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Originally Posted by b49P23TIvg
    Code:
    #define GROUP struct conglomeration
    WTH?! ?!?!?

    There's a first time for anything!
    I'm not against innovation or imagination, but what is wrong with using "struct conglomeration" where that type is required? What do you gain (not counting a few keystrokes) by using a macro instead?

    Also note that the usual way to gain keystrokes is with a typedef (I still prefer the literal "struct conglomeration")

    Code:
    typedef struct conglomeration group;
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    Thank you both for the replies, but this code is a bit...complex. We've never gone over #define or GROUP...and we barely covered fgets.

    We're pretty much just using these:
    printf
    scanf
    for
    while
    do
    ++a
    switch
    case
    counter = 0

    ...and other basic commands like that.

    Is there a way to store data using these basic commands? Or do I have to use advanced functions like the one you posted? I can't seem to find anything on Google, and I don't think any of my ideas will work.

    EDIT: I'm seeing some posts (on Google) that talk about arrays. Could I use one of those to store multiple input, like 10 or more names?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Yes, you need arrays (possibly of some structure).
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,855
    Rep Power
    481
    Do you actually lose credit for reading ahead in the text book?
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0
    @b49P23TIvg: No, we don't lose credit if we jump a little bit ahead. Arrays are in Chapter 6 (which we aren't on yet)...but the really complex stuff is probably covered in Chapter 18 or something. He'd definitely mark me down because then it's obvious I got a ton of help.

    Here's what I came up with for my array. For now, I'm only working with one piece of input at a time...so I'm just trying to figure out the "multiple names" part.

    I'm pretty sure this is wrong -- I even got 2 error messages -- but at least it shows I'm trying:

    Error Codes Generated
    [Error]Line 19 (the "PrintArray" line): undefined reference to `printArray(char const (*) [31])'
    [Error] collect2: ld returned 1 exit status


    Code:
    #include <stdio.h>
    void printArray( char const a[][ 31 ] ); /* function prototype */
    //function main begins program execution
    
    int main( void )
    {
    int i = 0;
    char name[i][31];
    printf("Please enter a name, or type -1 to end: \n");
    scanf("%c", name);
    
    for ( i = 0; name[i][31] != -1; i++) {
    printf("Please enter a name, or type -1 to end: \n", name[1]);
    }
    printArray ( name );
    return 0; //indicate that the program ended successfully
    } //end function main
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    20
    Rep Power
    0
    My question is more basic information needed from the first post.

    What is the purpose of entering these multiple people?
    1) It it to collect data for use later in the program?
    2) Is it to create a file of students so the next program can read a file?
    3) Is it simply to input multiple people just to learn how to use a loop?

    #1 requires arrays.
    #2 and 3 do not.

    So it really depends on exactly what the assignment is meant to teach you. You need to provide more information.

IMN logo majestic logo threadwatch logo seochat tools logo