Reputation: 3
I wanna program a simple calculator. The calculations are in functions. My problem is, the result is always 0. :( What is wrong? Look at my code please:
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
double addition(double a,double c);
double subtraktion(double a, double c);
double multiplikation(double a, double c);
double division(double a, double c);
int main()
{
double z=0, a, c;
char b;
printf("Insert your Numbers to calculate!");
scanf_s("%lf", &a);
scanf_s("%c", &b);
scanf_s("%lf", &c);
if (b == '+')
{
addition(a, c);
}
if (b == '-')
{
subtraktion(a, c);
}
if (b == '*')
{
multiplikation(a, c);
}
if (b == '/')
{
division(a, c);
}
printf("Result: %lf", z);
system("pause");
}
double addition(double a, double c)
{
double z;
z = a + c;
return(z);
}
double subtraktion(double a, double c)
{
double z;
z = a - c;
return(z);
}
double multiplikation(double a, double c)
{
double z;
z = a*c;
return(z);
}
double division(double a, double c)
{
double z;
z = a / c;
return(z);
}
I program with Visual Studio 2013. I tried to debug the Program but this don´t work. Please forgive me my bad English.
Upvotes: 0
Views: 1279
Reputation: 311
I tried this code and it works properly:
#include <stdlib.h>
#include <stdio.h>
double addition(double a,double c);
double subtraktion(double a, double c);
double multiplikation(double a, double c);
double division(double a, double c);
int main()
{
double z=0, a, c;
char b;
printf("Insert your Numbers to calculate!");
scanf("%lf", &a);
scanf("%c", &b);
scanf("%lf", &c);
if (b == '+')
{
z = addition(a, c);
}
if (b == '-')
{
z = subtraktion(a, c);
}
if (b == '*')
{
z = multiplikation(a, c);
}
if (b == '/')
{
z = division(a, c);
}
printf("Result: %lf\n", z);
}
double addition(double a, double c)
{
double z;
z = a + c;
return(z);
}
double subtraktion(double a, double c)
{
double z;
z = a - c;
return(z);
}
double multiplikation(double a, double c)
{
double z;
z = a*c;
return(z);
}
double division(double a, double c)
{
double z;
z = a / c;
return(z);
}
Compile and then in input type: 3+2 [Press enter] and it shows the exact result!
P.S. I deleted the 'custom' header and then used the simple scanf function. I deleted the system("pause") too because I'm not under Visual Studio. (so you may need that)
So what I've done is:
- Deleted your custom header #include "stdafx.h"
- Edited the scanf_s to scanf(...)
- Added z = functions()
- printf(...\n) // not necessary
And it works.
Upvotes: 2
Reputation: 2647
Nowhere are you assigning the results of the operation to z
. Instead of:
if (b == '+')
{
addition(a, c);
}
You should do
if (b == '+')
{
z = addition(a, c);
}
And likewise for all the other cases
Upvotes: 4