Jan 24th, 2003, 03:21 PM
#1
Thread Starter
Frenzied Member
Combining strings for output
OK im reading a book called
C++ Weekend Crash Course
I know in c++ console to combine a string or seperate value's
you use
VB Code:
cout << nVal1 << " Is the same as "<< nVal2
But how do i do this in c++ window? Is it the same?
Also is it possible to make like a dos window? More specifically
is it possible to make a console sort of like a window? ill post a pic in this post to explain.
Thats the EDIT window or program in dos.
Attached Images
- JayWare
Live to love. Not to Hate
Im to busy to have a site. But I got one and still working on it.
http://dre3k.net/
Jan 25th, 2003, 09:36 AM
#2
cout << nVal1 << " Is the same as "<< nVal2;
That's a common misconception. You're not concatenating strings here. You outputting several strings and other values sequentially. They simply show up next to each other in the console window, but that's not really a necessity.
You can concatenate the string class using the + operator:
Code:
sting s1("Hello"), s2("World!"), s3;
s3 = s1 + ", " + s2;
cout << s3 << endl;
You can also build a string from strings and other data using the stringstream classes.
Code:
#include <sstream>
using namespace std;
ostringstream oss;
oss << "Hello, you're the " << nNum << "th visitor.";
cout << oss.str();
As for the edit app, you'll notice that "graphic" elements are really characters from the IBM charset (known as OEM charset in windows). So it's just good placement of characters (don't know how to color them though).
All the buzzt
CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Jan 25th, 2003, 02:39 PM
#3
Thread Starter
Frenzied Member
I cant get the below to work
VB Code:
sting s1("Hello"), s2("World!"), s3;
s3 = s1 + ", " + s2;
cout << s3 << endl;
i though you just made a typo on sting = string but still no good
VB Code:
string s1("Hello"), s2("World!"), s3;
s3 = s1 + ", " + s2;
cout << s3 << endl;
And the other one i tried but nothings working what did i do now?
VB Code:
#include <stdio.h>
#include <iostream.h>
#include <sstream>
using namespace std;
int main (int nArgs, char* pszArgs[])
{
int nNum = 2;
ostringstream oss;
oss << "Hello, you're the " << nNum << "th visitor.";
cout << oss.str();
return 0;
}
thanks and also how will this work in VC++. Win32 Application (window) or MFC
- JayWare
Live to love. Not to Hate
Im to busy to have a site. But I got one and still working on it.
http://dre3k.net/
Jan 25th, 2003, 02:44 PM
#4
You will also need to include the <string> header.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
Jan 25th, 2003, 02:46 PM
#5
Monday Morning Lunatic
Also, you must use <iostream> and <cstdio>. Actually, <cstdio> has no place in a C++ program, don't use it.
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
Jan 25th, 2003, 03:00 PM
#6
Thread Starter
Frenzied Member
ok i tried but no good.
VB Code:
#include <stdio.h>
#include <iostream.h>
#include <sstream>
#include <string>
using namespace std;
int main (int nArgs, char* pszArgs[])
{
string s1("Hello"), s2("World!"), s3;
s3 = s1 + ", " + s2;
cout << s3 << endl;
return 0;
}
- JayWare
Live to love. Not to Hate
Im to busy to have a site. But I got one and still working on it.
http://dre3k.net/
Jan 25th, 2003, 07:07 PM
#7
<iostream>, not <iostream.h>.
<iostream.h> is outdated.
All the buzzt
CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Jan 25th, 2003, 07:44 PM
#8
Thread Starter
Frenzied Member
- JayWare
Live to love. Not to Hate
Im to busy to have a site. But I got one and still working on it.
http://dre3k.net/
Jan 27th, 2003, 02:28 PM
#9
Thread Starter
Frenzied Member
ok how do i convert a
string <- to a -> char ?
- JayWare
Live to love. Not to Hate
Im to busy to have a site. But I got one and still working on it.
http://dre3k.net/
Jan 27th, 2003, 02:35 PM
#10
Monday Morning Lunatic
If you need it, you can get a const pointer using .c_str() on the string. If you want to actually get a copy, then you'll need to do:
Code:
char buf = new char[str.length() + 1];
strcpy(buf, str.c_str());
buf[str.length()] = 0; // null termination
// use buf
delete[] buf;
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
Jan 27th, 2003, 02:37 PM
#11
Thread Starter
Frenzied Member
ok i got confused in that peice of code where would i put my string?
(edited)
OK sorry i didnt study that to hard
const char *ptr1 = 0;
ptr1= vIni.data ( );
i used the above and got it thank you so much you people are great
Last edited by JasonLpz; Jan 27th, 2003 at 02:40 PM .
- JayWare
Live to love. Not to Hate
Im to busy to have a site. But I got one and still working on it.
http://dre3k.net/
Jan 28th, 2003, 06:07 AM
#12
parksie: strcpy automatically 0-terminates the string for you.
All the buzzt
CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Jan 28th, 2003, 06:22 AM
#13
Monday Morning Lunatic
Does it? Force of habit I think
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
Jan 28th, 2003, 06:33 AM
#14
The CRT string functions will happily create access violation due to buffer overflows, but they manage the NUL really well.
All the buzzt
CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Jan 28th, 2003, 06:37 AM
#15
Monday Morning Lunatic
I think it was about 5 years ago I got into the habit of scattering null terminators around my code just in case Haven't had a problem yet
(I really oughta start trusting the libraries...)
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