【问题标题】:C++ Program works when using int with cin but not a stringC++ 程序在将 int 与 cin 但不是字符串一起使用时工作
【发布时间】:2020-06-03 06:51:57
【问题描述】:

我编写了这个简单的程序来或多或少地“热身”使用 C++,当要求输入字符串时,程序停止按预期工作。

这里要求一个 int,它按预期工作:

#include <iostream>

using namespace std;

int main() {
    cout << "enter something\n";
    int usertypestuff;
    cin >> usertypestuff;
    cout << usertypestuff << " is a number.";
}

输出如下:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\Users\fortm\Documents\CPPPrac> cd "c:\Users\fortm\Documents\CPPPrac\MainPrac\" ; if ($?) { g++ Main.cpp -o Main } ; if ($?) { .\Main }
enter something
12.5
12 is a number.
PS C:\Users\fortm\Documents\CPPPrac\MainPrac> 

但是,当更改代码以改用字符串时,程序会跳过“输入内容”和 cin 行并跳转到新行(用于命令等)。

非工作代码:

#include <iostream>
#include <string>

using namespace std;

int main() {
    cout << "enter something\n";
    string usertypestuff;
    cin >> usertypestuff;
    cout << usertypestuff << " is a number.";
}

输出日志:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\Users\fortm\Documents\CPPPrac> cd "c:\Users\fortm\Documents\CPPPrac\MainPrac\" ; if ($?) { g++ Main.cpp -o Main } ; if ($?) { .\Main }
PS C:\Users\fortm\Documents\CPPPrac\MainPrac> 12
12
PS C:\Users\fortm\Documents\CPPPrac\MainPrac> hu
hu : The term 'hu' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ hu
+ ~~
    + CategoryInfo          : ObjectNotFound: (hu:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\Users\fortm\Documents\CPPPrac\MainPrac> 

我已尝试使用 MinGW 和 CygWin 进行编译,但无济于事,我正在 Visual Studio 代码中使用 coderunner(在终端中运行已检查)运行程序。

【问题讨论】:

  • 如果您不介意,能否请您重新表述一下您想做什么?我很难理解。
  • 请显示无效的代码(minimal reproducible example),您能解释一下causes the program to instead look for a command instead of running the program 的含义吗?
  • std::cin 在遇到空格后停止接受输入。您应该确保您的字符串在开头没有任何空格。要使用空格输入,请使用 std::getline (std::cin,usertypestuff)
  • 您是否尝试输入多行字符串?如前所述,&gt;&gt; 运算符将在任何空白处停止读取,包括换行符。当您按下Enter 键时,程序将读取该行并(对于&gt;&gt;)从该行获取第一个“单词”。然后程序将打印其输出并退出,留下您可能输入的任何其他行作为控制台或 shell 的输入。如果您想读取单行,则使用单个 std::getline 调用(再次提及),否则在循环中使用 std::getline 来读取所有行。
  • 顺便请edit把你的问题告诉我们你是如何运行程序的。

标签: c++ string cin


【解决方案1】:

我做的时候效果很好..

代码:

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

int main() {
    cout << "enter something\n";
    string usertypestuff;
    cin >> usertypestuff;
    cout << usertypestuff << " is a number.";
    return 0;
}

输出:

enter something
25
25 is a number.

【讨论】:

    【解决方案2】:

    您可以使用std::to_string()函数将usertypestuff转换为字符串,

    string str = to_string(usertypestuff);
    

    【讨论】:

      【解决方案3】:
      cin >> usertypestuff;
      

      std::operator&gt;&gt;(std::istream&amp;, std::string&amp;) 方法从流中读取,直到找到一个空格,然后返回。然后程序的其余部分运行。您的代码很好,但我怀疑在终端中运行程序时,您输入的输入中间有一个空格。程序读取空间,然后正确完成运行,然后退出。之后,终端读取输入的其余部分作为下一个要运行的程序。但由于您输入的命令无效,终端会显示CommandNotFoundException

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-10
        • 1970-01-01
        • 2013-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-06
        相关资源
        最近更新 更多