Jump to content

What should I do? c programming

 

Good day everyone, I'm new in programming and using c language, I'am currently exploring and trying out but I expected my program to work but I don't know what the problem, What should I do?

 


#include<stdio.h>

    void Entry_Array()
    {
        int Em_Num[10],a,r,c,t,p,s,m;
        int Hours_W[10];
        char Em_Stat[10]={'r','R','p','P','c','C','t','T'};
        char C_Stat[10]={'s','S','m','M'};
        
             printf("Enter the Employee Number:\n");
                             
                 for(a=0;a<10;a++)
                 {
                     scanf("%d",&Em_Num[a]);
                }
                        
             printf("Enter the Employment Status:\n");
             
                for(a=0;a<10;a++)
                {
                    scanf("%s",&Em_Stat[a]);
                }
                            
            printf("Enter the Civil Status:\n");
                            
                for(a=0;a<10;a++)
                {
                    scanf("%s",&C_Stat[a]);
                }
                            
            printf("Enter the Hours Work:\n");
                    
                for(a=0;a<10;a++)
                {
                    scanf("%d",&Hours_W[a]);
                }
                                
                for(a=0;a<10;a++)
                {
                    printf("\nEmployee Number:%d\n",Em_Num[a]);
                    printf("Employment Status:%c\n",Em_Stat[a]);
                    printf("Civil Status:%c\n",C_Stat[a]);
                    printf("Hours Work:%d\n",Hours_W[a]);
                }
    }
    
    void Search_Array()
    {
        int Em_Num[10],a,r,c,t,p,s,m;
        int Hours_W[10],Search,Flag;
        int G_Inc,Net_Inc,RPD,RPH,OTH,OT,Basic,P_Health;
        float SSS,P_i,T_Ded;
        char Em_Stat[10]={'r','R','p','P','c','C','t','T'};
        char C_Stat[10]={'s','S','m','M'};
        
        printf("Enter the Employee Number to be Search:");
            
                for(a=0;a<10;a++)
                {
                    scanf("%d",&Search);
                }
            printf("\n");
            
                for(a=0;a<10;a++)
                {
                    if(Em_Num[a]==Search)
                    {
                        Flag=1;
                        break;
                    }
                }
                
                if(Flag==1)
                {
                        switch(Em_Stat[a])
                        {
                            case 'R':
                                            RPD=800;
                                            break;
                            case 'P':
                                            RPD=600;
                                            break;
                                            
                            case 'C':
                                            RPD=500;
                                            break;
                            case 'T':
                                            RPD=450;
                                            break;
                            default:
                                            RPD=400;
                                            break;
                        }
                    
                    RPH=RPD/8;
                    
                        if(Hours_W[a]>120)
                        {
                            OTH=Hours_W[a]-120;
                            OT=1.5*(RPH*OTH);
                            
                            Basic=120*RPH;
                            G_Inc=Basic+OT;
                        }
                        else
                        {
                            Basic=Hours_W[a]*RPH;
                            G_Inc=Basic;
                        }
                        
                        if(C_Stat[a]=='S'||C_Stat[a]=='s')
                        {
                            P_Health=500;
                        }
                        else if(C_Stat[a]=='M'||C_Stat[a]=='m')
                        {
                            P_Health=300;
                        }
                        else
                        {
                            P_Health=400;
                        }
                        
                        if(G_Inc>12000)
                        {
                            SSS=0.1;
                            P_i=0.05;
                        }
                        else if(G_Inc>5000)
                        {
                            SSS=0.08;
                            P_i=0.03;
                        }
                        else if(G_Inc<=5000)
                        {
                            SSS=0.05;
                            P_i=0.02;
                        }
                        
                        T_Ded=P_Health+SSS+P_i;
                        Net_Inc=G_Inc-T_Ded;
                        
                        printf("\nEmployee Number:%d\n",Em_Num[a]);
                        printf("Employment Status:%c\n",Em_Stat[a]);
                        printf("Civil Status:%c\n",C_Stat[a]);
                        printf("Hours Work:%d\n",Hours_W[a]);
                        printf("Gross Income=%d",G_Inc);
                        printf("Total Deduction=%f\n",T_Ded);
                        printf("Net Income=%d\n",Net_Inc);
                }
                else
                {
                    printf("\nEmployee Number is not Found");
                }
    }
    
    void Update_Array()
    {
        int Em_Num[10],a,r,c,t,p,s,m;
        int Hours_W[10],Search,Flag,TBU,TU;
        int G_Inc,Net_Inc,RPD,RPH,OTH,OT,Basic,P_Health;
        float SSS,P_i,T_Ded;
        char Em_Stat[10]={'r','R','p','P','c','C','t','T'};
        char C_Stat[10]={'s','S','m','M'};
        
        printf("Enter the Employee Number to be Updated:\n");
        scanf("%d",&TBU);
        
            for(a=0;a<10;a++)
            {
                if(Em_Num[a]==TBU)
                {
                    printf("Enter the Values to Update:\n");
                    scanf("%d",&TU);
                    
                    Em_Num[a]=TU;
                    
                    printf("Successfully Updated the Values\n");
                }
                else
                {
                    printf("The Employee Number does not exist\n");
                }
            }
    }
    
    void Display_Array()
    {
        int Em_Num[10],a,r,c,t,p,s,m;
        int Hours_W[10],Search,Flag,TBU,TU;
        int G_Inc,Net_Inc,RPD,RPH,OTH,OT,Basic,P_Health;
        float SSS,P_i,T_Ded;
        char Em_Stat[10]={'r','R','p','P','c','C','t','T'};
        char C_Stat[10]={'s','S','m','M'};
        
        switch(Em_Stat[a])
        {    
            case 'R':
                    RPD=800;
                    break;
                    
            case 'P':
                    RPD=600;
                    break;
                                                            
            case 'C':
                    RPD=500;
                    break;
            
            case 'T':
                    RPD=450;
                    break;
            
            default:
                    RPD=400;
                    break;
        }
                                    
            RPH=RPD/8;
                                
            if(Hours_W[a]>120)
            {
                OTH=Hours_W[a]-120;
                OT=1.5*(RPH*OTH);
                                    
                Basic=120*RPH;
                G_Inc=Basic+OT;
            }
            else
            {
                Basic=Hours_W[a]*RPH;
                G_Inc=Basic;
            }
                                        
            if(C_Stat[a]=='S')
            {
                P_Health=500;
            }
            else if(C_Stat[a]=='M')
            {
                P_Health=300;
            }
            else
            {
                P_Health=400;
            }
                                        
            if(G_Inc>12000)
            {
                SSS=0.1;
                P_i=0.05;
            }
            else if(G_Inc>5000)
            {
                SSS=0.08;
                P_i=0.03;
            }
            else if(G_Inc<=5000)
            {
                SSS=0.05;
                P_i=0.02;
            }
                                        
                T_Ded=P_Health+SSS+P_i;
                Net_Inc=G_Inc-T_Ded;
        
        printf("\nEmployee Number:%d\n",Em_Num[a]);
        printf("Employment Status:%c\n",Em_Stat[a]);
        printf("Civil Status:%c\n",C_Stat[a]);
        printf("Hours Work:%d\n",Hours_W[a]);
        printf("Gross Income=%d",G_Inc);
        printf("Total Deduction=%d\n",T_Ded);
        printf("Net Income=%d\n",Net_Inc);
    }
    void Choice()
    {
        int Choice;
        char Answer;
        
        do
        {
            printf("----MENU----\n\n 1.ENTRY \n 2.SEARCH \n 3.UPDATE \n 4.DISPLAY \n 5.EXIT\n\n");
            
            printf("Enter Choice: ");
            scanf("%d",&Choice);
            
            switch(Choice)
            {
                case 1:
                        Entry_Array();
                        break;
                case 2:
                        Search_Array();
                        break;
                case 3:
                        Update_Array();
                        break;
                case 4:
                        Display_Array();
                        break;
                case 5:
                        printf("Do you want to Exit?");
                        break;
                default:
                        printf("Invalid Input\n");
                        break;
            }
            
            printf("Do you wish to exit[y/n]:\n");
            scanf("%s",&Answer);
        }while(Answer=='n'||Answer=='N');
    }
    
    void main()
    {
        Choice();
    }

 

Link to comment
Share on other sites

Link to post
Share on other sites

In what way does it not work?

 

- Does it not compile? In that case the compiler should tell you what the issue is. Ideally include the error message you're getting.

- Does it not work in the sense that it doesn't do what you expected? In that case you should mention what exactly it is you're trying to do.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Eigenvektor said:

In what way does it not work?

 

- Does it not compile? In that case the compiler should tell you what the issue is. Ideally include the error message you're getting.

- Does it not work in the sense that it doesn't do what you expected? In that case you should mention what exactly it is you're trying to do.

it compiles and there's no issues, when I tried to run the program works and functions but when I tried the different choices it doesn't show the entered data when I tried updating, searching and displaying the data.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Eigenvektor said:

Does it not compile? In that case the compiler should tell you what the issue is. Ideally include the error message you're getting.

^ in the same sense as this,

Assuming you're using GCC, 

ensure you use -Wall and -Wextra 

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, adrian nogo said:

it compiles and there's no issues, when I tried to run the program works and functions but when I tried the different choices it doesn't show the entered data when I tried updating, searching and displaying the data.

As far as I can see, all of your variables are local to each method, so there is no global data shared between them. Meaning the data inside these variables is lost as soon as you leave the method.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Eigenvektor said:

As far as I can see, all of your variables are local to each method, so there is no global data shared between them. Meaning the data inside these variables is lost as soon as you leave the method.

how to create a global data? can you give me and example please?

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, adrian nogo said:

how to create a global data? can you give me and example please?

Move the variables out of the functions. A variable declared inside a function only exists as long as you are inside that function.

 

So instead of this:

#include<stdio.h>

void Entry_Array()
{
    int Em_Num[10],a,r,c,t,p,s,m;
    int Hours_W[10];
    char Em_Stat[10]={'r','R','p','P','c','C','t','T'};
    char C_Stat[10]={'s','S','m','M'};
    
    …
}

…

 

You need to do this:

#include<stdio.h>
int Em_Num[10],a,r,c,t,p,s,m;
int Hours_W[10];
char Em_Stat[10]={'r','R','p','P','c','C','t','T'};
char C_Stat[10]={'s','S','m','M'};

void Entry_Array()
{
    
    …
}

…

 

These variables are now "global" and accessible by all methods and the data that resides in them will exist as long as your program is running. This also means you don't need to declare them multiple times.

 

~edit: Some background info:

https://www.tutorialspoint.com/cprogramming/c_scope_rules.htm

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Eigenvektor said:

Move the variables out of the functions. A variable declared inside a function only exists as long as you are inside that function.

 

So instead of this:


#include<stdio.h>

void Entry_Array()
{
    int Em_Num[10],a,r,c,t,p,s,m;
    int Hours_W[10];
    char Em_Stat[10]={'r','R','p','P','c','C','t','T'};
    char C_Stat[10]={'s','S','m','M'};
    
    …
}

…

 

You need to do this:


#include<stdio.h>
int Em_Num[10],a,r,c,t,p,s,m;
int Hours_W[10];
char Em_Stat[10]={'r','R','p','P','c','C','t','T'};
char C_Stat[10]={'s','S','m','M'};

void Entry_Array()
{
    
    …
}

…

 

These variables are now "global" and accessible by all methods and the data that resides in them will exist as long as your program is running. This also means you don't need to declare them multiple times.

 

~edit: Some background info:

https://www.tutorialspoint.com/cprogramming/c_scope_rules.htm

thank you master. have a good day :D

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×