Question

User should enter a few strings and input space as string when he is done. Code should return longest and shortest word entered.

strcmp always returns -1... what am i doing wrong?

#include <iostream>
#include <cstring>
using namespace std;
int main() {
    char S[100][20];
    int I = 0;
    do {
            cout << "Enter text:" << endl;
            cin.getline(S[I],100);
    } while (I < 19 && strcmp(S[I++],""));
    char Max[100], Min[100];
    strcpy(Max, S[0]);
    strcpy(Min, S[0]);
    for (int J = 1; J < I; J++) {
        if (strcmp(S[J], Max) == 1)
            strcpy(Max, S[J]);
        if (strcmp(S[J], Min) == -1)
            strcpy(Min, S[J]);
    }
    cout << "Max = " << Max << endl;
    cout << "Min = " << Min << endl;
    system("pause");
    return 0;
}
Was it helpful?

Solution

So, a couple of things:

  • variables should be lowercase;
  • you are defining your array of string with wrong length (should be s[20][100]);
  • in your while cycle, you should go 'till i < 20;
  • the last string in your array will always be the empty string (hence: s_min will be always empty);
  • strcmp compares strings, it doesn't tell you which one is the longest. You should use strlen for that...

Here the working code:

#include <iostream>
#include <cstring>
using namespace std;

int main() {
  char s[20][100];
  int i = 0;
  do {
    cout << "Enter text:" << endl;
    cin.getline(s[i], 100);
  } while (i < 20 && strcmp(s[i++],""));

  char s_max[100], s_min[100];
  strcpy(s_max, s[0]);
  strcpy(s_min, s[0]);
  for (int j = 1; j < i-1; j++) {
    if (strlen(s[j]) > strlen(s_max))
      strcpy(s_max, s[j]);
    if (strlen(s[j]) < strlen(s_min))
      strcpy(s_min, s[j]);
  }

  cout << "Max = " << s_max << endl;
  cout << "Min = " << s_min << endl;
  return 0;
}

OTHER TIPS

Try use char S[20][100]; instead of char S[100][20];

From cplusplus.com:

Returns an integral value indicating the relationship between the strings: A zero value indicates that both strings are equal. A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.

It shouldn't be 1 or -1, try testing with >/< 0. Also, test if the strings read from stdio ends up in '\0' and eventually add it(getline should), because strcmp makes use of it.

Also, array size if wrong. Should be char S[20][100].

  1. You're declaring your array backwards; it looks like you really want char S[20][100].
  2. You need to compare using < 0 and > 0, since strcmp() doesn't guarantee that it will return you a 1 or a -1. From the man page:

    The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2.

You're trying to compare the strings in your code but your question says you want to compare length. Use strlen on a null-terminated strings to compare lengths. Here is the reference. Better yet, if what you're writing is C++ code (as it is tagged), you're already using the standard library, so go ahead and use std::string. This sounds like it should be tagged homework, however, in which case I assume you can't use the std::string class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top