I've been out of this loop for 5 hours. My scanf method does not work.
Here is my loop.
I could not execute library's strcmp so I wrote myself.
int string_compare(char str1[], char str2[])//Compare method
{
int ctr=strlen(str1);
int ctr2=strlen(str2);
int counter=0;
if(strlen(str1)!=strlen(str2) ){//if their lengths not equal -1
return -1;
} else
{
for (int i = 0; i < strlen(str1); ++i)
{
if(str1[i]==str2[i]){ //looking for their chars
counter++;
}
}
if(counter==strlen(str1)){
return 0;
} else
{
return -1;
}
}
}
char str1[100]; //for users command
char newString[10][10]; //after spliting command i have
while(string_compare(newString[0],"QUIT") != 0){
printf("Enter Commands For Execution\n");
scanf("%10[0-9a-zA-Z ]s\n",str1);
int i,j,ctr;
j=0; ctr=0;
for(i=0;i<=(strlen(str1));i++)
{
// if space or NULL found, assign NULL into newString[ctr]
if(str1[i]==' '||str1[i]=='\0')
{
newString[ctr][j]='\0';
ctr++; //for next word
j=0; //for next word, init index to 0
} else
{
newString[ctr][j]=str1[i];
j++;
}
}
if(string_compare(newString[0],"QUIT") == 0){
printf("Quitting\n");
break;
}
if(string_compare(newString[0],"MRCT") == 0){
printf("hey\n");
}
if(string_compare(newString[0],"DISP") == 0){
printf("hey2\n");
}
}
When I execute my c file,
the loop asks me to enter command such as "MRCT".
It forever prints
Enter Command
hey
Enter Command
hey
My way of using scanf()
does not work here.