'how to print a string to console in c++
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
Solution 1:[1]
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.
Solution 2:[2]
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)
Solution 3:[3]
"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)
Solution 4:[4]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rich |
Solution 2 | xXCoolinXx |
Solution 3 | TylerH |
Solution 4 | Mutasim Al Moalmy |