Thread: Can anybody take a look at this?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    15

    Unhappy Can anybody take a look at this?

    I'm currently trying to write a (small) tax programme and have at last managed to do so, but not without my problems.

    My source code appears to be good, but when I run it (in Borland 3.0), the final output gets all screwed up. The code itself is pretty long:

    Code:
                     //Header Files
    
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <math.h>
    
    void main()      //Main
    {
    
                    //ABC Ltd. header
    
       printf("          -----------------------------------------------");
       printf("\n         |                                               |");
       printf("\n         |          ABC COMPANY LTD (MAURITIUS)          |");
       printf("\n         |                                               |");
       printf("\n         |              ANNUAL INCOME TAXES              |");
       printf("\n         |                                               |");
       printf("\n          -----------------------------------------------");
    
    
                    //Password protection
    
                    //Password: neoteric
    
                    //Encrypting password
    
       char encrypted[] = "EBuANANatYHcbQvIhAnU";
    
       char password[80];
       char unencrypted[] = "abcdefghijklmnopqrst";
    
       int i = 0;
       long code = 0;
    
       for(;;)
       {
       strcpy(unencrypted, "abcdefghijklmnopqrst");
       printf("\n\n\nEnter password (max. 20 characters, no spaces): ");
       scanf("%s", password);
    
       for( i= 0; i < 20 && password[i] != '\0'; i++)
       unencrypted[i] = password[i];
       code = 0;
    
       for(i=0; i < 20; code += unencrypted[i++]);
       code %= 52;
       code += code < 26 ? 'A' : ('a' - 26);
    
       for(i = 0; i < 20; i++)
       {
         code *= unencrypted[i];
         code %= 52;
         code += (code < 26) ? 'A': ('a' - 26);
    
         password[i] = (char)code;
       }
       password[i] = '\0';
    
                             //Checking password
    
       if(strcmp(password, encrypted) != 0)
       printf("\nAccess denied!\n");
       else
         {
          printf("\nLogged in.\n");
          break;
         }
    
       }
    
       clrscr();           //Clear screen
    
       printf("          -----------------------------------------------");
       printf("\n         |                                               |");
       printf("\n         |          ABC COMPANY LTD (MAURITIUS)          |");
       printf("\n         |                                               |");
       printf("\n         |              ANNUAL INCOME TAXES              |");
       printf("\n         |                                               |");
       printf("\n          -----------------------------------------------");
    
                               //Declaration of variables
       char title[3];
       char surname[60];
       char other_names[80];
       char maiden_name[60];
       char husband_name[100];
       char residential_address[120];
       char id[14];
       char marital_status[1];
       char name_spouse[100];
       char acc_spouse[8];
       char spouse_work[1];
       char children[1];
       char pensioner[1];
       char car_benef[1];
       char free_benef[1];
       char other_rev[1];
       int DOB;
       int DOM;
       int dept_no[3];
       int num_children;
       long phone_num_res[7];
       long phone_num_off[7];
       float basic_sal, total_sal, pension, emoluments, car_benefit, acc;
       float others, total_inc, emo_rel, total_relief, dep_child;
       float perso_deduc = 70000;
       float spouse_deduc = 55000;
       float child_deduc = 8000;
    
    
                   //Entry details
    
       textcolor(GREEN);
       cprintf("\n\n                      "
               "                             ENTRY DETAILS");
    
       printf("\n\n\nDepartment No. (3 digits): ");
       scanf("%d", &dept_no);
    
       printf("\nTitle (Ms/Mrs/Mr/Ww): ");
       scanf("%s", title);
    
       printf("\nSurname: ");
       scanf("%s", surname);
    
       printf("\nOther names: ");
       scanf("%s", other_names);
    
                  //Add maiden name and husband
                 //name if applicable
    
       if(strnicmp(title, "mrs", 3) == 0)
       {
         printf("\nMaiden name: ");
         scanf("%s", maiden_name);
    
         printf("\nName of husband: ");
         scanf("%s", husband_name);
       }
    
                //Continue with details
    
       printf("\nResidential address: ");
       scanf("%s", residential_address);
       getchar();
       getchar();
    
       printf("\nDate of birth (DD/MM/YY): ");
       scanf("%d", &DOB);
       getchar();
       getchar();
       getchar();
    
       printf("\nID No.: ");
       scanf("%s", id);
       getchar();
       getchar();
    
       printf("\nTelephone No. (Residence): ");
       scanf("%ld", &phone_num_res);
       getchar();
       getchar();
       getchar();
    
       printf("\nTelephone No. (Office): ");
       scanf("%ld", &phone_num_off);
       getchar();
       getchar();
    
                       //Verifying marital status
    
       if(strnicmp(title, "mr", 3) == 0)
       {
         printf("\nAre you married? (Y/N): ");
         scanf("%s", marital_status);
         getchar();
         getchar();
       }
    
                     //Other details for married men only
    
       if(strnicmp(marital_status, "y", 1) == 0)
       {
          printf("\nFull name of spouse: ");
          scanf("%s", name_spouse);
          getchar();
          getchar();
    
          printf("\nDate of marriage (DD/MM/YY): ");
          scanf("%d", &DOM);
          getchar();
          getchar();
    
          printf("\nTax Account No. of spouse: ");
          scanf("%s", &acc_spouse);
          getchar();
    
          printf("\nDoes your spouse work? (Y/N): ");
          scanf("%s", spouse_work);
       }
    
                           //Checking for dependent children
    
       printf("\nDo you have any children? (Y/N): ");
       scanf("%s", &children);
       getchar();
    
       if(strnicmp(children, "y", 1) == 0)
       {
         printf("\nHow many children?: ");
         scanf("%d", &num_children);
         getchar();
       }
    
       clrscr();          //Clear screen
    
       printf("          -----------------------------------------------");
       printf("\n         |                                               |");
       printf("\n         |          ABC COMPANY LTD (MAURITIUS)          |");
       printf("\n         |                                               |");
       printf("\n         |              ANNUAL INCOME TAXES              |");
       printf("\n         |                                               |");
       printf("\n          -----------------------------------------------");
    
                       //Requiring input for calculations
    
       cprintf("\n\n                      "
               "                           FINANCIAL DETAILS");
    
    
       printf("\n\nBasic salary: Rs ");
       scanf("%f", &basic_sal);
       getchar();
    
                        //Checking for pension
    
       printf("\n\nDo you touch any pension? (Y/N): ");
       scanf("%s", pensioner);
    
       if(strnicmp(pensioner, "y", 1) == 0)
       {
         printf("\n\nPension: Rs ");
         scanf("%f", &pension);
         getchar();
       }
    
       printf("\n\nArrears of emoluments: Rs ");
       scanf("%f", &emoluments);
       getchar();
    
                            //Checking for car benefit
    
       printf("\n\nAre you beneficiary of car benefit? (Y/N): ");
       scanf("%s", car_benef);
    
       if(strnicmp(car_benef, "y", 1) == 0)
       {
       printf("\n\nCar benefit: Rs ");
       scanf("%f", &car_benefit);
       getchar();
       }
    
                          //Checking for free accomodation
    
       printf("\n\nAre you beneficiary of free accomodation? (Y/N): ");
       scanf("%s", free_benef);
       getchar();
    
       if(strnicmp(free_benef, "y", 1) == 0)
       {
         printf("\n\nFree accomodation: Rs ");
         scanf("%f", &acc);
         getchar();
    
       }
    
                       //Checking for additional income
    
       printf("\n\nDo you get any revenue through other means"
              " (contract jobs, business, etc...)? (Y/N): ");
       scanf("%s", other_rev);
       getchar();
    
       if(strnicmp(other_rev, "y", 1) == 0)
       {
          printf("\n\nOther revenue: Rs ");
          scanf("%f", &others);
          getchar();
       }
    
       clrscr();
    
    
                     //Determining and displaying
                    //personal reliefs and deductions
    
       printf("\n\nBasic personal deduction is Rs %f", perso_deduc);
    
                     //Dependant spouse deduction where applicable
    
       if(strnicmp(spouse_work, "n", 1) == 0)
       {
         printf("\nDeduction for dependant spouse is Rs %f", spouse_deduc);
         getchar();
       }
    
                    //Dependant children deduction where applicable
    
       if(strnicmp(children, "y", 1) == 0)
       {
         dep_child = num_children * child_deduc;
         printf("\n\nDeduction for dependent children is Rs %f", dep_child);
         getchar();
       }
    
       clrscr();
    
    
       printf("          -----------------------------------------------");
       printf("\n         |                                               |");
       printf("\n         |          ABC COMPANY LTD (MAURITIUS)          |");
       printf("\n         |                                               |");
       printf("\n         |              ANNUAL INCOME TAXES              |");
       printf("\n         |                                               |");
       printf("\n          -----------------------------------------------");
    
                       //Generating report
    
       cprintf("\n\n                      "
               "                           FINAL REPORT");
    
                      //Displaying all the details
    
       printf("\n\nDepartment No. %d.", dept_no);
       getchar();
    
       printf("\n%s %s %s", title, surname, other_names);
       getchar();
    
       if(strnicmp(title, "mrs", 3) == 0)
       {
         printf("\nOf maiden name %s.", maiden_name);
         getchar();
    
         printf("\nName of husband is %s.", husband_name);
         getchar();
       }
    
       printf("\n%s.", residential_address);
       getchar();
    
       printf("\nBorn on %d.", DOB);
       getchar();
    
       printf("\nNational Identity No. %s.", id);
       getchar();
    
       printf("\nHome telephone number: %ld.", phone_num_res);
       getchar();
    
       printf("\nOffice telephone number: %ld.", phone_num_off);
       getchar();
    
       if(strnicmp(title, "mr", 3) == 0)
       {
         printf("\nFull name of spouse is Mrs %s.", name_spouse);
         getchar();
    
         printf("\nDate of marriage (DD/MM/YY) is %d.", DOB);
         getchar();
    
         printf("\nTax Account No. of spouse is %s.", acc_spouse);
         getchar();
       }
    
       if(strnicmp(children, "y", 1) == 0)
       {
         printf("\nNumber of dependent children is %d.", num_children);
         getchar();
       }
    
                                  //Showing values
    
       printf("\n\nBasic salary is Rs %f", basic_sal);
    
       printf("\n\nEnd-of-year bonus is Rs %f", basic_sal);
    
                 //Calculation of total salary for one year
    
       total_sal = (basic_sal * 12) + basic_sal;
       printf("\n\nTotal salary is Rs %f", total_sal);
    
    
       if(strnicmp(pensioner, "y", 1) == 0)
       {
         printf("\n\nPension received is Rs %f", pension);
       }
    
       printf("\n\nArrears of emoluments amount to Rs %f", emoluments);
    
       if(strnicmp(car_benef, "y", 1) == 0)
       {
       printf("\n\nCar benefit amounts to Rs %f", car_benefit);
       }
    
       if(strnicmp(free_benef, "y", 1) == 0)
       {
         printf("\n\nFree accomodation amounts to Rs %f", acc);
       }
    
          if(strnicmp(other_rev, "y", 1) == 0)
       {
          printf("\n\nOther revenue amounts to Rs %f", others);
       }
    
    
                       //Income Tax Calculations
                       //Displaying results of calculations
    
                       //Calculating the total income
    
       total_inc = total_sal + pension + emoluments + car_benefit + acc + others;
       printf("\n\nYour total income for this year is Rs %f", total_inc);
       getchar();
    
       emo_rel = 0.15 * total_inc;
       if(total_inc < 433333.333333)
       {
         printf("\nEmoluments relief (max. Rs 65000): Rs %f", emo_rel);
         getchar();
       }
    
    
                       //Total personal reliefs to be
                       //deducted from net income
    
       total_relief = emo_rel + perso_deduc + spouse_deduc + pension + dep_child;
       printf("\n\nTotal personal reliefs and deductions amount to Rs %f"
                                                                , total_relief);
    
    
       if(total_inc > total_relief)
       printf("\n\nTax payable for this year is Rs %f", total_inc - total_relief);
       getchar();
    
       if(total_inc < total_relief)
       printf("\n\nTax to be paid in excess for this year is Rs %f",
                                            total_relief - total_inc);
       getchar();
    
       if(total_inc == total_relief)
       printf("\n\nExempt of taxes for this year.");
       getchar();
    
    
                       //List of documents to be attached
    
       printf("\n\nPlease note that the following documents are required:");
       printf("\n\n1.  Original of Statement of Emoluments and Tax Deduction;");
       printf("\n\n2.  Appropriate receipts/certificates for the reliefs and"
              " deductions claimed.");
       printf("\n\nPayment should be made by crossed cheque payable to"
              " the Commissioner of Income Tax.");
       printf("\n\nFull name and Tax Account Number are to be written "
              "on the verso of the cheque.");
    
    
    getchar();
    getchar();
    getchar();
    getchar();
    
    }

    Does anybody know what's wrong with it?

    Criticism and additional comments on how to improve the program are also welcome (I'm still a beginner).

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>void main() //Main
    No, its int main(void) and return 0; at the end.

    >>char encrypted[] = "EBuANANatYHcbQvIhAnU";
    Variable declarations are not allowed to come after the code starts, so you need to move these to the top of the function block.

    >>long phone_num_off[7];
    This (and others) are arrays of numbers. You really one need one number (I think), so use
    >>long phone_num_off;
    OR keep it as a char array:
    >>char phone_num_off[8];
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    im using msvc 6 and heres what i got

    Code:
    --------------------Configuration: file - Win32 Debug--------------------
    Compiling...
    file.c
    file.obj - 84 error(s), 43 warning(s)
    the thing that gets me...when i go through and look at some of these errors they dont make sense? im also kinda confused about your for loops?...are they supposed to be nested inside one another or separate loops...?
    Last edited by dP munky; 11-20-2002 at 03:07 PM.
    guns dont kill people, abortion clinics kill people.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Gee, you should really consider splitting up that code into smaller parts (functions). It makes it much easier to read/search/debug .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    15
    Hammer - Yeah, I figured out about the telephone numbers. Thanks anyway. As for your corrections, I'll try them out as soon as possible. What you're saying makes sense, it'll very probably work. So, thanks again.

    dP munky - In its current state, most loops in the code are separate along with a couple of nested ones. I think I'm going to alter those a bit though (to improve the interface).

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    15
    Magos - I tried that, but the programme wouldn't run and I couldn't fix that. Could you give me an example of how to do it? I'll check it later then.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by TerryBogard
    Magos - I tried that, but the programme wouldn't run and I couldn't fix that. Could you give me an example of how to do it? I'll check it later then.
    Usually, you can't just take that code, split it in parts and place those parts in different functions, cause of dependencies (sp?).
    The splitting is something you have to consider on the planning stage.

    As example, you could place all your printouts in a separate function, ie: void PrintHeader(), void PrintMenu(), void PrintResult().
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    15
    Yeah, I'm doing it again right now; using 3 functions -- details, calculations, and report.

    I'll post it once I'm done, perhaps tomorrow.

    Thanks for the pointer.

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    15
    Magos - Well, I did what you said and opted for functions instead (you'll notice that it's a different kind of programme now --much shorter, but that's not very important, I think).

    Anyway, the coding is good (i.e. it runs, no errors) but the program itself is faulty. When I input my choice, be it 1, 2 or 3, the line "Invalid option. Please choose again." gets always displayed, no matter what. Why is this occuring?

    Also, the little logo and inputted password remain on the screen in spite of the 'clrscr()'. Have I misplaced it?

    Code:
    #include <stdio.h>         //Header Files
    #include <string.h>
    #include <conio.h>
    
    void menu(void);
    void details(void);           //Function declarations
    void financial(void);
    void report(void);
    
    int date;
    int dept_no[3];
    char dept_name[15];                  //Declaration of variables
    char dept_leader_surname[60];
    char dept_leader_other[80];
    int tel_no_first[7];
    int tel_no_sec[7];
    int fax[7];
    char other_means[1];
    char charity[1];
    float reg_inc, fringe, allocation, bonus, donation;
    float wages, maintain, other_issues, emp_needs;
    float donation_deduc = 20000;
    
    
    void main(void)      //Main
    {
    
                    //ABC Ltd. header
    
    
       printf("          -----------------------------------------------");
       printf("\n         |                                               |");
       printf("\n         |          ABC COMPANY LTD (MAURITIUS)          |");
       printf("\n         |                                               |");
       printf("\n         |              ANNUAL INCOME TAXES              |");
       printf("\n         |                                               |");
       printf("\n          -----------------------------------------------");
    
    
                    //Password protection
    
                    //Password: neoteric
    
                    //Encrypting password
    
       char encrypted[] = "EBuANANatYHcbQvIhAnU";
    
       char password[80];
       char unencrypted[] = "abcdefghijklmnopqrst";
    
       int i = 0;
       long code = 0;
    
       for(;;)
       {
       strcpy(unencrypted, "abcdefghijklmnopqrst");
       printf("\n\n\nEnter password (max. 20 characters, no spaces): ");
       scanf("%s", password);
    
       for( i= 0; i < 20 && password[i] != '\0'; i++)
       unencrypted[i] = password[i];
       code = 0;
    
       for(i=0; i < 20; code += unencrypted[i++]);
       code %= 52;
       code += code < 26 ? 'A' : ('a' - 26);
    
       for(i = 0; i < 20; i++)
       {
         code *= unencrypted[i];
         code %= 52;
         code += (code < 26) ? 'A': ('a' - 26);
    
         password[i] = (char)code;
       }
       password[i] = '\0';
    
                             //Checking password
    
       if(strcmp(password, encrypted) != 0)
       printf("\nAccess denied!\n");
       else
         {
          printf("\nLogged in.\n");
          break;
         }
    
       }
    
       getchar();
    
       clrscr;
       menu();
                                         //Clear screen
    }
    
    
       void menu(void)                 //Menu
       {
         printf("\n\nPlease choose desired option:");
         printf("\n\n\t[1] Entry details.");
         printf("\n\n\t[2] Financial details.");
         printf("\n\n\t[3] Display final report.");
         gotoxy(13,23);
    
         int choice;
    
         scanf("%d", &choice);
    
    
         switch(choice)            //Determine chosen option
         {
           case '1':
           details();
           break;
    
           case '2':
           financial();
           break;
    
           case '3':
           report();
           break;
    
           default:
           printf("\n\nInvalid option. Please choose again.");
           getchar();
    
         }
    
         getchar();
         getchar();
       }
    
    
       void details(void)
       {
    
         textcolor(GREEN);
         cprintf("\n\n              ENTRY DETAILS");
    
         printf("\n\nDate (DD/MM/YY): ");
         scanf("%d", &date);
    
         printf("\n\nDepartment No. (3 digits): ");
         scanf("%d", &dept_no);
    
         printf("\n\nFull department name: ");
         scanf("%s", dept_name);
    
         printf("\n\nDepartment manager's surname: ");
         scanf("%s", dept_leader_surname);
    
         printf("\n\nDepartment leader's other names: ");
         scanf("%s", dept_leader_other);
    
         printf("\n\nTelephone number 1: ");
         scanf("%d", &tel_no_first);
    
         printf("\n\nTelephone number 2: ");
         scanf("%d", &tel_no_sec);
    
         printf("\n\nFax number: ");
         scanf("%d", &fax);
    
         getchar();
         getchar();
    
       }
    
    
       void financial(void)
       {
         cprintf("\n\n               FINANCIAL DETAILS");
    
                                     //Income
    
         printf("\n\nNet regular income: Rs ");
         scanf("%f", &reg_inc);
    
         printf("\n\nDid department perceive any income"
                " through other means (Y/N: ");
         scanf("%s", other_means);
    
         if(other_means == "y" || other_means == "Y")
         {
           printf("Fringe benefits: Rs ");
           scanf("%f", &fringe);
         }
    
         printf("\n\nAmount allocated by company: Rs ");
         scanf("%f", &allocation);
    
         printf("\n\nBonus emoluments: Rs ");
         scanf("%f", &bonus);
    
    
                                      //Deductions
    
         printf("\n\nDid department donate money to charitable"
                " institutions? (Y/N): ");
         scanf("%s", charity);
    
         if(charity == "Y" || charity == "Y")
         {
           printf("\n\nHow much? Rs ");
           scanf("%s", &donation);
         }
    
         if(donation > 10000)
         {
           printf("\n\nDeduction for donation: Rs %0.2f", donation_deduc);
         }
    
    
                                   //Expenses
    
         printf("\n\nAmount spent as wages to employees: Rs ");
         scanf("%f", &wages);
    
         printf("\n\nAmount spent to cater for employees' other needs: Rs ");
         scanf("%f", &emp_needs);
    
         printf("\n\nAmount spent on maintenance and similar issues: Rs ");
         scanf("%f", &maintain);
    
         printf("\n\nAmount spent on any other issues: Rs ");
         scanf("%f", &other_issues);
    
         clrscr();
    
         getchar();
         getchar();
    
                             //Performing calculations
    
       }
    
       void report(void)
       {
         cprintf("\n\n                   REPORT");
    
    
         getchar();
         getchar();
    
       }

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
         scanf("%d", &choice);
    
    
         switch(choice)            //Determine chosen option
         {
           case '1':
           details();
           break;
    
           case '2':
           financial();
           break;
    
           case '3':
           report();
           break;
    
           default:
           printf("\n\nInvalid option. Please choose again.");
           getchar();
    
         }
    This could be the problem. You read in an integer ( %d ), but compare it to characters.
    Either read an integer ( %d ) and compare it to integers (case 1: , case 2: etc...), or read a charcater ( %c ) and compare it to characters (case '1':, case '2' etc...).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    15

    Wink

    Believe it or not, I was never aware of this fact (but then, it's not that I ever used a switch statement either before).

    Again, thanks for the help. With this part solved, I'll hopefully be able to do the rest all by myself. Hopefully.

Popular pages Recent additions subscribe to a feed