我是编程新手,我似乎想不出如何解决这个问题。我的任务非常简单,只需使用strcmp函数按字母顺序对两个单词进行排序。
我的程序编译并运行,但是直到我用字符完全填满数组,它才会继续运行,但是我们应该能够使用不同长度的单词。
下面是我写的内容:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int x;
char wordone[10] , wordtwo[10];
cout << "Please enter your first word: \n";
for(x=0; x<10; x++) cin >> wordone[10];
cout << "Please enter the second word: \n";
for(x=0; x<10; x++) cin >> wordtwo[10];
if(strcmp(wordone, wordtwo)<0)
{
cout << wordone << endl << wordtwo;
}
if ( strcmp( wordone,wordtwo)>0)
{
cout << wordtwo << endl << wordone;
}
else
{
cout << wordone << endl << wordtwo;
}
return 0;
}
And the output looks like this:
Please enter your first word:
help
me
please
Please enter the second word:
hello
how
`
@
àu0þ我已经尝试了我能想到的所有组合,任何帮助都将不胜感激!
发布于 2019-10-30 10:22:07
用于输入。使用getline或直接接受输入。而不是这个-
cout << "Please enter your first word: \n";
for(x=0; x<10; x++) cin >> wordone[10];可能的解决方案。
char name10;cin>>name;
string s;cin>>s;//只是像普通变量一样接受输入。记住#include
字符串s;//对于getline,这是必须的。使用字符串getline(cin,s);
https://stackoverflow.com/questions/58617769
复制相似问题