Asked By
Mia Walker
30 points
N/A
Posted on - 04/08/2012
Hi Experts,
I am newbie in learning c++ programming language. Can you give me some example of c++ program that display string?
Thanks.
How can I display a string using c++?
Hy,
Suppose you want to open a text file through the C++, then you need to use bellows script or source :
intmain()
{ std::wfstream myfile("C:\Users\nirob\Documents\mytext.txt");
if(!myfile.is_open()
)
{std::cout <<"error"<<std::endl;
}
else
{ std::cout <<"opened"<<std::endl;
} std::wstring mystring; myfile >>mystring;
std::wcout <<mystring <<std::endl; system("PAUSE");
}
There I tried to open a text document which directory is ("C:\Users\nirob\Documents\mytext.txt” , So at first you should keep your text file in a folder then just put the directory in your C++ source or script. Then use above code for call any text file in your script. That’s a easy process to open any text file by that C++ source.
Suppose you want to open a variable string in C++, then you should create your own source
by the help of bellows source or code :
string vers ="v. ";
vers +=maj_vers;// + 50; vers +=".";
vers +=min_vers;// + 50; vers +=".";
vers +=patch_vers;// + 50;
constchar*pvers =vers.c_str();
mvaddstr(7,19,pvers); refresh();
Thanks.
How can I display a string using c++?
Hello Mia Walker,
As you said you are newbie so 1st you should have to learn about strings.
String is character arrays. like "char arr[50];" is a string. To display it we have a different method.
1st method:
use for loop:
for(int i=0;i<50;i++)
{
cout<<arr[i];
}
this is a lengthy method.
2nd method:
use string.h library,
and just write string name in
puts(arr);
this will also print the same thing.