-
Calling to function
Example:
Code:
1 printf ("Enter name: \n");
2 scanf ("%s", &name);
3 printf ("Enter phone: \n");
4 scanf ("%d", &phone);
5 add_field(name,phone);
6 ....
7 add_field(a,b)
8
9 char a;
10 char b;
11
12 {
13 printf ("%s - name\n", name);
14 printf ("%d - phone\n", phone);
15 }
When I try to compile this...
Error 5: Extra parameter in call to add_field()
How can I correct it?
-
Without seeing all of the code I would have to say that add_field doesn't take 2 parameters as you have passed to it. Look at the decleration of add_field to determine the parameters.
-
full code
Code:
#include "stdio.h"
char name;
char phone;
int main()
{
int choice; /* promenliva za izbor */
int clear; /* izchistva <CR> */
float add_field();
printf ("Menu: \n");
printf ("Add field (1)\n");
printf ("Del field (2)\n");
printf ("Update field (3)\n");
printf ("View all fields (4)\n");
printf ("Exit (5)\n");
scanf ("%d", &choice);
switch(choice)
{
case 1 :
printf ("Enter name: \n");
scanf ("%s", &name);
printf ("Enter phone: \n");
scanf ("%d", &phone);
add_field(name,phone);
break;
case 2 :
printf ("asd");
break;
case 3 :
printf ("asd");
break;
case 4 :
printf ("asd");
break;
default :
printf("Ok. Thanks for using me. Goodbye");
return 0;
}
}
add_field(char *a, char *b)
{
printf ("%s - name\n", name);
printf ("%d - phone\n", phone);
}
-
Your defining add_field as taking no parameters and then redefining it later to take two parameters, so not suprisingly the compiler is complaining.
-
so will u tell me how to correct this? what I must do?
-
There are many other things you would need to do besides correcting the definition of the "add_field" function.
1.) Allocate the memory for the variable "name" and declare it appropriately.
2.) phone is of type "int" rather than "character"
3.) function declaration should be changed to
float add_field(char *, int);
Dunno why do you want to call it add_field and why is your return value of the type float.
I would rather declare it as
void display_fields (char *, int);
Anoop.
-
I'be correct the source code...
Code:
#include "stdio.h"
void add_field();
char name[20];
int phone;
int main()
{
int choice; /* promenliva za izbor */
int clear; /* izchistva <CR> */
printf ("Menu: \n");
printf ("Add field (1)\n");
printf ("Del field (2)\n");
printf ("Update field (3)\n");
printf ("View all fields (4)\n");
printf ("Exit (5)\n");
scanf ("%d", &choice);
switch(choice)
{
case 1 :
printf ("Enter name: \n");
scanf ("%s", &name);
printf ("Enter phone: \n");
scanf ("%d", &phone);
add_field(name,phone);
break;
case 2 :
printf ("asd");
break;
case 3 :
printf ("asd");
break;
case 4 :
printf ("asd");
break;
default :
printf("Ok. Thanks for using me. Goodbye");
}
return 0;
}
void add_field(char *n, int phone)
{
printf ("%s - name\n", n);
printf ("%d - phone\n", phone);
}
...but the same error occur...
-
Code:
#include "stdio.h"
void add_field(); //change to: void add_field(char *, int);
char name[20];
int phone;
But anoopks already wrote that.
And change
scanf ("%s", &name);
to
scanf ("%s", name);
-
I've alredy correct my code. Now all is OK. Thanks for your help.
-
hope this helps
Spedge,
I've had a little play with your code and made some changes that make it work. I'm too new at this to tell you weather what I've done is good coding practice but it should give you an idea of where your heading.
PS: If there are any glaring errors or just bad practices in my code, I be grateful if someone would let me know.
Code:
#include "stdio.h"
void add_field(void);//must include function prototype
char name[20]; //made these arrays of char rather than single chars
char phone[20];
int main()
{
int choice,a; /* promenliva za izbor (int a added to keep window open)*/
int clear; /* izchistva <CR> */
/*float add_field();*///dont know what this is for?
printf ("Menu: \n");
printf ("Add field (1)\n");
printf ("Del field (2)\n");
printf ("Update field (3)\n");
printf ("View all fields (4)\n");
printf ("Exit (5)\n");
scanf ("%d", &choice);
switch(choice)
{
case 1 :
printf ("Enter name: \n");
scanf ("%s", &name);
printf ("Enter phone: \n");
scanf ("%s", &phone); //changed %d integer to %s string
add_field();//arrays defined globally so no arguments needed
break;
case 2 :
printf ("asd");
break;
case 3 :
printf ("asd");
break;
case 4 :
printf ("asd");
break;
default :
printf("Ok. Thanks for using me. Goodbye");
}
scanf("%d",&a);/*used by me to keep window open*/
return 0;/*return taken out of switch statement*/
}
void add_field(void) //tell compiler no arguments taken or returned
{
printf ("%s - name\n", name);
printf ("%s - phone\n", phone);//again %d made %s
}
-
>>>PS: If there are any glaring errors or just bad practices in my code, I be grateful if someone would let me know.
Just that it would be nice to do away with as many global variables as possible. And hence you could declare the function as
void add_field( char *, char *);
and declare variables "name" & "phone" inside main and pass them as arguments.
Anoop
-
scanf ("%s", &name);
scanf ("%s", &phone);
name and phone are pointers. Thus, no address-of operator & is needed.
scanf ("%s", name);
scanf ("%s", phone);
btw: good idea to change phone to char string. I forgot about phone numbers starting with 0. ;)
-
Cheers Anoop
thanks Anoop,
Ordinarily I wouldn't use global variables, but I took Spedge's code and just tried to make it work, without really considering how I'd go about that problem,(it's the pressure of trying to read the board, figure out what the problem is and convince my boss that I really am working hard, given that my job has nothing to do with programming).
Once again Cheers:)