0

Sorry for the language difference, you don't have to pay attention to other things, just focus on what I describe. This is a simple student management program. .

I have used do while to keep the program from exiting, but I don't understand why it still stops automatically. If you choose case 9 and insert any number ( you should enter a number ) you will see the program automatically stop . Apparently there's no exit command there

.I just want to ask if it is due to data conflicts I would really appreciate it if I could get some help

#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"math.h"
struct ct {
    int id;
    char name[100]="";
    int people;
    int v1;
    int v2;
};
void sys(){
    char g[50];
    fflush(stdin);
    printf("\n\nBan co muon ket thuc ngay lap tuc : ");
    fgets(g,sizeof(g),stdin);
    if(g[0]=='Y'&&g[1]=='E'&&g[2]=='S'){
        exit(0);
    }
}
void cntt(){
    printf("     _._     _,-'""`-._ \n");
    printf("    (,-.`._,'(       |\`-/| \n");
    printf("        `-.-' \ )-`( , o o) \n");
    printf("              `-    \`_`""_  \n");
}
int rate1(ct city[],int i){                 // tinh ty le mui 1 
    float t;
    float y=float(city[i].people)/100;
    t=float(city[i].v1)/y;
    return t;
}
int rate12(ct city[],int i){
    float rate ;
    float add12 = float(city[i].v1)+city[i].v2;
    float one = float( city[i].people)/100;
    rate = add12/one;
    return rate;
}
void print(){
    printf("%5s %10s    %10s    %10s    %10s    %10s    %10s","STT","Ma","Ten","So dan","Mui 1","Mui 2","Ti Le(%)");
    printf("\n");
}
void out(ct city[],int i,int h){                        //xuat : chuc nang chinh 
    float g;
    g=rate12(city,i);
    printf("%5d %10d    %10s    %10d    %10d    %10d    %10.2f",h,city[i].id,city[i].name,city[i].people,city[i].v1,city[i].v2,g);
    printf("\n");
}
void out2(ct city[],int i,int h){                       //xuat : chuc nang v1 
    float g;
    g=rate1(city,i);
    printf("%5d %10d    %10s    %10d    %10d    %10d    %10.2f",h,city[i].id,city[i].name,city[i].people,city[i].v1,city[i].v2,g);
    printf("\n");
}
void del_n(char x[]){
    size_t g=strlen(x);
    if(x[g-1]=='\n'){
        x[g-1]='\0';
    }
}
void input (ct city[],int n){
    for(int i=0;i<n;i++){
        printf("    Nhap ma thanh pho : ");
        scanf("%d",&city[i].id);
        printf("    Nhap ten thanh pho : ");
        fflush(stdin);
        fgets(city[i].name,sizeof(city[i].name),stdin);
        del_n(city[i].name);
        do{
        printf("    Nhap so dan cu : ");
        scanf("%d",&city[i].people);
        if(city[i].people<0){
            printf("    So dan cu phai lon hon 0");
        }
        }while(city[i].people<0);
        do{
            printf("    Nhap so luong mui tiem 1 : ");
            scanf("%d",&city[i].v1);
            if(city[i].v1>city[i].people){
                printf("    So mui tiem khong duoc vuot qua so dan cu");
            }
        }while(city[i].v1>city[i].people);
        do{
            printf("    Nhap so luong mui tiem 2 : ");
            scanf("%d",&city[i].v2);
            if(city[i].v2>city[i].v1){
                printf("    So luong mui tiem 2 phai nho hon mui 1 ");
            }           
        }while(city[i].v2>city[i].v1);  
    }
}
void print1(){
    printf("%5s %10s    %10s    %10s    %10s    %10s    ","STT","Ma","Ten","So dan","Mui 1","Mui 2");
    printf("\n");
}
void out1(ct city[],int i){
    float g;
    g=rate12(city,i);
    printf("%5d %10d    %10s    %10d    %10d    %10d",i+1,city[i].id,city[i].name,city[i].people,city[i].v1,city[i].v2);
    printf("\n");
}
void output (ct city[],int n){
    printf("\n");
    print1();
    for(int i=0;i<n;i++){
        out1(city,i);
    }
}
void swap(ct &city,ct &city2){
    ct temp;
    temp=city;
    city=city2;
    city2=temp;
}
void maxToMin12(ct city[],int n){
    for(int i=0;i<n-1;i++){
        for(int j=i+1;j<n;j++){
            if(rate12(city,i)<rate12(city,j)){
                swap(city[i],city[j]);
            }
        }
    }
}
void topN12(ct city[],int n){
    int t;
    do{
        printf("\nNhap top can kiem tra so luong tiem ca 2 mui : ");
        scanf("%d",&t);
        if(t>n){
            printf("So top khong duoc lon hon so thanh pho kiem tra ");
        }
    }while(t>n);
    maxToMin12(city,n);
    int h=1;
    printf("\n");
    print();
    for(int i=0;i<t;i++){
        out(city,i,h);
        if(rate12(city,i)!=rate12(city,i+1)){
            h++;
        }
    }
}
void maxToMin12up(ct city[],int n){             //top 12 tang dan 
    for(int i=0;i<n-1;i++){
        for(int j=i+1;j<n;j++){
            if(rate12(city,i)>rate12(city,j)){
                swap(city[i],city[j]);
            }
        }
    }
}
void topN12up(ct city[],int n){             //top 12 tang dan 
    int t;
    do{
        printf("\nNhap top can kiem tra so luong tiem ca 2 mui : ");
        scanf("%d",&t);
        if(t>n){
            printf("So top khong duoc lon hon so thanh pho kiem tra ");
        }
    }while(t>n);
    maxToMin12up(city,n);
    int h=1;
    printf("\n");
    print();
    for(int i=0;i<t;i++){
        out(city,i,h);
        if(rate12(city,i)!=rate12(city,i+1)){
            h++;
        }
    }
}
void v1(ct city[],int n){                       // xep theo ty le mui 1 giam dan 
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(rate1(city,i)<rate1(city,j)){
                swap(city[i],city[j]);
            }
        }
    }
    int h=1;
    print();
    for(int i=0;i<n;i++){
        out2(city,i,h);
        if(rate1(city,i)!=rate1(city,i+1)){
            h++;
        }
    }
}
void v1up(ct city[],int n){                     // xep theo ty le mui 1 tang dan 
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(rate1(city,i)>rate1(city,j)){
                swap(city[i],city[j]);
            }
        }
    }
    int h=1;
    print();
    for(int i=0;i<n;i++){
        out2(city,i,h);
        if(rate1(city,i)!=rate1(city,i+1)){
            h++;
        }
    }
}
void changeV1(ct city[],int n){
    int x;
    int dem=0;
    int z;
    do{
    printf("Nhap ma thanh pho can thay doi : ");
    scanf("%d",&x);
    for(int i=0;i<n;i++){
        if(x==city[i].id){
            dem++;
            z=i;
        }
    }
    if(dem==0){
        printf("Khong co ma thanh pho nay \n");
    }
    }while(dem==0);
    printf("Nhap so mui tiem 1 can thay doi cua thanh pho  %s : ",city[z].name);
    scanf("%d",&city[z].v1);

    
}
void changeV2(ct city[],int n){
    int x;
    int dem=0;
    int z;
    do{
    printf("Nhap ma thanh pho can thay doi : ");
    scanf("%d",&x);
    for(int i=0;i<n;i++){
        if(x==city[i].id){
            dem++;
            z=i;
        }
    }
    if(dem==0){
        printf("Khong co ma thanh pho nay \n");
    }
    }while(dem==0);
    printf("Nhap so mui tiem 2 can thay doi cua thanh pho  %s : ",city[z].name);
    scanf("%d",&city[z].v2);

    
}
void addCity (ct city[],int &n){
    int i=n;
    n=n+1;
    printf("    Nhap ma thanh pho : ");
        scanf("%d",&city[i].id);
        printf("    Nhap ten thanh pho : ");
        fflush(stdin);
        
        fgets(city[i].name,sizeof(city[i].name),stdin);
        del_n(city[i].name);
        /*
        do{
        printf("    Nhap so dan cu : ");
        scanf("%d",&city[i].people);
        if(city[i].people<0){
            printf("    So dan cu phai lon hon 0");
        }
        }while(city[i].people<0);
        do{
            printf("    Nhap so luong mui tiem 1 : ");
            scanf("%d",&city[i].v1);
            if(city[i].v1>city[i].people){
                printf("    So mui tiem khong duoc vuot qua so dan cu");
            }
        }while(city[i].v1>city[i].people);
        do{
            printf("    Nhap so luong mui tiem 2 : ");
            scanf("%d",&city[i].v2);
            if(city[i].v2>city[i].v1){
                printf("    So luong mui tiem 2 phai nho hon mui 1 ");
            }           
        }while(city[i].v2>city[i].v1);  
*/
}
void deleteCity (ct city[],int &n){
    int x;
    int dem=0;
    int z;
    do{
    printf("Nhap ma thanh pho can xoa : ");
    scanf("%d",&x);
    for(int i=0;i<n;i++){
        if(x==city[i].id){
            dem++;
            z=i;
        }
    }
    if(dem==0){
        printf("Khong co ma thanh pho nay \n");
    }
    }while(dem==0);
    for(int i=z;i<n;i++){
        city[i]=city[i+1];
    }
    n--;
}
int main(){
    ct *city;
    int n;
    char y=3;
    printf("Input number : ");
    scanf("%d",&n);
    
    city=(ct*)malloc(sizeof(ct)*n);
    int v;
    
    do{
        system("cls");
        cntt();
        printf("                    * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
        printf("                    *                      MENU %c%c%c                   *\n",y,y,y);
        printf("                    *          1.Nhap thong tin tinh thanh pho        *\n");
        printf("                    *          2.Xuat thong tin tinh thanh pho        *\n");
        printf("                    *          3.Xac dinh top N thanh pho co ty       *\n");
        printf("                    *            le tiem 2 mui cao nhat               *\n");
        printf("                    *          4.Xac dinh top N thanh pho co ty       *\n");
        printf("                    *            le tiem 2 mui  thap nhat             *\n");
        printf("                    *          5.Sap xep cac thanh pho theo ty        *\n");
        printf("                    *            le tiem mui 1 giam dan               *\n");
        printf("                    *          6.Sap xep cac thanh pho theo ty        *\n");
        printf("                    *            le tiem mui 1 tang dan               *\n");
        printf("                    *          7.Thay doi so mui 1 vacxin             *\n");
        printf("                    *          8.Thay doi so mui 2 vacxin             *\n");
        printf("                    *          9.Them thanh pho                       *\n");
        printf("                    *          10.Xoa thanh pho                       *\n");
        printf("                    *          11.Ket thuc chuong trinh               *\n");
        printf("    Lua chon cua ban : ");
        scanf("%d",&v);
        switch (v){
            case 1:
                fflush(stdin);
                printf("Nhap thong tin thanh pho\n");
                input(city,n);
                sys();
                break;
            case 2:
                output(city,n);
                sys();
                break;
            case 3:
                topN12(city,n);
                sys();
                break;
            case 4:
                topN12up(city,n);
                sys();
                break;
            case 5:
                v1(city,n);
                sys();
                break;
            case 6:
                v1up(city,n);
                sys();
                break;
            case 7:
                changeV1(city,n);
                sys();
                break;
            case 8:
                changeV2(city,n);
                sys();
                break;
            case 9:
                addCity(city,n);
                sys();
                break;
            case 10:
                deleteCity(city,n);
                sys();
                break;
            case 11:
                printf("\nThanks");
                exit(0);
            default:
                printf("\n\n\n                   ******************************************************\n");
                printf("                   **               Hay nhap dung ky tu                **\n");
                printf("                   ******************************************************\n\n\n");
                sys();
            
        }
        
        
    }while(v!=11);
    free(city);
     
}
  • 2
    "you don't have to pay attention to other things, just focus on what I describe." this is a lot of code, you should drill down your problem to a [mre] – yano Dec 30 '21 at 16:12
  • Add the compiler flag `-Wall` or in your IDE find the option to show all warnings. This code is filled with far more issues than what you're asking about. – codyne Dec 30 '21 at 16:13
  • sorry for the code, but i think it's in case 9 and addCity . I tried deleting the input of addCity function and the program does not stop by itself . – Ngoc Anh Dec 30 '21 at 16:17
  • All other tests are working normally, only case 9 has the phenomenon of self-stop I use case 9 to input data – Ngoc Anh Dec 30 '21 at 16:19

0 Answers0