Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,755 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,459 people online right now. Registration is fast and FREE... Join Now!




c program in function

 
Reply to this topicStart new topic

c program in function, perform 5 math function

pascal
22 Feb, 2007 - 06:03 PM
Post #1

New D.I.C Head
*

Joined: 8 Feb, 2007
Posts: 18


My Contributions
CODE
:huh:


my program is on functions using c. it suppose to perform 5 mathematical operations but i get 36 errors
here is what i wrote:
CODE

include<stdio.h>

int sum,sub,mult,div,mod,num1,num2;
int choice;




main()
{
            printf("please enter two integers /n");
            scanf("%d/n%d/n", & num1, & num2);

            menu()

                        scanf(choice);

                        if(choice==1)
                                    sum=addition("%d/n%d/n",&num1,&num2);
                        printf("sum is %d", sum);

                        else if(choice==2)
                                     sub=subtration("%d/n%d/n",&num1,&num2);
                        printf("sub is %d", sub);

                        if (num1>num2)

                        largest=num1;

                        else
                                    largest=num2;
                        prinf("largest is %d", largest);

                        else if (choice==3)
                         mult=multiplication("%d/n%d/n",&num1,&num2);
                        printf("mult is %d",mult);

                        else if (choice==4)
                        { if (num2==0)
                                     printf("division by zero is not allowed");
                        else { div=division("%d/n%d/n",&num1,&num2);
                        printf("div is %d", div);

                        if (num1>num2)
                        largest=num1;
                        else
                                    largest=num2;
                        printf("largest is %d", largest);
                        }

                        else if (choice==5)
                                    { if(num2==0)
                        printf("modulos by zero is not allowed");
                        else{ mod=modulos("%d/n%d/n",&num1,&num2);
                        printf("mod id %d", mod);
                        if (num1>num2)
                                    largest=num1;
                        else
                                    largest=num2;
                        printf("largest is %d", largest);
                        }

                        else if (choice==0)
                      printf("exiting now!!");

                                    return 0;
                        
                        }


                        function menu()

                        {
                                    printf("please enter 1 for addition");
            printf("please enter 2 for subtraction");
            printf("please enter 3 for multiplication");
                                    printf("please enter 4 for division");
                                    printf("please enter 5 for modulos");
                                    printf("please enter 0 for exit");

                        }

                        function addition(num1,num2);

                                    int a;

                        {
                                    a=num1+num2;

                                                return (a);

                        }

                        function subtraction (num1,num2);

                        int b;
                        {
                                    b=num1-num2;

                                                return (B);

                        }

                        function multiplication (num1,num2);

                        int c;
                        {
                                    c=num1*num2;

                                                return ©;

                        }

                        function division(num1,num2);

                                    int d;

                        {
                                    d=num1/num2;

                                                return (d);

                        }

                        function modulos(num1,num2);

                                    int e;

                        {
                                    e=num1%num2;

                                                return (e);

                        }


help please
User is offlineProfile CardPM
+Quote Post

skyhawk133
RE: C Program In Function
22 Feb, 2007 - 06:12 PM
Post #2

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 15,262



Thanked: 61 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
I find it funny that you were able to put the :huh: in [code] tags, but not your actual code.

Oh well, it won't stop us from helping you. Welcome to dream.in.code
User is offlineProfile CardPM
+Quote Post

Craige
RE: C Program In Function
22 Feb, 2007 - 06:21 PM
Post #3

D.I.C Head
**

Joined: 2 Nov, 2006
Posts: 63


My Contributions
Could you post the errors please. It's a lot easier to debug when we know what we're looking for.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: C Program In Function
22 Feb, 2007 - 06:24 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,306



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
It would be much easier to help you if you post the error messages you are currently getting.

On second thought I think I see where most of your problems are coming from. This is not valid syntax for a call to a function. You have many places in your code where you are making a call like this.
CODE

sum=addition("%d/n%d/n",&num1,&num2);

Should be like this:
CODE

sum=addition( num1, num2);


You will also need to fix your functions, since currently you are trying to return a value even though your functions have not been declared as having return values. Functions do not have a semi-colon at the end, the code for the function is just enclosed in {} brackets. In addition you do not include the word "function" in your function. Replace the word 'function' with the return type of your function, in this case 'int' is needed.

Incorrect function definition:
CODE

function addition(num1,num2);

Correct function name and parameter list:
CODE
int addition(num1,num2)
{
   //your functions code here
}


You also need to have prototypes defined before your main function that lets the compiler know what functions to expect while it is compiling.
CODE

include<stdio.h>

//Prototypes go here
int addition(num1,num2);
int subtraction (num1,num2);

int sum,sub,mult,div,mod,num1,num2;
int choice;

main()
{


Start by fixing all those problems then we can move on to the rest of the problems you have in your code.
User is offlineProfile CardPM
+Quote Post

pascal
RE: C Program In Function
22 Feb, 2007 - 06:26 PM
Post #5

New D.I.C Head
*

Joined: 8 Feb, 2007
Posts: 18


My Contributions
QUOTE(Craige @ 22 Feb, 2007 - 07:21 PM) *

Could you post the errors please. It's a lot easier to debug when we know what we're looking for.


ok th errors are:

C:\math.cpp(1) : error C2143: syntax error : missing ';' before '<'
C:\math.cpp(1) : error C2501: 'include' : missing storage-class or type specifiers
C:\math.cpp(1) : error C2143: syntax error : missing ';' before '<'
C:\math.cpp(11) : error C2065: 'printf' : undeclared identifier
C:\math.cpp(12) : error C2065: 'scanf' : undeclared identifier
C:\math.cpp(12) : error C2065: 'num1' : undeclared identifier
C:\math.cpp(12) : error C2065: 'num2' : undeclared identifier
C:\math.cpp(14) : error C2065: 'menu' : undeclared identifier
C:\math.cpp(16) : error C2146: syntax error : missing ';' before identifier 'scanf'
C:\math.cpp(19) : error C2065: 'sum' : undeclared identifier
C:\math.cpp(19) : error C2065: 'addition' : undeclared identifier
C:\math.cpp(22) : error C2181: illegal else without matching if
C:\math.cpp(23) : error C2065: 'sub' : undeclared identifier
C:\math.cpp(23) : error C2065: 'subtration' : undeclared identifier
C:\math.cpp(28) : error C2065: 'largest' : undeclared identifier
C:\math.cpp(32) : error C2065: 'prinf' : undeclared identifier
C:\math.cpp(34) : error C2181: illegal else without matching if
C:\math.cpp(35) : error C2065: 'mult' : undeclared identifier
C:\math.cpp(35) : error C2065: 'multiplication' : undeclared identifier
C:\math.cpp(38) : error C2181: illegal else without matching if
C:\math.cpp(41) : error C2065: 'div' : undeclared identifier
C:\math.cpp(41) : error C2065: 'division' : undeclared identifier
C:\math.cpp(51) : error C2181: illegal else without matching if
C:\math.cpp(54) : error C2065: 'mod' : undeclared identifier
C:\math.cpp(54) : error C2065: 'modulos' : undeclared identifier
C:\math.cpp(63) : error C2181: illegal else without matching if
C:\math.cpp(71) : error C2065: 'function' : undeclared identifier
C:\math.cpp(71) : error C2146: syntax error : missing ';' before identifier 'menu'
C:\math.cpp(73) : error C2143: syntax error : missing ';' before '{'
C:\math.cpp(83) : error C2146: syntax error : missing ';' before identifier 'addition'
C:\math.cpp(94) : error C2146: syntax error : missing ';' before identifier 'subtraction'
C:\math.cpp(94) : error C2065: 'subtraction' : undeclared identifier
C:\math.cpp(104) : error C2146: syntax error : missing ';' before identifier 'multiplication'
C:\math.cpp(114) : error C2146: syntax error : missing ';' before identifier 'division'
C:\math.cpp(125) : error C2146: syntax error : missing ';' before identifier 'modulos'
C:\math.cpp(135) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

math.obj - 36 error(s), 0 warning(s)
User is offlineProfile CardPM
+Quote Post

pascal
RE: C Program In Function
22 Feb, 2007 - 06:47 PM
Post #6

New D.I.C Head
*

Joined: 8 Feb, 2007
Posts: 18


My Contributions
[still errors]


i have 30 errors now as change it up a bit this is the program:
CODE

#include<stdio.h>

int addition(num1,num2);
int subtraction(num1,num2);
int multiplication (num1,num2);
int division(num1,num2);
int modulos(num1,num2);


int sum,sub,mult,div,mod,largest,num1,num2;
int choice;




main()
{
            printf("please enter two integers /n");
            scanf("%d/n%d/n", & num1, & num2);

            menu()

                        scanf(choice);

                        if(choice==1)
                                    sum=addition("%d/n%d/n",&num1,&num2);
                        printf("sum is %d", sum);

                        else if(choice==2)
                                     sub=subtraction("%d/n%d/n",&num1,&num2);
                        printf("sub is %d", sub);

                        if (num1>num2)

                        largest=num1;

                        else
                                    largest=num2;
                        prinf("largest is %d", largest);

                        else if (choice==3)
                         mult=multiplication("%d/n%d/n",&num1,&num2);
                        printf("mult is %d",mult);

                        else if (choice==4)
                        { if (num2==0)
                                     printf("division by zero is not allowed");
                        else { div=division("%d/n%d/n",&num1,&num2);
                        printf("div is %d", div);

                        if (num1>num2)
                        largest=num1;
                        else
                                    largest=num2;
                        printf("largest is %d", largest);
                        }

                        else if (choice==5)
                                    { if(num2==0)
                        printf("modulos by zero is not allowed");
                        else{ mod=modulos("%d/n%d/n",&num1,&num2);
                        printf("mod id %d", mod);
                        if (num1>num2)
                                    largest=num1;
                        else
                                    largest=num2;
                        printf("largest is %d", largest);
                        }

                        else if (choice==0)
                      printf("exiting now!!");

                                    return 0;
                        
                        }


                        function menu()

                        {
                                    printf("please enter 1 for addition");
                                    printf("please enter 2 for subtraction");
                                    printf("please enter 3 for multiplication");
                                    printf("please enter 4 for division");
                                    printf("please enter 5 for modulos");
                                    printf("please enter 0 for exit");

                        }

                        int addition(num1,num2)

                                    int a;

                        {
                                    a=num1+num2;

                                                return (a);

                        }

                        int subtraction (num1,num2)

                        int b;
                        {
                                    b=num1-num2;

                                                return (B);

                        }

                        int multiplication (num1,num2)

                        int c;
                        {
                                    c=num1*num2;

                                                return ©;

                        }

                        int division(num1,num2)

                                    int d;

                        {
                                    d=num1/num2;

                                                return (d);

                        }

                        int modulos(num1,num2)

                                    int e;

                        {
                                    e=num1%num2;

                                                return (e);

                        }

and these are the errors:
C:\math.cpp(3) : error C2065: 'num1' : undeclared identifier
C:\math.cpp(3) : error C2065: 'num2' : undeclared identifier
C:\math.cpp(3) : error C2078: too many initializers
C:\math.cpp(4) : error C2078: too many initializers
C:\math.cpp(5) : error C2078: too many initializers
C:\math.cpp(6) : error C2078: too many initializers
C:\math.cpp(7) : error C2078: too many initializers
C:\math.cpp(21) : error C2065: 'menu' : undeclared identifier
C:\math.cpp(23) : error C2146: syntax error : missing ';' before identifier 'scanf'
C:\math.cpp(23) : error C2664: 'scanf' : cannot convert parameter 1 from 'int' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\math.cpp(29) : error C2181: illegal else without matching if
C:\math.cpp(39) : error C2065: 'prinf' : undeclared identifier
C:\math.cpp(41) : error C2181: illegal else without matching if
C:\math.cpp(45) : error C2181: illegal else without matching if
C:\math.cpp(58) : error C2181: illegal else without matching if
C:\math.cpp(70) : error C2181: illegal else without matching if
C:\math.cpp(78) : error C2065: 'function' : undeclared identifier
C:\math.cpp(78) : error C2146: syntax error : missing ';' before identifier 'menu'
C:\math.cpp(80) : error C2143: syntax error : missing ';' before '{'
C:\math.cpp(92) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
C:\math.cpp(95) : error C2065: 'a' : undeclared identifier
C:\math.cpp(103) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
C:\math.cpp(105) : error C2065: 'b' : undeclared identifier
C:\math.cpp(113) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
C:\math.cpp(115) : error C2065: 'c' : undeclared identifier
C:\math.cpp(123) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
C:\math.cpp(126) : error C2065: 'd' : undeclared identifier
C:\math.cpp(134) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
C:\math.cpp(137) : error C2065: 'e' : undeclared identifier
C:\math.cpp(142) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

math.obj - 30 error(s), 0 warning(s)



~Admin Edit: Added [code] tags
User is offlineProfile CardPM
+Quote Post

bestbat
RE: C Program In Function
22 Feb, 2007 - 06:51 PM
Post #7

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 32


My Contributions
You should also check your spelling, one error shows printf has been spelled incorrectly,
the other spelling errors appear to have been corrected in your later version.

Also check your if statements.

This post has been edited by bestbat: 22 Feb, 2007 - 06:54 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: C Program In Function
22 Feb, 2007 - 07:09 PM
Post #8

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,306



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Sorry I forgot to mention that you need to include the data type that is being passed into the function as well.
CODE

int addition(int num1,int num2);
int subtraction(int num1,int num2);
int multiplication (int num1,int num2);
int division(int num1,int num2);
int modulos(int num1,int num2);



Don't forget to fix that same problem in the function declaration themselves, too.

REFER to my previous post about fixing the function calls as well, in addition the the other problems I noted. They must match the function declarations.
User is offlineProfile CardPM
+Quote Post

pascal
RE: C Program In Function
22 Feb, 2007 - 07:16 PM
Post #9

New D.I.C Head
*

Joined: 8 Feb, 2007
Posts: 18


My Contributions
[25 errors]

the program now:
CODE

#include<stdio.h>

int addition(num1,num2);
int subtraction(num1,num2);
int multiplication (num1,num2);
int division(num1,num2);
int modulos(num1,num2);


int sum,sub,mult,div,mod,largest,num1,num2;
int choice;




main()
{
            printf("please enter two integers /n");
            scanf("%d/n%d/n", & num1, & num2);

            menu()

                        scanf(choice);

            if(choice==1){
                                    sum=addition(num1,num2);
                        printf("sum is %d", sum);

                        
            }else{
                
                if(choice==2){
                                     sub=subtraction(num1,num2);
                        printf("sub is %d", sub);

                        if (num1>num2)

                        largest=num1;

                        else
                                    largest=num2;
                        printf("largest is %d", largest);

                }else{
                    if (choice==3){
                         mult=multiplication(num1,num2);
                        printf("mult is %d",mult);
                    } else{
                        
                        if (choice==4)
                        { if (num2==0)
                                     printf("division by zero is not allowed");
                        else { div=division(num1,num2);
                        printf("div is %d", div);

                        if (num1>num2)
                        largest=num1;
                        else
                                    largest=num2;
                        printf("largest is %d", largest);
                        }

                        }else{
                            if (choice==5){
                                    { if(num2==0)
                        printf("modulos by zero is not allowed");
                        else{ mod=modulos(num1,num2);
                        printf("mod id %d", mod);
                        if (num1>num2)
                                    largest=num1;
                        else
                                    largest=num2;
                        printf("largest is %d", largest);
                        }

                                    }else {

                                        if (choice==0){
                      printf("exiting now!!");

                                    return 0;
                        
                        
                                        }
                                    }
                            }
                        }
                    }


                        function menu()

                        {
                                    printf("please enter 1 for addition");
                                    printf("please enter 2 for subtraction");
                                    printf("please enter 3 for multiplication");
                                    printf("please enter 4 for division");
                                    printf("please enter 5 for modulos");
                                    printf("please enter 0 for exit");

                        }

                        int addition(num1,num2)

                                    int a;

                        {
                                    a=num1+num2;

                                                return (a);

                        }

                        int subtraction (num1,num2)

                        int b;
                        {
                                    b=num1-num2;

                                                return (B);

                        }

                        int multiplication (num1,num2)

                        int c;
                        {
                                    c=num1*num2;

                                                return ©;

                        }

                        int division(num1,num2)

                                    int d;

                        {
                                    d=num1/num2;

                                                return (d);

                        }

                        int modulos(num1,num2)

                                    int e;

                        {
                                    e=num1%num2;

                                                return (e);

                        }


the errors are:

C:\math.cpp(3) : error C2065: 'num1' : undeclared identifier
C:\math.cpp(3) : error C2065: 'num2' : undeclared identifier
C:\math.cpp(3) : error C2078: too many initializers
C:\math.cpp(4) : error C2078: too many initializers
C:\math.cpp(5) : error C2078: too many initializers
C:\math.cpp(6) : error C2078: too many initializers
C:\math.cpp(7) : error C2078: too many initializers
C:\math.cpp(21) : error C2065: 'menu' : undeclared identifier
C:\math.cpp(23) : error C2146: syntax error : missing ';' before identifier 'scanf'
C:\math.cpp(23) : error C2664: 'scanf' : cannot convert parameter 1 from 'int' to 'const char *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\math.cpp(76) : error C2181: illegal else without matching if
C:\math.cpp(91) : error C2065: 'function' : undeclared identifier
C:\math.cpp(91) : error C2146: syntax error : missing ';' before identifier 'menu'
C:\math.cpp(93) : error C2143: syntax error : missing ';' before '{'
C:\math.cpp(105) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
C:\math.cpp(108) : error C2065: 'a' : undeclared identifier
C:\math.cpp(116) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
C:\math.cpp(118) : error C2065: 'b' : undeclared identifier
C:\math.cpp(126) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
C:\math.cpp(128) : error C2065: 'c' : undeclared identifier
C:\math.cpp(136) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
C:\math.cpp(139) : error C2065: 'd' : undeclared identifier
C:\math.cpp(147) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
C:\math.cpp(150) : error C2065: 'e' : undeclared identifier
C:\math.cpp(155) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

math.obj - 25 error(s), 0 warning(s)
User is offlineProfile CardPM
+