#include<stdio.h>
#include<conio.h>
#include<dos.h>
main()
{
    int a,b,c,number;
    clrscr();

    int ad() {
        clrscr();
        printf("Enter first number");
        scanf("%d",&a);
        printf("Enter  second number");
        scanf("%d",&b);
        c=a+b;
        printf("The answer is %d",c);
    }
    int sub() {
        clrscr();
        printf("Enter first number");
        scanf("%d",&a);
        printf("Enter  second number");
        scanf("%d",&b);
        c=a-b;
        printf("The answer is %d",c);
    }
    int mult() {
        clrscr();
        printf("Enter first number");
        scanf("%d",&a);
        printf("Enter  second number");
        scanf("%d",&b);
        c=a*b;
        printf("The answer is %d",c);
    }
    int division() {
        clrscr();
        printf("Enter first number");
        scanf("%d",&a);
        printf("Enter  second number");
        scanf("%d",&b);
        c=a/b;
        printf("The answer is %f",d);
    }
    int main() {
        do {
            ad();
            sub();
            mult();
            division();
            while(ans!=13||ans!13);
        }
        do {
            if(number=1) {
                ad();
            }
            else if(number==2) {
                sub();
            }
            else if(number==3) {
                mult();
            }
            else if(number==4) {
                division();
            }
            ans=getche();
        }
        while(ans!=13||ans!13);
    }
    getch();
}
what wrong with my code? help me please....

Recommended Answers

All 3 Replies

>>I don't know how to debug my own code,

Well, you had better start learning. The first thing you need to do is to tell us what's wrong with the program? What does it do that it's not supposed to do, or vice versa.

The first thing I noticed is that you have declared functions within functions. For example main() starts on line 4, then add() starts on line 9 but before main() is finished. You can't do that. You have to finish one function before coding another function.

To debug your code, add output statements in key places to show you what key values are at each step of the program. If one of the values displayed is wrong, you now look at the code to find out why. Of course, you need to know what the values should be at every step.

#include<stdio.h>
#include<conio.h>
#include<dos.h>
main()
{
    int a,b,c,number;
    clrscr();

    int ad() {
        clrscr();
        printf("Enter first number");
        scanf("%d",&a);
        printf("Enter  second number");
        scanf("%d",&b);
        c=a+b;
        printf("The answer is %d",c);
    }
    int sub() {
        clrscr();
        printf("Enter first number");
        scanf("%d",&a);
        printf("Enter  second number");
        scanf("%d",&b);
        c=a-b;
        printf("The answer is %d",c);
    }
    int mult() {
        clrscr();
        printf("Enter first number");
        scanf("%d",&a);
        printf("Enter  second number");
        scanf("%d",&b);
        c=a*b;
        printf("The answer is %d",c);
    }
    int division() {
        clrscr();
        printf("Enter first number");
        scanf("%d",&a);
        printf("Enter  second number");
        scanf("%d",&b);
        c=a/b;
        printf("The answer is %f",d);
    }
    int main() {
        do {
            ad();
            sub();
            mult();
            division();
            while(ans!=13||ans!13);
        }
        do {
            if(number=1) {
                ad();
            }
            else if(number==2) {
                sub();
            }
            else if(number==3) {
                mult();
            }
            else if(number==4) {
                division();
            }
            ans=getche();
        }
        while(ans!=13||ans!13);
    }
    getch();
}
what wrong with my code? help me please....

If you are using turbo C, then you can debug the code by first compiling the program and then pressing f7 for each statement.Further you can also add watches to your program that will keep track of values of the variables while debugging.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.