how to print a string to console in c++
Asked Answered
H

4

35

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

Headless answered 25/2, 2013 at 16:34 Comment(10)
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.Cadency
Which OS are you targeting?Cordeiro
Note that global variables such as divisibleBy are not good; you should pass it to the function as a constant reference argument.Shaunda
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.Shaunda
Have you included iostream on the top of your program?Ephebe
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.Cashandcarry
If your platform is windows, do you use a conole application?Swagerty
@Headless 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.Garderobe
Yes it is possible and the code you posted is correct assuming divisiblyBy is std::string.Claraclarabella
I've tried all the answers here and VS still says "'cout': undeclared identifier".Waites
G
45

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.

Garderobe answered 25/2, 2013 at 17:23 Comment(0)
M
14

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)

Muir answered 14/5, 2016 at 0:1 Comment(1)
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.Tatman
T
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)

Tatman answered 24/6, 2016 at 8:48 Comment(0)
G
3

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
Geocentric answered 19/4, 2022 at 23:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.