-
Silly error
Ok im writing a little program to get practice, its going to be an email whitelist thing, you can either add a email to the list or check if a email is in the list. Anyway im testing out what ive written so far and im getting an errror " [Linker error] undefined reference to `addemail' ". Cant see what ive done wrong anyway, heres my code.
Code:
#include <stdio.h>
void addemail();
main()
{
int choice;
printf("Email whitelist");
printf("(a) to add email, (c) to check email against list, (e) to exit.\n");
printf("Choice: ");
choice = -1;
while (choice == -1)
{
choice=getchar();
}
switch (choice)
{
case 'a':
addemail();
break;
case 'c':
/* checkemail(); */
break;
case 'e':
break;
default:
printf("Invalid Choice.");
break;
}
void addemail();
{
char buf[40];
FILE *fp;
printf("Enter email: ");
fgets(buf, sizeof(buf), stdin);
fp = fopen("emails","a+");
fputs(buf,fp);
fclose(fp);
printf("\nEmail address %s added.",buf);
}
}
-
You are defining your addemail function inside of your main function. ISO C doesn't support nested function and your compiler should say that if you have all warnings turned on. You have to end your main function before defining your addemail function. Also, use int main() and return an exit status
Code:
#include <stdio.h>
void addemail();
int main()
{
int choice;
printf("Email whitelist");
printf("(a) to add email, (c) to check email against list, (e) to exit.\n");
printf("Choice: ");
choice = -1;
while (choice == -1)
{
choice=getchar();
}
switch (choice)
{
case 'a':
addemail();
break;
case 'c':
/* checkemail(); */
break;
case 'e':
break;
default:
printf("Invalid Choice.");
break;
}
return 0;
}
void addemail()
{
char buf[40];
FILE *fp;
printf("Enter email: ");
fgets(buf, sizeof(buf), stdin);
fp = fopen("emails","a+");
fputs(buf,fp);
fclose(fp);
printf("\nEmail address %s added.",buf);
}
You can also check the return values of the functions you use, such as fopen, fgets, etc. We have a faq that might have some useful entries to you: http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
-
1. C does not support nested functions
2. You haven't written a function called addemail(), though you have managed to prototype it TWICE (check the semicolons)
-
Functions cannot be defined inside of other functions. Take your addemail function out of main and paste it after main (remembering to remove the semicolon).
Code:
#include <stdio.h>
void addemail(void);
int main(void)
{
int choice;
printf("Email whitelist");
printf("(a) to add email, (c) to check email against list, (e) to exit.\n");
printf("Choice: ");
choice = -1;
while (choice == -1)
{
choice=getchar();
}
switch (choice)
{
case 'a':
addemail();
break;
case 'c':
/* checkemail(); */
break;
case 'e':
break;
default:
printf("Invalid Choice.");
break;
}
return 0;
}
void addemail(void)
{
char buf[40];
FILE *fp;
printf("Enter email: ");
fgets(buf, sizeof(buf), stdin);
fp = fopen("emails","a+");
fputs(buf,fp);
fclose(fp);
printf("\nEmail address %s added.",buf);
}
>while (choice == -1)
EOF isn't guaranteed to be -1, use the EOF macro instead.
-
balls, didnt notice that lol, cant believe i closed main AFTER my function and didnt even notice!
-
I told it to print out the choice, and it does say a if you put a in, but it still says invalid choice. Not sure what ive done wrong
Code:
#include <stdio.h>
void addemail();
main()
{
int choice;
printf("Email whitelist");
printf("(a) to add email, (c) to check email against list, (e) to exit.\n");
printf("Choice: ");
scanf("%c",&choice);
printf("%c",choice);
switch (choice)
{
case 'a':
addemail();
break;
case 'c':
/* checkemail(); */
break;
case 'e':
break;
default:
printf("Invalid Choice.");
break;
}
}
void addemail()
{
char buf[40];
FILE *fp;
printf("Enter email: ");
fgets(buf, sizeof(buf), stdin);
fp = fopen("emails","a+");
fputs(buf,fp);
fclose(fp);
printf("\nEmail address %s added.",buf);
}
-
> scanf("%c",&choice);
%c means char, and choice is an int
You're also using fgets elsewhere, so its going to mess up eventually. Just stick to fgets for everything
Code:
int main() /* be specific about the return type */
{
char choice[20];
printf("Email whitelist");
printf("(a) to add email, (c) to check email against list, (e) to exit.\n");
printf("Choice: ");
fgets( choice, sizeof choice, stdin );
switch (choice[0])
-
the fgets idea works nicely thanks. I thought it was better to use int for single characters, i cant remember where i read it though.