printf和string s [英] printf and string s

查看:71
本文介绍了printf和string s的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在尝试在屏幕上打印一个字符串...但该字符串来自一个

变量字符串。 ..这是代码


#include< cstdlib>

#include< iostream>

#include< string>


使用命名空间std;


int main(int argc,char * argv [])

{

string t =" tes";

printf("%s",t);

system(" PAUSE" );

返回EXIT_SUCCESS;

}

但这不起作用....程序在执行printf时崩溃...

有谁知道如何解决这个问题?

Hello all,

I''m trying to print a string on my screen... But the string comes from a
variable string... This is the code

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
string t = "tes";
printf ("%s", t );
system("PAUSE");
return EXIT_SUCCESS;
}
But this isn;t working.... the program is crashing when doing printf...
anyone know how to fix this ?

推荐答案

" Joah Senegal" < bl **** @ hva.nlwrites:
"Joah Senegal" <bl****@hva.nlwrites:

> Hello all,
>Hello all,


>我正在尝试在屏幕上打印一个字符串......但字符串来自一个变量字符串...这是代码
>I''m trying to print a string on my screen... But the string comes from a
variable string... This is the code


> #include< cstdlib>
#include< iostream>
#include< string>
>#include <cstdlib>
#include <iostream>
#include <string>


> using namespace std;
>using namespace std;


> int main(int argc,char * argv [])
$

printf("%s",t);

system(" PAUSE");

返回EXIT_SUCCESS ;
}
>int main(int argc, char *argv[])
{
string t = "tes";
printf ("%s", t );
system("PAUSE");
return EXIT_SUCCESS;
}


>但这不起作用....程序在执行printf时崩溃...
>But this isn;t working.... the program is crashing when doing printf...
anyone know how to fix this ?



printf'期待一个C风格的char数组string。给它想要的东西

使用

printf("%s",t.c_string());

或(更好)使用C ++的

cout<< t;

printf''s expecting a C-style char-array "string". Give it what it wants
by using
printf ("%s", t.c_string());
or (better) use C++''s
cout << t;


Joah Senegal在14/09/2006 14:44发表以下内容:
Joah Senegal said the following on 14/09/2006 14:44:

大家好,


我正在尝试在屏幕上打印一个字符串......但字符串来自一个

变量字符串..这是代码


#include< cstdlib>

#include< iostream>

#include< string> ;


使用命名空间std;


int main(int argc,char * argv [])

{

string t =" tes";

printf("%s",t);

system(" PAUSE") ;

返回EXIT_SUCCESS;

}
Hello all,

I''m trying to print a string on my screen... But the string comes from a
variable string... This is the code

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
string t = "tes";
printf ("%s", t );
system("PAUSE");
return EXIT_SUCCESS;
}



您尝试使用std :: string和printf。


printf是库C的一个函数现在,你可以用t.c_str()来实现它,但是它不是很好的c ++符合。


试试这段代码:


#include< iostream>

#inclu de< string>


int main(int argc,char * argv []){

std :: string t =" tes" ;;

std :: cout<< t;

返回EXIT_SUCCESS;

}

You try tu use std::string with printf.

printf is a function of library C

now, you can do it with t.c_str(), but it''s not very c++ compliant.

try this code:

#include <iostream>
#include <string>

int main( int argc, char *argv[] ) {
std::string t = "tes";
std::cout << t;
return EXIT_SUCCESS;
}


Joah Senegal写道:
Joah Senegal wrote:

>

#include< cstdlib>

#include< iostream>

#include< string> ;


使用命名空间std;


int main(int argc,char * argv [])

{

string t =" tes";

printf("%s",t);
>
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
string t = "tes";
printf ("%s", t );



cout<< t<< ENDL;

cout << t << endl;


system(" PAUSE");

返回EXIT_SUCCESS;

}
system("PAUSE");
return EXIT_SUCCESS;
}



如果你*必须*使用printf,那么传递t.c_str(),你必须包括

< cstdio>。 std :: string不会自动转换为char *。为什么

你是否包括< iostream如果你不打算使用它?

If you *must* use printf, then pass t.c_str(), and you must include
<cstdio>. A std::string does not automagically convert to a char*. Why
did you include <iostreamif you weren''t going to use it?


这篇关于printf和string s的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆