Hello there!
I am new to C programming, so need you help with this. I have been given an assignment to create a simple calculator by splitting the program in 3 files. I was absent in this class so I am not sure how to go about doing this. It should have 2 .c files and 1 .h... I went through the internet extensively and could only come up with this. Please help me with the problem.
main.c:
Code:
//Calculator main.c
#include <stdio.h>
#include <conio.h>
#include "Functions.h"
int main()
{
float x = 0, y = 0;
int operation;
char answer_calc = 'null';
extern float a, b;
do
{
printf("This is the main function!\n");
printf("Enter the first number: ");
scanf("%f", &x);
printf("Enter the second number: ");
scanf("%f", &y);
printf("What do you want to perform?");
printf("\n1. Addition");
printf("\n2. Subtraction");
printf("\n3. Division");
printf("\n4. Multiplication");
printf("\n!. Exit");
printf("\nSelect the corresponding number only: ");
scanf(" %c", &operation);
if(operation == '1')
{
printf("The sum of the two numbers is: %d", addition(x, y));
}
else if(operation == '2')
{
printf("The difference of the two numbers is: %d", difference(x, y));
}
else if(operation == '3')
{
printf("The remainder of the two numbers is: %d", remainder(x, y));
}
else if(operation == '4')
{
printf("The product of the two numbers is: %d", product(x, y));
}
else if(operation == '!')
{
exit(0);
}
else
{
printf("Invalid option");
}
printf("\nDo you want to continue? (Y/N)");
scanf(" %c", &answer_calc);
} while ((answer_calc == 'y') || (answer_calc == 'Y'));
return 0;
}
Functions.c
Code:
#include "Functions.h"
extern float x, y;
float addition (float a, float b)
{
return a + b;
}
float difference (float a, float b)
{
return a - b;
}
float remainder (float a, float b)
{
int rem;
if (b == 0)
{
printf ("Cannot divide by 0 (ZERO)");
}
else
{
rem = a/b;
}
return rem;
}
float product (float a, float b)
{
return a*b;
}
Functions.h
Code:
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
float Sum(float a, float b);
float difference (float a, float b);
float remainder (float a, float b);
float product (float a, float b);
#endif
When I do a 'cl main.c' on the Developer Command window for VS2013, i get an error that reads :
main.obj
main.obj : error LNK2019: unresolved external symbol _difference referenced in f
unction _main
main.obj : error LNK2019: unresolved external symbol _product referenced in func
tion _main
main.obj : error LNK2019: unresolved external symbol _addition referenced in fun
ction _main
main.exe : fatal error LNK1120: 3 unresolved externals