'GetIndex' : not all control paths return a value

This is a discussion on 'GetIndex' : not all control paths return a value within the C Programming forums, part of the General Programming Boards category; Having some trouble, Could use some help. Code: #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> #define SZ ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    'GetIndex' : not all control paths return a value

    Having some trouble, Could use some help.
    Code:
     
    
    #define _CRT_SECURE_NO_DEPRECATE
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    #define SZ 100
    typedef struct 
    	{
    		char owner[SZ];
    		char type[SZ];
    		char name[SZ];
    		char date[SZ];
    		char sex;
    		int age;
    		double cost;
    	}pet;
    
    void GetPet(pet mypet[]);
    void Menu();
    void addEntry(pet mypet[], int numentries);
    int GetIndex(pet mypet[]);
    void deleteEntry(pet mypet[], int numentries, int index);
    void setAge(pet mypet[], int numentries);
    void Display(pet mypet[]);
    void Save(pet mypet[]);
    void Clear(pet mypet[]);
    char Quit();
    
    int main()
    {
    	pet mypet[SZ];
    	int numentries=5;
    	char choice;
    	char again='Y';
    	int index;
    	
    	while(again=='Y' || 'y')
    	{
    		GetPet(mypet);
    		setAge(mypet, numentries);
    		Menu();
    		scanf(" %c", &choice);
    		switch(choice)
    		{
    			case 'A': addEntry(mypet, numentries);
    					  numentries++;
    				      break;
    			case 'D': index=GetIndex(mypet);
    				      deleteEntry(mypet, numentries, index);
    				      numentries--;
    				      break;
    			case 'P': Display(mypet);
    				      break;
    			case 'S': Save(mypet);
    				      break;
    			case 'C': Clear(mypet);
    				     numentries=0;
    				     break;
    			case 'Q': again=Quit();
    					  break;
    		}
    	}
    	return 0;
    	
    }
    
    void GetPet(pet mypet[])
    {
    	strcpy(mypet[0].owner,"Doe");
    	strcpy(mypet[0].type,"Snake");
    	strcpy(mypet[0].name,"Sassy");
    	strcpy(mypet[0].date,"11/23/07");
    	mypet[0].sex='F';
    	mypet[0].age=9;
    	mypet[0].cost=112.56;
    
    	strcpy(mypet[1].owner,"Johnson");
    	strcpy(mypet[1].type,"Parrot");
    	strcpy(mypet[1].name,"Polly");
    	strcpy(mypet[1].date,"1/14/06");
    	mypet[1].sex='M';
    	mypet[1].age=15;
    	mypet[1].cost=224.90;
    
    	strcpy(mypet[2].owner,"Willruth");
    	strcpy(mypet[2].type,"Dog");
    	strcpy(mypet[2].name,"Winston");
    	strcpy(mypet[2].date,"5/28/07");
    	mypet[2].sex='M';
    	mypet[2].age=10;
    	mypet[2].cost=157.32;
    
    	strcpy(mypet[3].owner,"Mendes");
    	strcpy(mypet[3].type,"Cat");
    	strcpy(mypet[3].name,"Dandylion");
    	strcpy(mypet[3].date,"2/07/07");
    	mypet[3].sex='F';
    	mypet[3].age=2;
    	mypet[3].cost=300.87;
    
    	strcpy(mypet[4].owner,"Basic");
    	strcpy(mypet[4].type,"Cat");
    	strcpy(mypet[4].name,"Kiki");
    	strcpy(mypet[4].date,"6/17/05");
    	mypet[4].sex='F';
    	mypet[4].age=3;
    	mypet[4].cost=273.99;
    }
    
    void Menu()
    {
    	printf("\nA....Add a new record");
    	printf("\nD....Delete a record");
    	printf("\nP....Display all records");
    	printf("\nS....Save all records to a file");
    	printf("\nC....Clear all records");
    	printf("\nQ....Quit");
    }
    
    void addEntry(pet mypet[], int numentries)
    {
    	printf("\nEnter owner's name: ");
    	scanf(" %s", mypet[numentries].owner);
    	printf("\nEnter type of pet: ");
    	scanf(" %s", mypet[numentries].type);
    	printf("\nEnter name of pet: ");
    	scanf(" %s", mypet[numentries].name);
    	printf("\nEnter date the pet was last seen: ");
    	scanf(" %s", mypet[numentries].date);
    	printf("\nEnter sex of the pet: ");
    	scanf(" %c", mypet[numentries].sex);
    	printf("\nEnter age of the pet: ");
    	scanf(" %d", mypet[numentries].age);
    	printf("\nEnter the cost of the visit: ");
    	scanf(" %lf", mypet[numentries].cost);
    }
    
    int GetIndex(pet mypet[])
    {
    	char lastName[SZ];
    	char petName[SZ];
    	int i;
    	printf("\nEnter last name of the owner: ");
    	scanf(" %s", lastName);
    	printf("\nEnter name of pet: ");
    	scanf(" %s", petName);
    	for(i=0;i<SZ;i++)
    	{
    		if(strcmp(lastName, mypet[i].owner)==0 && strcmp(petName, mypet[i].name)==0)
    		{
    			return i;
    		}
    		else return -1;
    	}
    }
    
    void deleteEntry(pet mypet[], int numentries, int index)
    {
    	mypet[index]=mypet[numentries-1];
    }
    
    void setAge(pet mypet[], int numentries)
    {
    	int j;
    	for(j=numentries; j<SZ; j++)
    	{
    		mypet[j].age=-1;
    	}
    }
    
    void Diplay(pet mypet[])
    {
    	int k;
    	for(k=0;k<SZ;k++)
    	{
    		if(mypet[k].age!=-1)
    		{
    			printf("\nOwner: %s", mypet[k].owner);
    			printf("\nType of animal: %s", mypet[k].type);
    			printf("\nName of pet: %s", mypet[k].name);
    			printf("\nDate animal was last seen: %s", mypet[k].date);
    			printf("\nSex of animal: %c", mypet[k].sex);
    			printf("\nAge of animal: %d", mypet[k].age);
    			printf("\nCost of visit: %f", mypet[k].cost);
    		}
    	}
    }
    
    void Save(pet mypet[])
    {
        int x;
    	FILE *outp;
    	outp=fopen("vet.txt", "w");
    	for(x=0;x<SZ;x++)
    	{
    		if(mypet[x].age!=-1)
    		{
    			fprintf(outp, "\nThe owners name is %s", mypet[x].owner);
    			fprintf(outp, "\nThe type of pet is %s", mypet[x].type);
    			fprintf(outp, "\nThe name of the pet is %s", mypet[x].name);
    			fprintf(outp, "\nThe date pet was last seen is %s", mypet[x].date);
    			fprintf(outp, "\nThe sex of the pet is %c", mypet[x].sex);
    			fprintf(outp, "\nThe age of the animal is %d", mypet[x].age);
    			fprintf(outp, "\nThe cost of the visit is %f", mypet[x].cost);
    		}
    	}
    	fclose(outp);
    }
    
    void Clear(pet mypet[])
    {
    	int y;
    	for(y=0;y<SZ;y++)
    	{
    		mypet[y].age=-1;
    	}
    }
    
    char Quit()
    {
    	char input='N';
    	return input;
    }

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Well it should be pretty obvious from the error. In your GetIndex() function, not all control paths return a value.
    i.e. If SZ == 0, then the for loop will never execute and there is no return statement after the for loop.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    also, the for loop is kind of redundant, because no matter what, it will execute 0 or 1 times. once inside the for loop, it will return if the first condition, and it will return else--it returns in both cases so the loop will execute at most once.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    while(again=='Y' || 'y')
    this is always true. change it to:
    Code:
    while(again=='Y' || again== 'y')

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    15
    Should I take the return -1 out of the loop?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by partnole View Post
    Should I take the return -1 out of the loop?
    Yes!

    Whilst I think it's a bad idea to "guess and not know what you are doing", I also think this is one of those questions where if you try it, you'll find that it works.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 06:57 AM
  3. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  4. string class errors
    By CodeMonkey in forum C++ Programming
    Replies: 13
    Last Post: 07-20-2003, 11:20 PM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21