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.