4 Replies - 33189 Views - Last Post: 25 September 2008 - 02:58 AM Rate Topic: -----

#1 mariji  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 24-September 08

CALCULATOR PROGRAM USING FUNCTIONS!

Posted 24 September 2008 - 05:11 PM

We are asked to make a calculator program using C that begins with like this:

first sample output:

=================================

Welcome to the calculator program

1- Addition
2- Subtraction
3- Multiplication
4- Division
5- Power
6- Exit


What operation would you like to perform? _

=================================


then.. after choosing the operation, the screen will be cleared.
If the person choose 1 (addition), a note will appear.



second sample output:

=================================
You've choosen Addition!

How many numbers do you like to add?_ // assuming that you type 3

Please enter 3 numbers:
_
_
_

The sum is: _

=================================


so here's my sample code for addition and subtraction only using FUNCTIONS but it wont work!!! T_T

It has many errors..! T_T

and i dont know how to use FUNCTIONS!

what's wrong with this code?

#include<stdio.h>

int add(no);
int sub(no);


main()

{
int op, result;

printf(" 1- addition");
printf(" 2- subtraction");
printf("\n\n");
printf("What operation would you like to use? "); scanf("%d", op);

switch(op)
{
case 1: result=add(no);
	printf("Sum:%d", result);
	break;
case 2: result=sub(no);
	printf("Difference:%d", result);
	break;
}


int add(no)
{
int no, num, knt, sum=0;

printf("You've choosen ADDITION!");
printf("How many integers do you like to use?"); scanf("%d", &no);
printf("\n\n");
printf("Please enter %d integers: \n");

for(knt=1; knt<=no; knt++)
{
printf("\t\t");
scanf("%d", &num);
sum=sum+num;
}
}

int sub(no)
{
int no, num, knt, diff=0;

printf("You've choosen SUBTRACTION!");
printf("How many integers do you like to use?"); scanf("%d", &no);
printf("\n\n");
printf("Please enter %d integers: \n");

for(knt=1; knt<=no; knt++)
{
printf("\t\t");
scanf("%d", &num);
diff=diff-num;
}
}

return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: CALCULATOR PROGRAM USING FUNCTIONS!

#2 csmanoj  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 150
  • Joined: 06-August 07

Re: CALCULATOR PROGRAM USING FUNCTIONS!

Posted 25 September 2008 - 01:15 AM

Remove 'no' from those brackets. Don't write add(no) just write add() because you don't pass any parameters to the add function. Even if you passed parameters you have to use the parameter type too, in the function declaration. Similarly for subtract() too.

Don't write just 'main()'. Write 'int main()'. You must specify the return type.

And both add() and subtract() must return the results using 'return' statement.

You have put the function definitions of add() and subtract() inside the main() function definition. Move them out of it. (i.e., move the last two lines to the line after the switch block.

Also when you're doing scanf() after the menu, you left the '&' symbols.

The printf() statements that ask you to enter the numbers is incomplete.

And always initialize variables before you use them.

I'd advise you study again about C/C++ functions from a good book.

(That's only about the syntax. The semantics of the subtract function is a bomb.)
Was This Post Helpful? 0
  • +
  • -

#3 Soura  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 24-September 08

Re: CALCULATOR PROGRAM USING FUNCTIONS!

Posted 25 September 2008 - 02:51 AM

[quote name='mariji' date='24 Sep, 2008 - 05:11 PM' post='423450']
The program is(without power func.)-
#include<stdio.h>
#include<conio.h>
#include<iostream.h>

main()
{
      int a, b, r, n;
      printf ("MENU: \n Press '1' For ''Addition'' \n Press '2' For ''Substraction''\n Press '3' For ''Multiplication''\n Press '4' For ''divition'' \n ");
      printf("Enter your Option=");
      scanf("%d", &n);
      if(n==1 || n==2 || n==3 || n==4)
      {
              printf(" Enter the first no. ");
              scanf("%d", &a);
              printf(" Enter the second no. ");
              scanf("%d", &B)/>;
              switch(n)
              {
                       case 1:
                            r=a+b;
                            break;
                       case 2:
                            r=a-b;
                            break;
                       case 3:
                            r=a*b;
                            break;
                       case 4:
                            r=a/b;
              }             
       printf (" The Result is = %d", r);
               
      }         
      else
      {
           printf ("Your CHOICE is WRONG, Please Enter the RIGHT CHOICE.\n Press any key to Exit.");   
      }
       
       getch();
}
/*end of main*/


Please try this.
Was This Post Helpful? 0
  • +
  • -

#4 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: CALCULATOR PROGRAM USING FUNCTIONS!

Posted 25 September 2008 - 02:55 AM

Why doesn't anyone use the code tags nowadays?

:code:
Please... :(
Was This Post Helpful? 0
  • +
  • -

#5 Soura  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 24-September 08

Re: CALCULATOR PROGRAM USING FUNCTIONS!

Posted 25 September 2008 - 02:58 AM

View Postgabehabe, on 25 Sep, 2008 - 02:55 AM, said:

Why doesn't anyone use the code tags nowadays?

:code:
Please... :(

OK.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1