+ Reply to Thread
Results 1 to 11 of 11
  1. #1
    Ultimate Member
    Join Date
    Jan 2000
    Location
    Melb, Australia
    Posts
    1,287

    Can anyone debug this program?

    I'm quite new to C, and just started on using functions, but I'm quite sure this code is right, but my compiler displays an error within the functions.

    #include <stdio.h>

    int option,var1,var2,exec;
    int add(int add1,add2);
    int sub(int sub1,sub2);
    int multi(int multi1,multi2);
    int divid(int divid1,divid2);

    main()
    {
    printf("Please enter a value: ");
    scanf("%d",&var1);
    printf("Please choose an operation:");
    printf("\n1. Addition");
    printf("\n2. Subtraction");
    printf("\n3. Multiplication");
    printf("\n4. Division\n");
    scanf("d",&option);
    printf("Please enter a second value: ");
    scanf("d",&var2);

    if (option == 1)
    exec=add(var1,var2);
    else
    if (option == 2)
    exec=sub(var1,var2);
    else
    if (option == 3)
    exec=multi(var1,var2);
    else
    exec=divid(var1,var2);

    printf("\nThe Solution is %d.\n",exec);

    return 0;
    }

    int add(int add1,add2)
    {
    int x_add;

    x_add=add1+add2;

    return x_add;
    }

    int sub(int sub1,sub2)
    {
    int x_sub;

    x_sub=sub1-sub2;

    return x_sub;
    }

    int multi(int multi1,multi2)
    {
    int x_multi;

    x_multi=multi1*multi2;

    return x_multi;
    }

    int divid(int divid1,divid2)
    {
    int x_divid;

    x_divid=divid1/divid2;

    return x_divid;
    }

  2. #2
    Member
    Join Date
    Mar 1999
    Location
    Massachusetts
    Posts
    387
    What exactly does the compiler complain about? Whats the error?

  3. #3
    Banned qball's Avatar
    Join Date
    Oct 1999
    Location
    Rockaway, NJ 07866
    Posts
    1,730
    if (option == 1)
    exec=add(var1,var2);
    else
    if (option == 2)
    exec=sub(var1,var2);
    else
    if (option == 3)
    exec=multi(var1,var2);
    else
    exec=divid(var1,var2);
    Should be:
    if (option == 1)
    {exec=add(var1,var2);}
    else
    if (option == 2)
    {exec=sub(var1,var2);}
    else
    if (option == 3)
    {exec=multi(var1,var2);}
    else
    {exec=divid(var1,var2);}

    Or sumtin like that, you need to mind your braces.

    [This message has been edited by qball (edited 07-15-2001).]

  4. #4
    Senior Member
    Join Date
    May 1999
    Location
    Denver, CO, USA
    Posts
    775
    I don't know C well enough to be positive about this, but I thought that if there are no brackets, then the if statement performs the command directly after it, but then continues on with the rest of the code.

    -Z

  5. #5
    Member emcron's Avatar
    Join Date
    Mar 1999
    Location
    Universe
    Posts
    450
    If you post the error message you get, it would help a lot. First I dont think the braces are the problem. Also if you want to make you program simpler use the "switch" statement.

  6. #6
    Member
    Join Date
    Mar 1999
    Location
    Massachusetts
    Posts
    387
    Actually.. i just noticed something...

    These are your prototypes:
    Code:
    int add(int add1,add2);
    int sub(int sub1,sub2);
    int multi(int multi1,multi2);
    int divid(int divid1,divid2);
    Don't make the parameters into lists like you declare your variables (btw - global variables should be avoided if you can.. infact, by using globals like you have, you don't even need to pass the arguments.. see my adjustment below).. try changing the first few lines to this:

    Code:
    int add(int add1, int add2);
    int sub(int sub1, int sub2);
    int multi(int multi1, int multi2);
    int divid(int divid1, int divid2);
    
    main()
    {
      int option,var1,var2,exec;
    Also do it on the functions themselves... add the argument types to each variable.


  7. #7
    Banned qball's Avatar
    Join Date
    Oct 1999
    Location
    Rockaway, NJ 07866
    Posts
    1,730
    zskillz,

    Correct for simple if statement (inline). Once if-then-else is desired, needum brackets.

    Curious as to the actual problem.

  8. #8
    Ultimate Member
    Join Date
    Jan 2000
    Location
    Melb, Australia
    Posts
    1,287
    Thanks Jamis, I'll try your adjustment when I get back from school! I'll see if the adjustment works and I'll post it up.

  9. #9
    Junior Member
    Join Date
    May 2001
    Location
    San Jose, Ca, USA
    Posts
    20
    Hi Wilan,

    Here's your debugged code. Your errors were in your prototype statements like Jamis pointed out. You need to specify the data type for each parameter. In other words, you can't do int function (int x,y,z);

    Then after you fix the prototypes, you'll have to copy and paste them to replace the function definitions (just don't copy the ";").

    Hope this helps. Also, you should get in the habit of commenting your code more.

    Best regards,

    techwriter

    #include <stdio.h>
    int option,var1,var2,exec;
    int add(int add1,int add2);
    int sub(int sub1, int sub2);
    int multi(int multi1, int multi2);
    int divid(int divid1, int divid2);
    main()
    {
    printf("Please enter a value: ");
    scanf("%d",&var1);
    printf("Please choose an operation:");
    printf("\n1. Addition");
    printf("\n2. Subtraction");
    printf("\n3. Multiplication");
    printf("\n4. Division\n");
    scanf("%d",&option);
    printf("Please enter a second value: ");
    scanf("%d",&var2);
    if (option == 1)
    exec=add(var1,var2);
    else
    if (option == 2)
    exec=sub(var1,var2);
    else
    if (option == 3)
    exec=multi(var1,var2);
    else
    exec=divid(var1,var2);
    printf("\nThe Solution is %d.\n",exec);
    return 0;
    }
    int add(int add1,int add2)
    {
    int x_add;
    x_add=add1+add2;
    return x_add;
    }
    int sub(int sub1, int sub2)
    {
    int x_sub;
    x_sub=sub1-sub2;
    return x_sub;
    }
    int multi(int multi1, int multi2)
    {
    int x_multi;
    x_multi=multi1*multi2;
    return x_multi;
    }
    int divid(int divid1, int divid2)
    {
    int x_divid;
    x_divid=divid1/divid2;
    return x_divid;
    }


    [This message has been edited by techwriter (edited 07-18-2001).]

  10. #10
    Ultimate Member
    Join Date
    Jan 2000
    Location
    Melb, Australia
    Posts
    1,287
    Thanks everyone for your replies.. the source compiled successfully! But, the program ended after typing out the first value, and it just printed out the options available.. and the program ended.. wot did i do wrong this time!? I'm very new to C, and the language is so different from the ones i previously done, which was vb.

  11. #11
    Junior Member
    Join Date
    May 2001
    Location
    San Jose, Ca, USA
    Posts
    20
    Wilan,

    I'm not sure I understand your question here: "...the source compiled successfully! But, the program ended after typing out the first value, and it just printed out the options available.. and the program ended..."

    You should just copy and paste the code I gave you in my previous reply into your compiler, then run it. It worked on my machine using MS visual C++ 6.0 compiler.

    If you're asking why the program stops after computing the first value, its because you programmed it to do so. If you want the program to continue printing the menu after computing the first two values, you should put your menu in a do...while loop and also, add an option to quit the program. The do portion basically saying to display the menu, and the while condition being while not the value you decide for stopping the program.

    Hope this helps clear things up a bit.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts







New Security Features Planned for Firefox 4
Another Laptop Theft Exposes 21K Patients' Data
Oracle Hits to Road to Pitch Data Center Plans
Microsoft Preps Array of Windows Patches
Microsoft Nears IE9 Beta With Final Preview
Simplified Analytics Improve CRM, BI Tools
Android Passes RIM as Top Mobile OS in 2Q
VMware Updates Hyperic System Management
File Monitoring Key to Enterprise Security
LinkedIn Snaps Up SaaS Player mSpoke