Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to cout a string??

Status
Not open for further replies.

newbieKing

Programmer
Jan 29, 2003
19
CA
I've been using Borland 5.0, and am trying to use Visual C++ 6.0.
outputting a string in borland is fine but i can't seem to cout strings in visual. Am i missing an include file??
This is what i've included:

#include <math.h>
#include <conio.h>
#include <iomanip.h>
#include <fstream.h>
#include <string>
#include <vector>
#include <stdlib.h>
#include <assert.h>
using namespace std;

void main()
{
string word = &quot;hello&quot;;
cout << word;
}

stirng2.cpp
c:\documents and settings\laurence\my documents\visual c++\first simulation\stirng2.cpp(15) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class s
td::allocator<char> >' (or there is no acceptable conversion)
Error executing cl.exe.

Please tell me what i'm missing.
Thank you!

 
try adding

[tt]#include <iostream>[/tt]
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
OK, either remove the

[tt]#include <fstream.h>[/tt]

or do this instead:[tt]

std::cout << word;[/tt]

The problem is the abiguity bwteen headers and namespace.
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Thanks, but it still doesn't work....


#include <math.h>
#include <conio.h>
#include <iomanip.h>

#include <iostream.h>
#include <string>
#include <vector>
#include <stdlib.h>
#include <assert.h>
using namespace std;

void main()
{
string word = &quot;hello&quot;;
std::cout << word;

}

--------------------Configuration: string - Win32 Debug--------------------
Compiling...
stirng2.cpp
c:\documents and settings\laurence\my documents\visual c++\first simulation\stirng2.cpp(19) : fatal error C1010: unexpected end of file while looking for precompiled header directive
Error executing cl.exe.

stirng2.obj - 1 error(s), 0 warning(s)
 
Why not use char* instead of string, that should do it
so you would have
char* word= &quot;hello&quot;
instead of string word= &quot;hello&quot;
 
it should be

[tt]#include <iostream>[/tt]

not

[tt]#include <iostream.h>[/tt]

tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Thank you for all your help! After a long time i finally figured out that i made 2 errors:
i should have put <iostream> and <iomanip> instead of <iostream.h> and <iomanip.h>
 
#include <iostream>
#include <string>

using namespace std;

int main()
{

string word = &quot;Hello!&quot;
cout << endl
<< word << endl;
cin.ignore();return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top