1.

Solve : Need C Programming Help! (tinker with program)?

Answer»

So, to begin, I must write a program that I can type in a number of inches and it will list how many miles, yards, feet and inches I entered. With this program I keep getting the error:

[Linker error] undefined reference to `measure(int, int, int, int)'

Any ideas on how I can make this work?



#include
#include
#include

int MAIN()
{

int measure(int B, int C, int D, int E);
int miles, yards, feet, inches, B, C, D, E;

miles==B, yards==C, feet==D, inches==E;

int total=0;

printf("Please enter a number of inches\n");

scanf("%d", total);

measure(B, C, D, E);
 
if (total>=63360)
B= (total/63360);

if (total>=1760)
C= (total-(B*63360))/1760;

if (total>=5280)
D= ((total- (B*63360)-(C*1760))/5280);

if (total>=12)
E= ((total-(B*63360)-(C*1760)-(D*5280))/12);
 
printf("Total inches entered: %d\n", total);
printf("%d miles\n", miles);
printf("%d yards\n", yards);
printf("%d feet\n", feet);
printf("%d inches\n", inches);

system ("PAUSE");
RETURN 0;
}
 you don't define the measure() routine anywhere.

Probably best to keep things in the Forum thread

To Elaborate:

There is no measure function. The only procedure you define is the main() method.
Within main, you do use this line:

Code: [Select]int measure(int B, int C, int D, int E);
But from what I understand this is a No-op statement- it basically defines a function pointer type and then does nothing with it so it get's discarded.

As a result when you refer to measure() later on, the compiler doesn't know what you are talking about.


here is a version that compiles. You will need to move appropriate logic. I also took the liberty of moving your System call into a separate routine and using stdio to create the same effect. A good rule of thumb when programming in C and C++ is to never use System() unless your aim is to purposely start a separate process. If you are trying to create a desired effect (such as pausing for enter or any key being pressed) you should create that functionality yourself. Too often I see self-proclaimed C/C++ programmers (not yourself) claiming multiple years of experience when all they are doing is writing a glorified batch file consisting of a series of system() calls.

Anyway, here is the version that compiles and appears to work. I've placed comments to try to describe certain key parts when it comes to C as I understand them. You should do some research on that which I bring up on your own to either confirm or refute the accuracy. I re-made the measure routine based on what I suspected it was supposed to be doing. the function requires some pointer usage which I try to explain as best I can.

Code: [Select]#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#ifdef _MSC_VER
#define getch() _getch()
#endif

//Since I used Visual Studio to test it, some of the above is probably MS compiler specific.
//Forward function declarations. in order for the C compiler to be able to parse and understand
//function calls, it needs to know what functions are available. the included header files above add function definitions for the appropriate functions and types
//and whatnot within the STANDARD library, but for your own code, you either need to create the actual function definitions before you use them, or use these,
//which are forward declarations which tell the Compiler that by the time it goes to actually link everything it will know what the function definition is (it is
//defined beneath main, which is where the function is used).
//I didn't know what measure() was supposed to do, so it has been removed. if part of the main() function was supposed to be there,
//I added the "pause()" function which performs a similar function to system("pause") but actually does it using C code so you don't spawn off a separate
//command INTERPRETER and also tie yourself to a specific set of Operating Systems.
void measure(int total, int* miles, int* yards, int* feet, int* inches);
void pause(void);
int main()
{
    int miles=0, yards=0, feet=0, inches=0;
    int total=0;
    printf("Please enter a number of inches\n");
//the original scanf() you had would have raised an access violation.
//scanf takes INPUT and puts it into a parameter. In order to do this, the second parameter
//is actually a pointer. If you do not pass it as an address, the result is that the scanf() function interprets
// the value of total passed in literally; it's 0 by default, so you end up with an access violation when scanf tries to save
//the value you type at address 0.
    scanf("%d", &total);
   
    //I moved the logic you had here previous to the "measure()" routine, since I think that was the intention.
//note that each parameter is passed by  address. Since measure is storing values in them, it needs their address; parameters are copied normally, (by value)
//so assigning to them has no effect.
//so
    measure(total,&miles,&yards,&feet,&inches);

    //measure should have done all the calculations for us here, and we can display the results.

    printf("Total inches entered: %d\n", total);
    printf("%d miles\n", miles);
    printf("%d yards\n", yards);
    printf("%d feet\n", feet);
    printf("%d inches\n", inches);
    pause();
    return 0;
}
//this is the trickiest bit to explain since it involves the use of pointers.
//as we can see, the parameter list defines all but the first parameter as a pointer.
//the reason is because the measure() routine is storing it's output in these values, and in order to do so
//it will need a pointer to the location to store them.
void measure(int total,int* miles, int* yards, int* feet, int* inches)
{
//there is a pattern here worth explaining.
//when dealing with a pointer, if you assign it directly, you assign the memory address the pointer points to. So,
//if we were to use "miles= (total/63360)" we would be changing the pointer itself to point at a completely different address.
//in that case the result would be that nothing would happen; the miles parameter pointer is actually a copy of the passed in value,
//so changing that would have no effect on the variable passed in from main.

//In order to actually get at the value the pointer is pointing at and use it, we need to "dereference" the pointer. Here we use that
//several times; the assignments all use *variable to dereference the pointer because we want to store the calculation in the memory the
//pointer is pointing at, and not the pointer itself. Likewise, the calculations refer to the results of the previous calculations too,
//so we dereference the previous results when they are used in the calculation (otherwise, the calculation would use the memory location
//of the pointer, which is not what we want.)
    if (total>=63360)
        *miles = (total/63360);

    if (total>=1760)
        *yards = (total-((*miles)*63360))/1760;

    if (total>=5280)
        *feet = ((total- ((*miles)*63360)-((*yards)*1760))/5280);

    if (total>=12)
        *inches = ((total-((*miles)*63360)-((*yards)*1760)-((*feet)*5280))/12);


}
void pause(void)
{
printf("Press Enter to Continue...");

    fflush(stdout);
    while ( getch() == 0 );

}



Discussion

No Comment Found