Click here to Skip to main content
15,889,714 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Now I'm trying to make a sofeware about online video on demand,use p2p to solve network question.
The following is an error & i can't find the way...
PS: it's seems like a litter longer... forgive me...

C++
int Command() //get the command
{
	char key[256],temp[256];
	int i,j,result;
	char *command[6]={"connect","play","pause","stop","quit","?"};
	char parameter[256];
	printf("please input command................\n");
	while(1)
	{
		memset(key,0,256);
		memset(temp,0,256);
		memset(parameter,0,256);
		int flag=0;
		printf("%s>>",strHostName); //show the command symbol
		gets(temp);
		j=0;  //remove the excess space 
		for(i=0;i<strlen(temp);i++)>
		{
			if(temp[i+1]!='\0')
			{
				if(temp[i]==' '&&temp[i+1]==' ')i++;
				else{key[j]=temp[i];j++;}
			}
			else
			{
				if(temp[i]==' '){key[j]='\0';break;}
				else{key[j]=temp[i];j++;key[j]='\0';}
			}
		}
		key[j+1]='\0';
		i=strcspn(key," ");
		key[i]='\0';
		i++;j=0;
		while(key[i]!='\0')
		{
			parameter[j]=key[i];
			i++;j++;
		}
		parameter[j]='\0';
		
		for(i=0;i<13;i++)   //compare with the original command String       
			if(strcmp(key,command[i])==0)
			{
				flag=1;
				break;
			}
		if(flag==0)
		{
			printf("%s>>",strHostName);
			cout<<"bad command!"<<endl;
		}
		else
		{
			switch(i)
			{
			case 0:
				result=Connect(parameter);
				break;
			case 1:
				result=Play(parameter);
				break;
			case 2:
				Pause();
				break;
			case 3:
				Stop();
				break;
			case 4:
				Quit();
				exit(0);
			case 5:
				for(i=0;i<6;i++)
				{
					printf(command[i]);
					printf("\n");
				}
				break;
			}
		}
		switch(result)
		{
		case 0:
			break;
		case 1:
			printf("%s>>",strUserName);
			cout<<"parameters are not enough!"<<endl;
		case 2:
			printf("%s>>",strUserName);
			cout<<"parameters are more!"<<endl;
		}
	}
}

int Connect(LPSTR parameter)
{
	int i,j,k,len=strlen(parameter);
	char temp[10];
//////////////////////////////////////////////////parameter/////////////////////////////////////////
	if(!strlen(parameter))
		return 1;  //no parameter
	k=strcspn(parameter," ");
	parameter[k]=5;
    i=0;j=0;
	while(parameter[i]!=5)
	{
		sIP[j]=parameter[i];
		i++;j++;
	}
	sIP[j]='\0';

	k=strcspn(parameter," ");
	if(k==len)
		return 1;  // lack parameter
	parameter[k]=5;
    i++;j=0;
	while(parameter[i]!=5)
	{
		strUserName[j]=parameter[i];
		i++;j++;
	}
	strUserName[j]='\0';

	k=strcspn(parameter," ");
	if(k==len)
		return 1;
	parameter[k]=5;
    i++;j=0;
	while(parameter[i]!=5)
	{
		strPassWord[j]=parameter[i];
		i++;j++;
	}
	strPassWord[j]='\0';
    
	k=strcspn(parameter," ");
	if(k<len)>
		return 2;  //excess parameter
	i++;j=0;
	while(parameter[i]!='\0')
	{
		temp[j]=parameter[i];
		i++;j++;
	}
	temp[j]='\0';
	Port=atoi(temp);




Then open clients.exe,no matter what words,the answer always be"parameters are not enought¶meters are more "
I have no idea about it...hope someone to save me unless my god..

Edit: Changed tags to C as there's no C++ in there
Posted
Updated 10-Jul-12 7:01am
v2

1 solution

Well, to start with, you need a break in all of your cases unless you want the next case to execute:
C++
switch(result)
{
case 0:
    break;
case 1:
    printf("%s>>",strUserName);
    cout<<"parameters are not enough!"<<endl;
    break; //if this isn't here case 2 will also run!
case 2:
    printf("%s>>",strUserName);
    cout<<"parameters are more!"<<endl;
    break; //you don't need this one, but it will prevent problems if you add more cases and forget to add it then
}

So that's why you get both messages. Getting both tells me that you are always hitting case 1 (case 0 would print nothing, and case 2 would only print the last message). So debug the program and figure out which "return 1" is being hit, and work from there!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900