Click to See Complete Forum and Search --> : Strings SIMPLE !
WP
Dec 20th, 2000, 05:49 AM
I want to put some text in a string, I did the following:
char part1[1];
char part2[1];
char total[1];
int main()
{
part1 = "this is";
part2 = "a test";
total = part1 + " " + part2 //So total would be 'this is a test'
return 0;
}
But he gives errors on this!
How could it?
thanks,
WP
HarryW
Dec 20th, 2000, 06:25 AM
char is a datatype representing one character. A string is an array of characters, you have that bit right, but you need one array element for each character in the string. You need to declare your char arrays to be bigger.
Secondly, I think you have the wrong idea about how C handles strings. They are quite complicated compared to VB when you use char arrays and not some of the special string classes which simplify them. You can't just concatenate strings using the + operator. You need to use a function to do it, I have forgotten which, but it may be using strcopy().
part1 written just like that will return a pointer to the first element in the array, the same goes for part2, and similarly the string " " is just a pointer to that string literal within your executable. So you are assigning a pointer plus a pointer plus another pointer to the address of the array total, and you can't do that.
Also, you forgot a semicolon just before your comment.
WP
Dec 20th, 2000, 06:30 AM
WOW, did you see that! Thanks.
WP
I recomend using the STL string library....
#include <string>
using namespace std;
//do not include .h for the stl string library
int main()
{
string p1, p2, p3;
p1 = "this is";
p2 = "a test";
p3 = p1 + " " + p2;
cout << p3;
return 0;
}
there are quite a bit of functions for the string datatype..:
stringname.length(); //returns the length
stringname.c_str(); //returns the c_style string, with the null char at the end.
stringname.substr(s,f); //gets a substring from point s with
//the length of f
those are the most common ones..
and here is the header for the strcpy() function, you only use it for character arrays, or pointers to character arrays(which are essentially the same thing).
char *strcpy( char *strDestination, const char *strSource );
PS:
the reason + works with the string datatype, is because it is overloaded, so it can add strings.....
<edited: I made a little mistake when explaining the substr() function... but it's fixed now>
[Edited by denniswrenn on 12-21-2000 at 06:09 AM]
WP
Dec 21st, 2000, 04:27 AM
Thanks Denniswrenn, it worked!
WP
WP
Dec 21st, 2000, 06:31 AM
How can I output it in a console window?
cout<<p3; wouldn't work
WP
Vlatko
Dec 21st, 2000, 11:04 AM
stringname.c_str(); //returns the c_style string, with the null char at the end.
So you would use:
cout<<p3.c_str();
parksie
Dec 21st, 2000, 12:28 PM
WP - You can use
cout << string;
I expect you included <iostream.h>. If you use ANY of the STL then you have to use the STL iostream header, which is in <iostream> (without the .h). Then it'll work.
WP
Dec 22nd, 2000, 08:56 AM
It worked thank you, but I have 2 questions for you.
1) What's the difference between 'cout' and 'printf' ?
2) I received this error: 'First-chance exception in Tafels.exe: 0xC0000005: Access Violation.' how handle with this ? (not on your line, but in the begin of the prog)
WP
Vlatko
Dec 22nd, 2000, 09:42 AM
I think cout is the new way you should use to write to a console and printf was used in C. They alsi have different header files.
printf is in stdio.h, and cout is in iostream.h and I like cout a LOT better......... printf is a little too complicated to use for simple output.....
parksie
Dec 22nd, 2000, 12:06 PM
printf came from C. The format string was the only practical non-object-oriented method. In C++ the insertion operators can be used :).
WP - What source line is the error on?
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.