-1

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.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
dud3
  • 145
  • 1
  • 12
  • 1
    where is the definition for `string_compare()`? – Stephen Docy Feb 28 '18 at 22:54
  • Here is my string-compare method int string_compare(char str1[], char str2[]) { int ctr=strlen(str1); int ctr2=strlen(str2); int counter=0; if(strlen(str1)!=strlen(str2) ){ return -1; } else{ for (int i = 0; i < strlen(str1); ++i) { if(str1[i]==str2[i]){ counter++; } } if(counter==strlen(str1)){ return 0; } else{ return -1; } } } – dud3 Feb 28 '18 at 22:55
  • oh sorry for this mess. My first question here :( – dud3 Feb 28 '18 at 22:56
  • @dud3 Just edit the question to add the method – Felix Feb 28 '18 at 22:57
  • No worries, don't post the code in a comment, it is unreadable, edit your question and post it there – Stephen Docy Feb 28 '18 at 22:57
  • Hmmm, should you be passing in `newString[0]` to see if it is `QUIT` or `newString[ctr - 1]`? In your input, is `QUIT` the last word entered or the first? I'm thinking last, as in `MRCT DISP QUIT`? – Stephen Docy Feb 28 '18 at 23:00
  • In fact, the code in the while loop will only ever process the first word in the input, over and over – Stephen Docy Feb 28 '18 at 23:06
  • Edited. By the way someone told me that i should use gets method. It worked. newString contains user input for example, if user enters MRCT 5, i have MRCT at newString[0] and 5 at newString[1]. – dud3 Feb 28 '18 at 23:07
  • 1
    @dud3 "By the way someone told me that i should use gets method." Using `gets()` is a [terrible choice](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) - and a hint to the quality of that advise. Use `fgets()`. – chux - Reinstate Monica Feb 28 '18 at 23:36

2 Answers2

1

The scanf() stops scanning after the first failure.

So here:

scanf("%10[0-9a-zA-Z ]s\n",str1);

The scan tries to interpret three things:

  • A string %[] into a variable str1
  • the character s
  • The character '\n' which will result in a sequence of white space characters being read from the input.

Note if the string is zero length it will fail at the string and not interpret the s or the \n character.

One: I suspect that s is an error and you don't want it.

Two: Don't use "%[^\n]\n" to read a line. As this fails if the line is empty (just has the \n character).

if (scanf("%10[0-9a-zA-Z ]", str1) == 1)
{
    // Always check that the value was read.
    // Then deal with it.
    ....
}
scanf("%*[^\n]"); // Ignore any remaining character above 10.
                  // Note this may still fail so don't add \n on the end
                  // Deal with end of line separately.

char c;
if (scanf("%c", &c) == 1 && c == '\n')  // Now read the end of line character.
{
    // End of line correctly read.
}
Martin York
  • 257,169
  • 86
  • 333
  • 562
-1

Use: scanf("%[^\n]\n" , str1); to get a line using scanf function.

Ebram Shehata
  • 446
  • 5
  • 20
  • 1
    `"%[^\n]\n"` fails to read anything if the line begins with a `"\n"`. Otherwise `"%[^\n]\n"` will consume a _line_ and all following white-space and blocks until following non-white-space detected. Would not pass a code review. Use `fgets()` to read a line. – chux - Reinstate Monica Feb 28 '18 at 23:21