Thread: Splitting a program in 3 files

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    3

    Splitting a program in 3 files

    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

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I don't know VS, so this is only a guess, but have you tried running cl with the filenames of both .c files?

    cl main.c Functions.c

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    3
    Quote Originally Posted by Matticus View Post
    I don't know VS, so this is only a guess, but have you tried running cl with the filenames of both .c files?

    cl main.c Functions.c
    It has worked! DAMMIT!

    Now a new problem arises.. i changed all float to int now, and the calculator works just like how i expected. In my assignment, my mentor had asked us to use extern key word. I just need to know, if that is applicable here? meaning, can i user the extern key work 'somehow' or 'anyhow' here?

    Thank you for the help

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Well, I suppose you could declare a global variable for the result in "main.c", and extern it to "Functions.c", and have those math functions update that variable instead of returning their result.

    I understand that the exercise is to learn the "extern" keyword, but be aware that actually implementing it that way is considered poor form. Your current implementation is better form - returning variables from the math functions. But as I said, it's probably just to get exposure to "extern".

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    3
    I get it.. Thank you very VERY much! Have a nice day!

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're very welcome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Splitting template classes between .h and .cpp files
    By Neo1 in forum C++ Programming
    Replies: 12
    Last Post: 01-16-2010, 05:47 AM
  2. splitting up files
    By pastitprogram in forum C++ Programming
    Replies: 1
    Last Post: 01-27-2009, 07:33 AM
  3. Splitting Code Into Multiple Files
    By pobri19 in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2008, 04:21 AM
  4. Splitting source code files
    By rocksteady in forum C Programming
    Replies: 2
    Last Post: 10-19-2007, 02:01 PM
  5. Problem splitting program into different files
    By kzar in forum C Programming
    Replies: 8
    Last Post: 09-19-2005, 06:03 AM

Tags for this Thread