loops and structures

This is a discussion on loops and structures within the C Programming forums, part of the General Programming Boards category; Hello, I made a small structure in order to see how loops work with them and I'm getting some weird ...

  1. #1
    Unregistered
    Guest

    loops and structures

    Hello,
    I made a small structure in order to see how loops work with them and I'm getting some weird results. Could someone look and see my mistakes.
    [code]/*
    #include<stdio.h>
    #include<stdlib.h>

    void main(void)
    {
    struct grade {
    char class_name[20];
    char name[20] ;
    };

    struct grade stu_record[2];
    int i;
    char repeat;
    for(repeat=0;repeat<2;repeat++)
    {

    printf("\nEnter class name: ");
    scanf("%s",stu_record[repeat].class_name);
    printf("\nEnter student name: ");
    scanf("%s", stu_record[repeat].name);
    }
    for(i=0; i<repeat; i++)
    {
    printf("\n%s, you are enrolled in %s.",stu_record[repeat].name,
    stu_record[repeat].class_name);
    }
    }
    */ [/code
    I believe it must be in the loops somewhere because it worked ok until I tried to add the loop. Any help would be awesome!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The problem is twofold. One potential problem is that scanf is leaving a newline character in the input stream and that is causing problems with further calls. The other problem is if you enter names with spaces in them. The %s flag for scanf is delimited by a space, so if you enter:

    Enter class name: foo 101

    Enter student name: J Walker

    Then the result will probably not be what you wanted. The general mindset is that scanf is bad and should be avoided, this is usually good advice. Here's the same program using fgets:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    struct grade {
      char cname[20];
      char name[20];
    };
    
    int main(void)
    {
      struct grade stu_record[2];
      int i;
      for(i=0;i<1;i++)
      {
        printf("\nEnter class name: ");
        fgets(stu_record[i].cname, 20, stdin);
        stu_record[i].cname[strcspn(stu_record[i].cname, "\n")] = '\0';
        printf("\nEnter student name: ");
        fgets(stu_record[i].name, 20, stdin);
        stu_record[i].name[strcspn(stu_record[i].name, "\n")] = '\0';
      }
      for(i=0; i<1; i++)
      {
        printf("\n%s, you are enrolled in %s.\n",
          stu_record[i].name,
          stu_record[i].cname);
      }
      return 0;
    }
    There are many ways you can improve upon user input and many pitfalls, do a few searches on this board for more information.

    Another problem you had was using the result variable in the second loop, you should have been using i as the index for the array. This was probably the exact problem you were having.

    >void main(void)
    No, main returns an int and nothing else. void main is undefined.

    -Prelude
    Last edited by Prelude; 08-04-2002 at 07:22 PM.
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest
    Thank you very much! I do have a few questions if I may?
    1) What is stdin?
    2)What is strcspn?
    3)Is '\0' what tells the system that there is a space between characters in a string?
    Like I said I'm new to programming and these questions may be elementary, but I'm wanting to learn as much about c as I can.
    Once again, thanks!

  4. #4
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Cool Hope this helps.

    1) What is stdin?
    This is an input/output macro. sort of like scanf for stdin and printf for stdout.

    2)What is strcspn?
    the c = complement and spn = span. Basically, it computes the length of the max substring say in s1 entirely of char not in s2.
    see eg. strcspn(const char *s1, const char *s2);

    3)Is '\0' what tells the system that there is a space between characters in a string?
    The means NULL or 0 in stdio.h.
    I know its very basic but at www.google.com there are lots more info about this.
    Also try http://www.howstuffworks.com because this is a good site for info.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: Hope this helps.

    Originally posted by correlcj
    1) What is stdin?
    This is an input/output macro. sort of like scanf for stdin and printf for stdout.
    No. Not really. 'stdin' stands for the 'standard input device'. This is commonly your keyboard. It has nothing to do with output. It is exclusivly input. (stdout is the standard output device, commonly the display adapter/monitor) It is not just a macro.

    stdin represents the file stream that is used to read input. It can be used any time you are using a file stream function.

    The file stream is opened for reading when your program starts automaticly, and will remain open unless you implicitly close it, until the program exits.

    It is a file stream. Just like:

    FILE *mystream;


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help needed for sorting nested loops...segmentation error
    By starvinmarvin in forum C Programming
    Replies: 17
    Last Post: 12-01-2008, 08:13 PM
  2. Please help... Global structures
    By msmith in forum C Programming
    Replies: 5
    Last Post: 09-16-2007, 07:18 PM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Cin in while loops
    By chrismax2 in forum C++ Programming
    Replies: 2
    Last Post: 03-09-2004, 06:57 PM
  5. combo for loops with if structures
    By Nova in forum C Programming
    Replies: 1
    Last Post: 10-15-2001, 12:49 PM

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