Ad

For Or While Loop For Char And More In C

- 1 answer

Apologise for some Korean alphabet if you don't understand.

I would like to print out this whole for 3 more times using loop and I couldn't figure out since yesterday. this program prints 1 person's information now but I need to take 2 more people's information and then print total 3 people's information using loop and then sort with ascending order from the birth year but I think I can figure that out if I can make this runs with loop first...

I tried for loop but it didn't work and can't think of any solution with my current level. Please let me know if you need more explanation. It is my first time using stackoverflow clearly, I have no idea how to use it well... I would be appreicated for any advices.

Thanks in advance for your help

Here is my code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
    int i;
    for (i = 0; i < 3; i++)
    {
        char name[20];
        char major[50];
        char ssn[20];

        // gets name, major, ssn
        printf("%s", "이름 : ");
        scanf("%[^\n]%*c", name);
        printf("%s", "학과 : ");
        scanf("%[^\n]%*c", major);
        printf("%s", "주민등록번호 : ");
        scanf("%[^\n]%*c", ssn);

        // last 2 digits of birth year
        char year[3];
        memcpy(year, &ssn[0], 2);
        year[2] = '\0';

        // 2 digits of birth month
        char month[3];
        memcpy(month, &ssn[2], 2);
        month[2] = '\0';

        // 2 digits of birth day
        char day[3];
        memcpy(day, &ssn[4], 2);
        day[2] = '\0';

        // distinguising birth years of 1900
        char fullyear[5];
        if ((ssn[6] == '1') || (ssn[6] == '2') || (ssn[6] == '5') || (ssn[6] == '6'))
        {
            fullyear[0] = '1';
            fullyear[1] = '9';
            fullyear[2] = year[0];
            fullyear[3] = year[1];
            fullyear[4] = '\0';
        }

        // distinguishing birth years of 2000
        if ((ssn[6] == '3') || (ssn[6] == '4') || (ssn[6] == '7') || (ssn[6] == '8'))
        {
            fullyear[0] = '2';
            fullyear[1] = '0';
            fullyear[2] = year[0];
            fullyear[3] = year[1];
            fullyear[4] = '\0';
        }

        // distinguishing birth years of 1800
        if ((ssn[6] == '9') || (ssn[6] == '0'))
        {
            fullyear[0] = '1';
            fullyear[1] = '8';
            fullyear[2] = year[0];
            fullyear[3] = year[1];
            fullyear[4] = '\0';
        }

        // print name
        printf("%s, ", name);
        // print birth year
        printf("%s년 ", fullyear);
        // print month
        printf("%s월 ", month);
        // print day
        printf("%s일, ", day);

        // leap year, converting from full year string --> int
        int leapyear;
        leapyear = atoi(fullyear);
        if (((leapyear % 4 == 0) && (leapyear % 100 != 0)) || (leapyear % 400 == 0))
        {
            printf("윤년, ");
        }
        else
        {
            printf("윤년아님, ");
        }

        // foreigner or Korean
        if ((ssn[6] == '5') || (ssn[6] == '6') || (ssn[6] == '7') || (ssn[6] == '8'))
        printf("외국, ");
        else printf("대한민국, ");

        // gender
        if ((ssn[6] == '1') || (ssn[6] == '3') || (ssn[6] == '5') || (ssn[6] == '7') || (ssn[6] == '9'))
        printf("남자, ");
        else printf("여자, ");

        // print major
        printf("%s\n\n", major);
    }
    return 0;
}
Ad

Answer

If you need to store 3 names, 3 majors, and 3 SSNs, then you need to declare those arrays as

char name[3][20];   // 3 strings of 20 characters each
char major[3][20];
char ssn[3][20];

and they need to be declared outside of the body of the loop.

Then in your loop, you'd read each element as

scanf( "%19[^\n]%*c", name[i] ); // Read no more than 19 characters into each
...                              // object - this means you may need to do
scanf( "%19[^\n]%*c", major[i] );// some cleanup between each read if the user
...                              // entered more than 20 characters for a 
scanf( "%19[^\n]%*c", ssn[i] );  // given entry

When you compute the birth date, just replace each instance of ssn with ssn[i], such as

memcpy(year, &ssn[i][0], 2);
...
memcpy(month, &ssn[i][2], 2);

etc.

You'll want at least two loops - one for all the input, and one for displaying the sorted output. With only three elements, you shouldn't need to do an actual sort, just keep track of which index needs to come first and second.

You do have a severe structural problem in your code - the lines

for (i = 0; i < 3; i++)

char name[20];
char major[50];
char ssn[20];
{

need to be changed to

char name[3][20];
char major[3][50];
char ssn[3][20];

for (i = 0; i < 3; i++)
{

What happened is you defined a loop with a single statement - the declaration of name. Everything after that is outside of the loop.

Start with those changes, and hopefully that will help you move forward.

EDIT

An alternate approach to using 3 separate arrays would be to use a single array of a struct type:

struct student
{
  char name[20];
  char major[20];
  char ssn[20];
} students[3];

for ( int i = 0; i < 3; i++ )
{
  // read input
  ...
  scanf( "%19[^\n]%*c", students[i].name );
  ...
  scanf( "%19[^\n]%*c", students[i].major );
  ...
  scanf( "%19[^\n]%*c", students[i].ssn );
  ...
}

// "sort" entries

for ( int i = 0; i < 3; i++ )
{
  // print output
}

You get the idea - instead of expressions like name[i] or major[i], you'd use students[i].name, student[i].major, etc.

Ad
source: stackoverflow.com
Ad