Home > other >  a C program that allows the user to enter names and ages of student then displays The names of stude
a C program that allows the user to enter names and ages of student then displays The names of stude

Time:11-03

//the program failed to print the names and age of students bellow the age of 18

#include <stdio.h>

int main()
{
    char cName[5][21]={0};
    int x=0;
    int age;

    printf("Enter name of 5 people:\n");

    for (x=0;x<5;x  )
    {
        printf("Enter name %d: ",x 1);
        scanf("%s",cName[x]);
    }

    for (x=0;x<5;x  )
    {
        printf("Enter age %d: ",x 1);
        scanf("%d",&age);
    }

    if (age>=0 && age<=17)
    {
        printf("Students bellow 18:%s",cName);
    }

    return 0;

}

CodePudding user response:

#include <stdio.h>

int main()
{
    char cName[5][21]={0};
    int x=0;
    int age[5]={0};//age needs to defined as an array too

    printf("Enter name of 5 people:\n");

    for (x=0;x<5;x  )
    {
        printf("Enter name %d: ",x 1);
        scanf("%s",cName[x]);
    }

    for (x=0;x<5;x  )
    {
        printf("Enter age %d: ",x 1);
        scanf("%d",age x);
    }

    for (x=0;x<5;x  )//use loop to output
    {
        if (age[x]>=0 && age[x]<=17)
        {
            printf("Students bellow 18:%s",cName[x]);
        }
    }

    return 0;

}

CodePudding user response:

#include <stdio.h>

int main()
{
    char cName[5][21]={0};
    int x=0;
    int cnt = 0;
    unsigned int age[5]={0};//age needs to defined as an array too

    printf("Enter name of 5 people:\n");

    for (x=0;x<5;x  )
    {
        printf("Enter name %d: ",x 1);
        scanf("%s",cName[x]);
        
        printf("Enter age %d: ",x 1);
        scanf("%d",age x);
    }

    printf("Students bellow 18:");
    for (x=0;x<5;x  )//use loop to output
    {

        if (age[x]<18)
        {
            printf("%d. %s \n",  cnt,cName[x]);
        }
    }
    if(!cnt){
        printf("No students in the age group");
    }

    return 0;

}

    
 
  •  Tags:  
  • c
  • Related