Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to print a string to console in c++

Tags:

c++

console

cout

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

like image 528
AngryDuck Avatar asked Feb 25 '13 16:02

AngryDuck


People also ask

How do you print a string in C?

using printf() If we want to do a string output in C stored in memory and we want to output it as it is, then we can use the printf() function. This function, like scanf() uses the access specifier %s to output strings. The complete syntax for this method is: printf("%s", char *s);

Why is printf not showing up in console?

Running the program in normal mode, not debug mode, then all printf's are shown in the console tab. Output buffering really is the reason why outputs of printf statements are not visible in the console tab during debugging. setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0);

Which function is used to print a message on the console in C?

printf() This is mainly used in C language. It is a formatting function that prints to the standard out. It prints to the console and takes a format specifier to print.

How do you read and write strings in C?

C provides two basic ways to read and write strings. input/output functions, scanf/fscanf and printf/fprintf. Second, we can use a special set of string-only functions, get string (gets/fgets) and put string ( puts/fputs ).


2 Answers

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.

like image 140
Rich Avatar answered Sep 30 '22 02:09

Rich


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)

like image 21
Erdogan Avatar answered Sep 30 '22 02:09

Erdogan