|
-
February 16th, 2022, 01:05 AM
#1
strcmp works sometime, but not always & blocks output to screen
This code is supposed to put 3 words in alphabetical order. It works fine for 5 of 6 possibilities. However, when I enter something like "rat cat dog" it doesn't (skips "rat" in Code::Blocks, no output in repl.it). Also, no cout's in the if/else structure print at all. Any ideas?
Code:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
char word1[20];
char word2[20];
char word3[20];
char phrase[64];
cout << "Enter three words, pressing Enter after each.\n";
gets(word1);
gets(word2);
gets(word3);
char first[20];
char second[20];
char third[20];
if (strcmp(word1, word2) > 0 && strcmp(word1, word3) > 0)
{
strcpy(third, word1);
if (strcmp(word2, word3) > 0)
{
strcpy(second, word2);
strcpy(first, word3);
}
else
{
strcpy(second, word3);
strcpy(second, word2);
}
}
else if (strcmp(word2, word1) > 0 && strcmp(word2, word3) > 0)
{
strcpy(third, word2);
if (strcmp(word1, word3) > 0)
{
strcpy(second, word1);
strcpy(first, word3);
}
else
{
strcpy(second, word3);
strcpy(first, word1);
}
}
else
{
strcpy(third, word3);
if (strcmp(word1, word2) > 0)
{
strcpy(second, word1);
strcpy(first, word2);
}
else
{
strcpy(second, word2);
strcpy(first, word1);
}
}
cout << "In order...\n";
cout << first << endl
<< second << endl
<< third << endl << endl; // puts
return 0;
}
Last edited by VictorN; February 16th, 2022 at 03:49 AM.
Reason: adding CODE tags
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|