Hi all,

I'm posting about an error I guess is pretty common, and I've done a Google and forum search before posting, and have spent ages trying to sort this myself. Just thought I'd clear that!

I have a program program that runs off the terminal, adds two numbers together and returns the result back to the terminal. There are two source files and a header file- twovars.c (this contains the main() function), string2int.c and string2int.h. I am using a makefile. I have got the program running fine, but am trying to get the code that returns the result in it's own function, by adding it into it's own source file.

I'll start with the working program.

twovars.c:

#include<stdio.h>
int main(int argc, char *argv[])
{

        int i, result;
        int a[3] = {0, 0, 0};

        if( argc == 1)
        {
                printf("Usage: %s number number ...\n", argv[0]);
                return;
        }

        if( argc > 3)
        {
                printf("Usage: two variables off the command line only!\n");
                return;
        }

        for( i = 1 ; i < argc ; i++)
        {
                a[i] = string_to_integer(argv[i]);
        }

        result = a[i - 2] + a[i - 1];
        printf("Result of adding %d and %d is: %d\n", a[i - 2], a[i - 1], result);

return 0;

} /* End of main() */

string2int.c:

#include<stdio.h>
#include"string2int.h"
int string_to_integer(char string[])
{

        int j, integer_value, result = 0;

        for( j = 0 ; string[j] >= '0' && string[j] <= '9'; j++)
        {
                integer_value = string[j] - '0';
                result = result * 10 + integer_value;
        }

        return result;

} /* End of string_to_integer() */

string2int.h:

#ifndef _STRING2INT
#define _STRING2INT

int string_to_integer(char []);

#endif

makefile:

stringconverter : string2int.o twovars.o
        cc -o stringconverter string2int.o twovars.o
string2int.o : string2int.c string2int.h
        cc -c string2int.c
twovars.o : twovars.c
        cc -c twovars.c

So far this code successfully compiles and runs.

Now, I'm trying to create a source file called returnresult.c, and editing the makefile so it's:

stringconverter : string2int.o twovars.o returnresult.o
cc -o stringconverter string2int.o twovars.o returnresult.o
string2int.o : string2int.c string2int.h
cc -c string2int.c
twovars.o : twovars.c
cc -c twovars.c
returnresult.o : returnresult.c
cc -c returnresult.c

Now I'm trying to edit the returnresult.c so that the action of displaying the result is in this function and not in twovars.c. However, I've been getting errors, and have been working from the ground up to try and find out what it is. I have left returnresult.c as literally just:

#include<stdio.h>

{
}

To test the makefile... but I get the error "expected identifier or '(' before '{' token" when compiling.

I'm not a programmer, so I'm struggling here. Can anyone give me some pointers? I hope this doesn't seem too rediculous.

Thanks.

You need at least an

[B]int main(void)[/B]
{
   return 0; /*else compiler complains about falling off the end */
}

Probably can trim out some of that, but this is a safe bet

Edited 5 Years Ago by jonsca: n/a

I'm assuming that code should be in returnresult.c, however, wouldn't the program error out having two mains?

You can compile a completely empty source file, or one that only has preprocessor statements like #include, #define, etc. You will possibly get a warning, but it shouldn't be fatal. You are correct in that two main() functions would not be viable. Even c++ which allows function overloading (two functions with same name, but different signatures) doesn't allow two main() functions - I just tested this on my system.

That said, there are a lot of nasty little bugs in this program, including the string_to_integer() function. You might want to read it through again carefully, especially with regard to array access/indexing in main().

You are correct in that two main() functions would not be viable.

Yes, sorry, I misunderstood, I thought you were trying to get the one file bootstrapped, built a reduced makefile out of it, and then continue on with the other portion later.

This article has been dead for over six months. Start a new discussion instead.