Click to See Complete Forum and Search --> : whats wrong here
nabeels786
Nov 10th, 2001, 07:54 PM
im trying out CString, just experimenting, and im going crazy. i've look at a couple of sites, and it's correct:
#include <cstring.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void main(void){
CString s1;
s1= "hello";
printf("%s",s1.Right(1));
}
The Errors:
--------------------Configuration: Cpp1 - Win32 Debug--------------------
Compiling...
Cpp1.cpp
C:\Nabeel\Projects\C++\gfxtest\Cpp1.cpp(7) : error C2065: 'CString' : undeclared identifier
C:\Nabeel\Projects\C++\gfxtest\Cpp1.cpp(7) : error C2146: syntax error : missing ';' before identifier 's1'
C:\Nabeel\Projects\C++\gfxtest\Cpp1.cpp(7) : error C2065: 's1' : undeclared identifier
C:\Nabeel\Projects\C++\gfxtest\Cpp1.cpp(8) : error C2146: syntax error : missing ';' before identifier 's2'
C:\Nabeel\Projects\C++\gfxtest\Cpp1.cpp(8) : error C2065: 's2' : undeclared identifier
C:\Nabeel\Projects\C++\gfxtest\Cpp1.cpp(8) : error C2228: left of '.Right' must have class/struct/union type
Error executing cl.exe.
Cpp1.exe - 6 error(s), 0 warning(s)
im going crazy over this!
then i tried converting to pointers, and still the same errors
abdul
Nov 10th, 2001, 08:05 PM
Try declaring the included file as:
#include <cstring>
#include <string>
#include <stdio.h>
#include <stdlib.h>
nabeels786
Nov 10th, 2001, 08:14 PM
still the same errors..
i think vc++ is on crack
transcendental
Nov 10th, 2001, 08:22 PM
I don't think you can printf a CString.
printf() is meant for null terminated strings.
Meanwhile I am looking the MSDN docs for you.
nabeels786
Nov 10th, 2001, 08:32 PM
thanks man,
i dont think thats the problem though., it says its missing a ';', when it isnt...and it says the vars aren't defined
transcendental
Nov 10th, 2001, 08:35 PM
You can use CString only in a MFC app.
I think you can only use null terminated strings. I don't think string class in #include<string> provide extracting strings from a string. You got to do it(the extracting) yourself
Meanwhile you can wait patiently for someone to help.;)
Me no string expert.
nabeels786
Nov 10th, 2001, 08:44 PM
only in an MFC app? darn...i dont know MFC yet...gotta start learnin more :(
--thanks for the help
CornedBee
Nov 11th, 2001, 05:31 AM
I think you can use CString in console apps by creating a "console app that supports MFC" (in the VC++ wizard). This will add a lot of overhead though.
To use it with printf you would need to explicitely cast to LPCTSTR:
CString str("Hello, World!");
printf("%s", (LPCTSTR)str.Right(1));
nabeels786
Nov 11th, 2001, 10:53 AM
yeah i made one that supports MFC and it worked.
ill try that other thingy later..mmm food
thanks :)
parksie
Nov 11th, 2001, 12:07 PM
If you want to get a substring out of a C++ string, then use the .substr(...) method:#include <iostream>
#include <string>
int main() {
string x = "Hello there!";
cout << x.substr(x.length() - 1, 1) << endl;
return 0;
}
nabeels786
Nov 11th, 2001, 05:58 PM
okiedokie..
i like my CString..it has the Left(), Right() and Mid() funcstions to make to happy :)
parksie
Nov 12th, 2001, 12:28 PM
You can synthesise Left and Right simply enough, and Mid == substr :D
Anyway, why use CString when you can easily use string, and then you've got full standard library compatibility? For example, you can't do this with a CString:cout << my_c_string << endl;...without writing your own inserter.
nabeels786
Nov 12th, 2001, 05:53 PM
do u have any tutorials? i'm willing to learn it...
thanks :)
parksie
Nov 13th, 2001, 11:12 AM
What's to learn? :)string x = "Hello";
string y = x + " " + "World!";
string z = string("This ") + x + " is an example";For z, note that the first This needs to be inside a string constructor, otherwise when the compiler resolves the right hand side it tries to add two pointers together, which is illegal. Forcing it into a string makes it all resolve properly :)
And then you've got .substr, which is like Mid().
There's no "Format" like with CString, but you can synthesise it easily enough using vsnprintf. I don't have the code at work, but when I get home if you've replied by then I can send it.
nabeels786
Nov 13th, 2001, 04:08 PM
well the string things i needed/wanted were like "Left", and "Right"
parksie
Nov 13th, 2001, 04:13 PM
That's my point, they're simply applications of Mid or substr.
nabeels786
Nov 14th, 2001, 10:27 AM
i was wondering though,
is there a tutorial? im playing around with string.h now..
parksie
Nov 14th, 2001, 11:41 AM
Ah. string.h != string.
string.h contains the C string handling functions, which you're likely to need a tutorial for (or just look at the MSVC docs). string has the C++ Standard Library's string class, and that's pretty easy to use.
Actually, the MSVC docs have info on the string class as well - they tell you everything you need to know.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.