首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何让我的数组在按enter键时停止读取字符?

如何让我的数组在按enter键时停止读取字符?
EN

Stack Overflow用户
提问于 2019-10-30 10:03:04
回答 1查看 24关注 0票数 0

我是编程新手,我似乎想不出如何解决这个问题。我的任务非常简单,只需使用strcmp函数按字母顺序对两个单词进行排序。

我的程序编译并运行,但是直到我用字符完全填满数组,它才会继续运行,但是我们应该能够使用不同长度的单词。

下面是我写的内容:

代码语言:javascript
运行
复制
 #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þ

我已经尝试了我能想到的所有组合,任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

发布于 2019-10-30 10:22:07

用于输入。使用getline或直接接受输入。而不是这个-

代码语言:javascript
运行
复制
 cout << "Please enter your first word: \n";
 for(x=0; x<10; x++)  cin >> wordone[10];

可能的解决方案。

  1. 这样做-

char name10;cin>>name;

  • or do this

string s;cin>>s;//只是像普通变量一样接受输入。记住#include

  • 或者这样做--

字符串s;//对于getline,这是必须的。使用字符串getline(cin,s);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58617769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档