Advertisement

Why do strings not work in VC++?

Started by May 22, 2000 04:46 AM
9 comments, last by Possibility 24 years, 6 months ago
In my C++ class working on Linux workstations, we use strings: #include iostream #include string and could do such things as string test("Testing 1 2 3"); but, even if I include string in my game written in VC++, string doesnt turn blue like an int or double does, and all I does it get errors from it. Whats up with that? Possibility Edited by - Possibility on 5/22/00 4:55:33 PM
It''s because it isn''t one of the basic types, you don''t have to include anything to define a type of int, char, float etc... String is a template class.
"I have realised that maths can explain everything. How it can is unimportant, I want to know why." -Me
Advertisement
In your class at school you are using a string class which is not part of the c++ keywords (that''s why it doesnt go blue). To use the String class in VC++ you have to have a look in the MSDN docs. Or you can just use an array of chars, like:

char mystring[40];

hope that helps.



- Daniel
my homepage
- DanielMy homepage
Try this
#include <iostream>
#include <string>
using namespace std;

Then
string test("Testing 1 2 3");
cout << test << endl;
should work, but no, it won't turn blue on you

Edited by - Armitage on May 22, 2000 7:31:58 AM
A polar bear is a rectangular bear after a coordinate transform.
Heheh actually you can make it turn blue, by adding it to the user-defined keywords file, but that's a bit too much effort for me, 'cause I'm a lazy bugger basically.

Here's how to do it ( for Visual Studio, obviously ):

To set syntax coloring for user-defined keywords

In the same directory as msdev.exe, use the Text editor or Windows Notepad to create a text-only file named usertype.dat.


Add your keywords to usertype.dat, one keyword per line.
Note The usertype.dat file is read during initialization. It cannot be renamed, nor can it be reloaded during an editing session. The syntax coloring mechanism checks the usertype.dat file last. Thus, all previously defined color settings take precedence over the user-defined keywords.

Save the file as a text-only file, exit, and then restart Visual Studio.


From the Tools menu, choose Options.


Select the Format tab.


In the Colors box, select User Defined Keywords.


Set the font, font size, foreground color, and background color to your preferences.

Click OK.


Easy isn't it?

#pragma DWIM // Do What I Mean!
~ Mad Keith ~

Edited by - MadKeithV on May 22, 2000 8:30:12 AM
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
Oops, in the #includes i wrote above, nothing appeared after them
I did include #include <"iostream"> and #include <"string"> but that still gave me errors. And as my C++ book said, i should also have 'using namespace std' in there, but then that creates a gazzillion errors in my code. So I guess I am just stuck with using Char[number], but I dont like this because I cant make it variable. I need to have variable strings, because them char arrays are variable. What I do alot in my code is:

int number = 1234;
char char_number[5];
sprint(char_number, "%d", number);

to convert ints and doubles ect... to char arrays so i can print them out using my font. But the number can be very different in size through each loop of the game. So how do I create a nice variable length string? The stupid 'string' doesnt want to work at all.

Possibility
P.S. What the fuck, when I do stream with greater and less then signs around it, it makes it dissapear.
I had to put in quotes to make them show up, but I know they dont go in there for real.

Edited by - Possibility on May 22, 2000 5:47:32 PM

Edited by - Possibility on May 22, 2000 5:49:06 PM

Edited by - Possibility on May 22, 2000 5:49:42 PM

Edited by - Possibility on May 22, 2000 5:50:44 PM

Edited by - Possibility on May 22, 2000 5:53:49 PM
Advertisement
Make sure you have Service Pack 3 for Visual Studio, as it fixes quite some STL related problems. Also check www.dinkumware.com for updates to the STL that comes with VC++ 6.

If using namespace std; gives problems, you can also use namespace resolution with each string. So instead of string, you''d use std::string. It takes some more typing, but you won''t end up with a polluted namespace because everything from the C++ header files is imported.

string is so much easier than old C-strings, everybody should use them

Erik
Possibility, the statement "using std::string" should bring the string class into the global namespace without bringing anything else in, so you shouldn''t have a lot of errors.

And if you type the < and > signs directly, it translates whatever is between them as an HTML tag, which doesn''t get shown. Instead, type &lt; and &gt; to do them.
kool, thanks kylotan.

Possiblity
strings work just fine in VC++...

char buf[256];
sprintf(buf, "Hello there %d", yourName);
printf("%s", buf);

etc.

This topic is closed to new replies.

Advertisement