|
-
Dec 20th, 2000, 06:49 AM
#1
Thread Starter
Hyperactive Member
I want to put some text in a string, I did the following:
Code:
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
-
Dec 20th, 2000, 07:25 AM
#2
Frenzied Member
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.
Harry.
"From one thing, know ten thousand things."
-
Dec 20th, 2000, 07:30 AM
#3
Thread Starter
Hyperactive Member
WOW, did you see that! Thanks.
WP
-
Dec 20th, 2000, 08:04 AM
#4
I recomend using the STL string library....
Code:
#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..:
Code:
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).
Code:
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]
-
Dec 21st, 2000, 05:27 AM
#5
Thread Starter
Hyperactive Member
thanks
Thanks Denniswrenn, it worked!
WP
-
Dec 21st, 2000, 07:31 AM
#6
Thread Starter
Hyperactive Member
BUT
How can I output it in a console window?
cout<<p3; wouldn't work
WP
-
Dec 21st, 2000, 12:04 PM
#7
Frenzied Member
stringname.c_str(); //returns the c_style string, with the null char at the end.
So you would use:
-
Dec 21st, 2000, 01:28 PM
#8
Monday Morning Lunatic
WP - You can use
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 22nd, 2000, 09:56 AM
#9
Thread Starter
Hyperactive Member
Thanks
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
-
Dec 22nd, 2000, 10:42 AM
#10
Frenzied Member
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.
-
Dec 22nd, 2000, 11:49 AM
#11
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.....
-
Dec 22nd, 2000, 01:06 PM
#12
Monday Morning Lunatic
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?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|