how to print a string to console in c++

AngryDuck picture AngryDuck · Feb 25, 2013 · Viewed 174.9k times · Source

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

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

EDIT: sorry forgot to mention divisiblyBy is the string

Answer

Rich picture Rich · Feb 25, 2013

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.