C++ 如何在c ++中将字符串打印到控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15071722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:59:12  来源:igfitidea点击:

how to print a string to console in c++

c++consolecout

提问by AngryDuck

Im trying to print a string to console in c++ console application.

我试图在 C++ 控制台应用程序中将字符串打印到控制台。

void Divisibility::print(int number, bool divisible)
{
    if(divisible == true)
    {
        cout << number << " is divisible by" << divisibleBy << endl;
    }
    else
    {
        cout << divisiblyBy << endl;
    }
}

i have the correct includes etc, this error i believe is just that i simply dont know how to print to console in c++ yet and this i guess isnt the way to do it

我有正确的包含等,我相信这个错误只是我根本不知道如何在 C++ 中打印到控制台,我想这不是这样做的方法

EDIT: sorry forgot to mention divisiblyBy is the string

编辑:抱歉忘了提及 divisivelyBy 是字符串

回答by Rich

yes it's possible to print a string to the console.

是的,可以将字符串打印到控制台。

#include "stdafx.h"
#include <string>
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    string strMytestString("hello world");
    cout << strMytestString;
    return 0;
}

stdafx.h isn't pertinent to the solution, everything else is.

stdafx.h 与解决方案无关,其他一切都是。

回答by Erdogan

All you have to do is add:

您所要做的就是添加:

#include <string>
using namespace std;

at the top. (BTW I know this was posted in 2013 but I just wanted to answer)

在顶部。(顺便说一句,我知道这是在 2013 年发布的,但我只是想回答)

回答by Racky

Quoting: "Visual Studio does not support std::cout as debug tool for non-console applications" = if you use it, Visual Studio shows nothing in the "output" window (in my case VS2008)

引用:“Visual Studio 不支持 std::cout 作为非控制台应用程序的调试工具”= 如果您使用它,Visual Studio 在“输出”窗口中不显示任何内容(在我的情况下为 VS2008)

https://stackoverflow.com/a/19095301/457128

https://stackoverflow.com/a/19095301/457128