I've to write a program which gets a colour from user.Users will enter the colour's name if the colours is exist in my string,program will give a msg.If it doesn't exist give an error msg and stop program.I've already write a code but it doesnt works accurately.

int i,j;

void getColours(char codes[10][7]);

int _tmain(int argc, _TCHAR* argv[])
{
	char colours[10][7]={"black","brown","red","orange","yellow","green","blue","violet","gray","white"};
	getColours(colours);

}

void getColours(char codes[10][7]){
	char colour[3][7];
	printf("Enter the colours:\n");
	for(i=0;i<3;i++){
		printf("Colour %d:\n",(i+1));
		scanf("%s",&colour[i][7]);
		for(j=0;j<10;j++){
				if(colour[i][7]==codes[j][7])
					printf("Valid\n");
				else{
					printf("invalid\n");
					break;
				}
			}
	}
}

Recommended Answers

All 3 Replies

Lines 17 and 19. "7" is an illegal index. You want the beginning of the string, not the end anyway, so use 0, not 7 as the second array index in lines 17 and 19. And since it's 0, you can actually drop that second index.

scanf("%s", colour[i]);

As for line 19, you want to use the strcmp function from string.h
http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

if(strcmp(colour[i], codes[j]) == 0)

Lines 17 and 19. "7" is an illegal index. You want the beginning of the string, not the end anyway, so use 0, not 7 as the second array index in lines 17 and 19. And since it's 0, you can actually drop that second index.

scanf("%s", colour[i]);

As for line 19, you want to use the strcmp function from string.h
http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

if(strcmp(colour[i], codes[j]) == 0)

Ohh i've been thinking about this question for two hours.
You solved my problem with two codes line :D Thanks a lot brother.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.