Advertisement
haithienht

C Struct Assignment

Jun 6th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. // Khoi tao data luu cac thuoc tinh no (thuc ra la student code), name va mark
  4. struct data{
  5.     int no;
  6.     char name [10];
  7.     float mark;
  8. };
  9.  
  10. int main(){
  11.     int stu_num, i;         //khai bao stu_num de nhap so luong hoc sinh, bien i de chay dong lap FOR
  12.     struct data stu[10];    //khai bao array stu luu duoc 10  du lieu va mang thuoc tinh cua data  (no, name, mark)
  13.    
  14.     printf("\nHow many student would you like to manage: ");
  15.     scanf("%d", &stu_num);
  16.    
  17.     //Neu nhap sai thi bat nhap lai trong khoang 1-10
  18.     while(stu_num<1||stu_num>10){
  19.         printf("\nThe number of Student is in range 1 - 10!\nPress any key to continue...");
  20.         getch();
  21.         fflush(stdin);
  22.         printf("\nHow many student would you like to manage: ");
  23.         scanf("%d", &stu_num);
  24.     }
  25.     //Dung dong lap for de nhap du lieu theo so hoc sinh minh da nhap
  26.     for (i=0;i<stu_num;i++){
  27.         printf("\t-Enter Student %d No: ",i+1);
  28.         scanf("%d",&stu[i].no);
  29.         fflush(stdin);
  30.         printf("\t-Enter Student %d Name: ",i+1);
  31.         gets(stu[i].name);
  32.         printf("\t-Enter Student %d Mark: ",i+1);
  33.         scanf("%f",&stu[i].mark);      
  34.     }
  35.    
  36.    
  37.     printf("\n\n--------- STUDENT INFORMATION ---------");
  38.     printf("\n\t (Note: P - Pass, R - Ref)");
  39.     printf("\n No     Code    Name    Mark    P/R");
  40.     printf("\n-------+-------+-------+-------+-------");
  41.    
  42.     for (i=0;i<stu_num;i++){
  43.         printf("\n%d\t%d\t%s\t%.2f\t",i+1,stu[i].no,stu[i].name,stu[i].mark);
  44.         if (stu[i].mark<5){
  45.             printf("R");
  46.         }
  47.         else{
  48.             printf("P");
  49.         }
  50.        
  51.     }
  52.  
  53.     getch();
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement