|
-
Oct 6th, 2004, 07:23 AM
#1
String/Char problems ....again...
When am i going to learn this....
I am trying to add one and one char to a char array. And then print it out using TextOut(...). But I have problems trying to add them. I tried using sprintf, but it adds a " " char in start and **** up everything if I do it like this.
Code:
//cText is a double array [20][256]
//and teksten is just a single char.
sprintf(cText[iAntTekst], "%c%c", cText[iAntTekst], teksten);
Anyone know how to fix this. There is one things I hate in C++ and that is strings....never handeled them very well...
-
Oct 6th, 2004, 12:50 PM
#2
First, why do you use C strings in C++?
Second, obviously it is not going to print a proper char if what you pass is a char[256]!
sprintf(cText[iAntTekst], "%c%c", cText[iAntTekst], teksten);
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.
-
Oct 6th, 2004, 02:01 PM
#3
try specifiying the first argument as a string:
Code:
sprintf(cText[iAntTekst], "%s%c", cText[iAntTekst], teksten);
(or)
Code:
int i = strlen(cText[iAntTekst]);
cText[iAntTekst][i] = teksten;
cText[iAntTekst][i + 1] = '\0'
anyway, the std::string class would be a lot easier.
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.
-
Oct 6th, 2004, 03:52 PM
#4
Originally posted by CornedBee
First, why do you use C strings in C++?
Second, obviously it is not going to print a proper char if what you pass is a char[256]!
sprintf(cText[iAntTekst], "%c%c", cText[iAntTekst], teksten);
First of all I told you I know nothing about strings in C/C++, it was my teacher that showed me the sprintf function, but I later found out that I had to add letters to the string. And then everything was ****ed....I am a graphics guy, not a string guy....guid me...
-
Oct 6th, 2004, 04:00 PM
#5
Originally posted by sunburnt
try specifiying the first argument as a string:
Code:
sprintf(cText[iAntTekst], "%s%c", cText[iAntTekst], teksten);
(or)
Code:
int i = strlen(cText[iAntTekst]);
cText[iAntTekst][i] = teksten;
cText[iAntTekst][i + 1] = '\0'
anyway, the std::string class would be a lot easier.
Well I don't have the code here, so I can't test the first one, I will do it at school tomorrow tho'. But I ended up doing it like in the second example there, but it looks like it is so much work to do so little. Just wanted to find a nice add a char to my string/char table. I have never worked with text at all, so consider me as a new guy here. Well the TextOut() function want a LPCTSTR, and I think that is *TCHAR. So I have no idea what a string actualy is in C++...for me everything looks like a char array...
-
Oct 6th, 2004, 05:32 PM
#6
a LPCTSTR is a long pointer to a const TCHAR -- ie const TCHAR*. a TCHAR is just a #define that is either char or wchar_t depending on whether or not you are compiling using unicode.
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.
-
Oct 7th, 2004, 12:07 AM
#7
OK.....so did anyone wanted to tell me what a string was? And what that easy way was?
Thanks
-
Oct 7th, 2004, 02:56 AM
#8
A string in its simplest meaning (but not simplest usage) is just that: a string of characters, or sequence of characters. Somewhere in memory there's a continuous block filled with valid numbers from some character set, and that's a string. Usually, it's a NUL-terminated string, meaning that the last character has the value 0 to indicate the end of the string.
Ok, the way in C to achieve that was through
1) A string literal. "Hello" makes the compiler create that character sequence. Usually this is read-only memory.
2) A character array: char buf[100]; creates a space in memory large enough to hold 100 characters. That's 99 usable characters, because the terminating 0 needs a space.
3) malloc, which creates space on the heap.
In cases 1 and 3, a character pointer was used to refer to the memory.
const char *lit = "Hello";
char *bla = malloc(100);
The big problem with C strings is that you have to do the memory management yourself. A slightly smaller problem is that you have to use weird functions (strlen, strcpy, strcat, sprintf) to work with the strings.
In C++, we have classes, and suddenly we can do very pleasant stuff.
Code:
#include <iostream>
#include <sstream>
#include <string>
using std::string;
using std::cout;
using std::endl;
using std::ostringstream;
#include <windows.h>
int main()
{
string str = "Hello"; // Create a string.
str += ", World!"; // Append a string
str += '\n'; // Append a single character.
// Do some weird formatting.
int i = 300;
float f = 82.48001f;
ostringstream oss;
oss << "The integer is " << i << " and the float is " << f << ".";
str += oss.str();
// And now pass the whole thing to MessageBox:
MessageBox(0, "Caption?", str.c_str(), 0);
}
You can use std::string's c_str() whereever a LPCTSTR is expected, such as TextOut - though strictly seen, you should use something a little different, but let's not go there.
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.
-
Oct 7th, 2004, 04:16 AM
#9
Well thanks for that tutorial...that was very helpfull. Sorry I don't have the time to test all of that right now. But I will do it when I have time and get back to you.
Thanks.
ØØ
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
|