Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7176155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:27:08+00:00 2026-05-28T16:27:08+00:00

User should enter a few strings and input space as string when he is

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:27:09+00:00Added an answer on May 28, 2026 at 4:27 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The user should input two strings and the script will performe a text search:
In my web application, I have a text box. The user should enter a
If a user types in [[0,0,0], [0,0,1], [1,1,0]] and press enter, the program should
I have an input (outside form) that captures name of an image. User should
In a Swing app a method should continue only after user enters a correct
I created a div(search result term bar) which should display only when user enters
I want to do the following: The user should have his own sub site
Theoretically, the end user should never see internal errors. But in practice, theory and
I'm currently working on a program that has many of those the user SHOULD
In my mental model of authentication, a user should be distinct from the way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.