34

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

AngryDuck
  • 4,358
  • 13
  • 57
  • 91
  • 4
    What doesn't work? How are you invoking this code? What error are you seeing? Help us help you. All you've done is posted one isolated segment of code which (in isolation) appears to be syntactically valid. – user229044 Feb 25 '13 at 16:35
  • 1
    Which OS are you targeting? – Michael Feb 25 '13 at 16:35
  • Note that global variables such as `divisibleBy` are not good; you should pass it to the function as a constant reference argument. – Jonathan Leffler Feb 25 '13 at 16:36
  • On the whole, that is the correct way to print to `cout` and if `cout` is attached to (going to) the console, it should be correct. If you are running this from a GUI IDE and it creates a new window which then vanishes, that is actually not directly a problem with the program but rather with the programming environment. You probably want a space after the `by` in the string literal. – Jonathan Leffler Feb 25 '13 at 16:37
  • Have you included `iostream` on the top of your program? – tune2fs Feb 25 '13 at 16:38
  • 3
    Make sure you didn't forget "using namespace std" at the top of the program. Try to replace all "cout" to "std:cout" to figure it out. – Mikhail Kalashnikov Feb 25 '13 at 16:38
  • If your platform is windows, do you use a conole application? – Lars Feb 25 '13 at 16:43
  • @AngryDuck it's not "hate" , they're just trying you help you formulate a more precise question which will help get you the answer you need. – Rich Feb 25 '13 at 16:51
  • Yes it is possible and the code you posted is correct assuming `divisiblyBy` is `std::string`. –  Feb 25 '13 at 17:17
  • I've tried all the answers here and VS still says "'cout': undeclared identifier". – Steve Smith Oct 25 '21 at 14:43

4 Answers4

44

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.

Rich
  • 4,572
  • 3
  • 25
  • 31
15

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)

xXCoolinXx
  • 361
  • 5
  • 15
  • 5
    Thank you for adding this comment, many people publish incomplete code that does not work so it is useless for beginners due to missing 1 or 2 lines of "include", "using" etc. – Racky Jun 24 '16 at 08:39
6

"Visual Studio does not support std::cout as debug tool for non-console applications"
- from Marius Amado-Alves' answer to "How can I see cout output in a non-console application?"

Which means if you use it, Visual Studio shows nothing in the "output" window (in my case VS2008)

TylerH
  • 20,799
  • 66
  • 75
  • 101
Racky
  • 1,173
  • 18
  • 24
2

you need to include the needed headers first which are:

1- #include<iostream>, so that you can read and write. 2- #include<string>, so that you can use (string) class. 3- using namespace std or you can just write

std::cout