6 Replies - 3563 Views - Last Post: 14 March 2013 - 08:03 AM Rate Topic: ***-- 2 Votes

#1 Brian J  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-December 12

Undeclared identifier in function

Posted 13 March 2013 - 05:35 PM

When compiling the following code I get undeclared identifier errors on 'listPointer','query' and 'String'.I have fixed the rest of the errors from the error list but these got me stumped.


#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<stdlib.h>
#include<dos.h>

//#defines here
#define FALSE 0
#define NULL 0
#define MAX 30
 
//functions
contact * getFirst(contact *listpointer,String query[MAX]);
void getLast();
void getEmail();
void getCompany();
void getNumber();
void editCon();
void delCon();
void addCon();
void listAll();
void sortCon();
void Menu (int *choice);


//struct to order contactList
struct contact
{
	String firstName[MAX],lastName[MAX],email[MAX],companyName[MAX];
	long phoneNum[MAX];
	struct contact *next;
	struct contact *prev;

};



int main()
{
	contact *listpointer;
	listpointer = NULL;
    String query;int iChoice = 0;
    


	Menu (&iChoice);
	
	iChoice;

	fflush(stdin);
	scanf_s("%d", &iChoice);
	// user enters one of 9 values
	// options are as follows: get first name,last name,list all contacts,search through contacts,add a new contact,edit/delete or sort contacts.
	switch(iChoice)
	{
		case 1:		
		{
			printf ("Enter contact first name to get details  ");
			scanf ("%d", &query);
			listpointer = getFirst (listpointer, query);
			break;
		}
		case 2:		
		{
			getLast();
			break;
		}
		case 3:		
		{
			listAll();
			break;
		}
		case 4:		
		{
			getEmail();
			break;
		}
		case 5:		
		{
			getCompany();
			break;
		}
		case 6:		
		{
			getNumber();
			break;
		}
		case 7:		
		{
			addCon();
			break;
		}
		case 8:		
		{
			editCon();
			break;
		}
		case 9:		
		{
			delCon();
			break;
		}
		case 10:		
		{
			sortCon();
			break;
		}
		case 11:		// exit
		{
			printf("\n\nProgram exiting!...");
			exit(0);//terminates program
			break;
		}
		default:
		printf ("Invalid menu choice - try again\n");
		break;
		
	}//end of switch
	return(iChoice);


}//end of main
//menu function to test if invalid input was entered in a menu choice.
void Menu (int *iChoice)
{
    char    local;

	system("cls");
	printf("\n\n\t\tBrian Varley/G00290342\n\n");
	printf("\n\n\t\tWelcome to my Contact Manager\n\n");
	printf("\n\t\t1. First name");
	printf("\n\t\t2. Last name");
	printf("\n\t\t3. List all contacts");
	printf("\n\t\t4. Search email");
	printf("\n\t\t5. Search company name");
	printf("\n\t\t6. Search number");
	printf("\n\t\t7. Add contact");
	printf("\n\t\t8. Edit contact");
	printf("\n\t\t9. Delete contact");
	printf("\n\t\t10. Sort contacts");
	printf("\n\t\t11. Exit");
	printf("\n\n\t\tEnter your menu choice: ");

    do
    {
	local = getchar ();
	if ( (isdigit(local) == FALSE) && (local != '\n') )
        {
	    printf ("\nYou must enter an integer.\n");
	    printf ("");
	}
    } while (isdigit ((unsigned char) local) == FALSE);

    *iChoice = (int) local - '0';
}

//function to get a contact by entering first name
contact * getFirst(contact *listpointer,String query[MAX]);
{
	//variables
	char query[MAX],firstName[MAX];
	FILE *fp, *ft;
	int i,l,found;

	system("cls");
	do
	{
	found=0;
	

	l=strlen(query);
	fp=fopen("ContactList.txt","r");
 
	system("cls");
	printf("\n\n..::Search result for '%s' \n===================================================\n",query);
	while(fread(&list,sizeof(list),1,fp)==1)
	{
	for(i=0;i<=l;i++)
	firstName[i]=list.firstName[i];
	firstName[l]='\0';
	if(stricmp(firstName,query)==0)
	{
	printf("\n..::First Name\t: %s\n..::Second Name\t: %ld\n..::Email\t: %s\n..::CompanyName\t:  %s\n..::Number\t:  %s\n",list.firstName,list.lastName,list.email,list.companyName.list.phoneNumber);
	found++;
	if (found%4==0)
	{
	printf("..::Press any key to continue...");
	getch();
	}
	}
	}
 
	if(found==0)
	printf("\n..::No match found!");
	else
	printf("\n..::%d match(s) found!",found);
	fclose(fp);
	printf("\n ..::Try again?\n\n\t[1] Yes\t\t[11] No\n\t");
	scanf("%d",&ch);
	}while(ch==1);



}








But as far as I can see I have declared these identifiers,so any ideas as to where I'm going wrong here? Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Undeclared identifier in function

#2 jimblumberg  Icon User is online

  • member icon

Reputation: 5334
  • View blog
  • Posts: 16,602
  • Joined: 25-December 09

Re: Undeclared identifier in function

Posted 13 March 2013 - 09:06 PM

Please post the complete error messages exactly as they appear in your development environment.

But one quick question, where have you defined your String data type?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Brian J  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-December 12

Re: Undeclared identifier in function

Posted 14 March 2013 - 04:52 AM

View Postjimblumberg, on 13 March 2013 - 09:06 PM, said:

Please post the complete error messages exactly as they appear in your development environment.

But one quick question, where have you defined your String data type?

Jim


I defined a query variable of type string in the function declaration.Also my error list is as follows:

36 Error error C2059: syntax error : ')' C:\Users\Brian\Documents\Visual Studio 2010\Projects\G00290342Manager.c\G00290342Manager.c\ContactManager.cpp 159 1
4 Error error C2065: 'listpointer' : undeclared identifier C:\Users\Brian\Documents\Visual Studio 2010\Projects\G00290342Manager.c\G00290342Manager.c\ContactManager.cpp 14 1
24 Error error C2065: 'query' : undeclared identifier C:\Users\Brian\Documents\Visual Studio 2010\Projects\G00290342Manager.c\G00290342Manager.c\ContactManager.cpp 61 1
5 Error error C2065: 'String' : undeclared identifier C:\Users\Brian\Documents\Visual Studio 2010\Projects\G00290342Manager.c\G00290342Manager.c\ContactManager.cpp 14 1
8 Error error C2078: too many initializers C:\Users\Brian\Documents\Visual Studio 2010\Projects\G00290342Manager.c\G00290342Manager.c\ContactManager.cpp 14 1
29 Error error C2086: 'int contact' : redefinition C:\Users\Brian\Documents\Visual Studio 2010\Projects\G00290342Manager.c\G00290342Manager.c\ContactManager.cpp 159 1
27 Error error C2143: syntax error : missing ';' before '*' C:\Users\Brian\Documents\Visual Studio 2010\Projects\G00290342Manager.c\G00290342Manager.c\ContactManager.cpp 159 1
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5893
  • View blog
  • Posts: 20,110
  • Joined: 05-May 12

Re: Undeclared identifier in function

Posted 14 March 2013 - 05:32 AM

Do you have a String class with a capital 'S'? Or did you mean to use string with a lowercase 's'?
Was This Post Helpful? 0
  • +
  • -

#5 Brian J  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-December 12

Re: Undeclared identifier in function

Posted 14 March 2013 - 07:14 AM

View PostSkydiver, on 14 March 2013 - 05:32 AM, said:

Do you have a String class with a capital 'S'? Or did you mean to use string with a lowercase 's'?


I wanted to declare my query variable a type String that will be passed on to the function.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon

Reputation: 5334
  • View blog
  • Posts: 16,602
  • Joined: 25-December 09

Re: Undeclared identifier in function

Posted 14 March 2013 - 07:22 AM

Since this seems to be a C program you need to either typedef your structure or use the struct keyword when using the structure.

Next you are trying to use the structure before you have defined it. You need to move the structure definition to after your includes but before your function prototypes.

Jim
Was This Post Helpful? 1
  • +
  • -

#7 Brian J  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-December 12

Re: Undeclared identifier in function

Posted 14 March 2013 - 08:03 AM

View Postjimblumberg, on 14 March 2013 - 07:22 AM, said:

Since this seems to be a C program you need to either typedef your structure or use the struct keyword when using the structure.

Next you are trying to use the structure before you have defined it. You need to move the structure definition to after your includes but before your function prototypes.

Jim


Thanks for the advice,I went through this in labs today so it is a lot clearer to me now. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1