简体   繁体   English

C++ 程序在将 int 与 cin 但不是字符串一起使用时工作

[英]C++ Program works when using int with cin but not a string

I wrote this simple program to more or less "warm up" in using C++ and when asking for a string input, the program stops working as intended.我编写了这个简单的程序,在使用 C++ 时或多或少地“热身”,当要求输入字符串时,程序停止按预期工作。

Here it is asking for an int, which works as intended:这里它要求一个 int,它按预期工作:

#include <iostream>

using namespace std;

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

Output is as follows: Output如下:

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> 

However, when changing code to use a string instead, the program skips over "enter something" and the cin lines and jumps to a new line (for commands and etc.) instead.但是,当更改代码以改用字符串时,程序会跳过“输入内容”和 cin 行并跳转到新行(用于命令等)。

Non-working code:非工作代码:

#include <iostream>
#include <string>

using namespace std;

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

Output log: Output 日志:

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> 

I've tried compiling with both MinGW and CygWin to no avail and am running the program with coderunner (run in terminal is checked) in Visual Studio code.我尝试使用 MinGW 和 CygWin 进行编译无济于事,并在 Visual Studio 代码中使用 coderunner(在终端中运行已检查)运行程序。

It worked fine when i did it..我做的时候效果很好。。

Code:代码:

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

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

Output: Output:

enter something
25
25 is a number.

You can use std::to_string() function to convert usertypestuff to string,您可以使用std::to_string() function 将usertypestuff转换为字符串,

string str = to_string(usertypestuff);
cin >> usertypestuff;

The std::operator>>(std::istream&, std::string&) method reads from the stream until it finds a space, and then returns. std::operator>>(std::istream&, std::string&)方法从 stream 读取,直到找到一个空格,然后返回。 Then the rest of the program runs.然后程序的rest运行。 Your code is fine, but I suspect that while running the program in a terminal, you're entering an input that has a space in the middle.您的代码很好,但我怀疑在终端中运行程序时,您正在输入一个中间有空格的输入。 The program reads up to the space, then finishes running correctly, and then exits.程序读取空间,然后正确完成运行,然后退出。 After that, the terminal reads the rest of the input as the next program to run.之后,终端读取输入的rest作为下一个程序运行。 But since your input wasn't a valid command, the terminal displays a CommandNotFoundException .但由于您的输入不是有效的命令,终端会显示CommandNotFoundException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM