C/C++ >> Strings in C++
Posted by _Paladin_ on 11:47:00 06-20-2001
hi,

at the moment i'm trying to switch from VB to C++... i'm using MS VC++ 6, and i'm wondering how to join two or more strings.

in VB i would just write:

strEnd = strOne & strTwo

how is it done in C++?

btw, converting an int into a string has stomped me three (!!!) days! i'm kinda tired of C++

please help

Paladin
Posted by SilentStrike on 15:53:00 06-20-2001
The easiest way to do this is like this. The hardened C guys will probably not like this way, cause it doesn't use the harder to understand and use built in character arrays, but coming from VB, this is a lot more friendly.

#include <string> //So we can use string datatype
#include <iostream> //For output
#include <stdlib.h> //For atoi (string to integer conversion
#include <stdio.h> //For getch
#include <conio.h> //For getch

using namespace std; //Probably haven't learned this yet. It let's use the string and cout... notice it's not iostream.h

int main()
{
string string1 = "1";
string string2 = "2";
string stringSum = string1 + string2;

//string1.c_str() returns the a character array version of the string, needed so atoi works, as
//atoi takes a character array as a parameter, thus, the conversion actually goes like this
//string -> char array -> int

int num1 = atoi(string1.c_str() );
int num2 = atoi(string2.c_str() );
int numericalSum = num1 + num2;

cout << "The string concatenation of " << string1 << " and " << string2 << " is " << stringSum << endl;
cout << "The numerical sum of " << num1 << " and " << num2 << " is " << numericalSum << endl;

getch(); //To pause program before exiting so I can see the output... you don't need this when using MSVC

return 0;
}



Posted by robost86 on 22:01:00 06-20-2001
There are also some other functions you may want to know about. atof() converts a string to a float, in BASIC integers and floats are the same type, but in C++ they're different, so you also need to know about that.

"The hardened C guys will probably not like this way, cause it doesn't use the harder to understand and use built in character arrays, but coming from VB, this is a lot more friendly." I have a strange feeling SilentStrike is talking about me there . (for everybody wondering what the hell I'm talking about, I and SilentStrike had a little discussion High level languages vs. Low level languages) .

I agree, the C++ strings and I/O functions are more BASIC-like than the old C functions. What's good with C++ is that you can choose. Fore some tasks, you don't need the low-level stuff, but if you do, you can do almost whatever you want .

VB (and Delphi, that I use for Windows programming) aren't very powerful compared to C++ (or C, or Assembler, or any real language).

One tip when you learn C++ is to code alot. Do small and funny DOS programs to learn everything, and maybe a year or two after that, you're ready to do Windows (and other advanced) programming in C++ (if that's what you want to do).

For small DOS programs, I don't recommend Miscrosofts Visual C++ (C) Microsoft Corporation, download Turbo C++ from this link instead. It's funnier than MSVC++, because you have more functions to play around with =). The programs made by this compiler can also run in DOS, MSVC++ can't make DOS programs (that run in plain DOS).

/ Robert =)
Posted by robost86 on 22:05:00 06-20-2001
Oh, one more thing. The Borland C++ 1.01 is free (is MSVC++?), and takes 2.5MB, a little less than VC++ =)

You can also convert to Linux and use GCC =).
Posted by robost86 on 22:13:00 06-20-2001
Oh, two more things.

1) This is post 200 for me
2) Welcome, Palading
Posted by SilentStrike on 02:15:00 06-21-2001
MSVC can make console applications, though I've never tried running them straight through DOS, you don't have to write Win32 code to get the basic console.

I am not so sure about Borland being easier either. MSVC supports things like word finishing, syntax highlighting, auto-indenting, descriptive text thing (type in say, strcat, and it will display the parameters, one const char array, and one regular char array). Likewise, to compile and run with MSVC, all one needs to do is hit control + F5. It's the most friendly IDE I've ever used. Borland is a command line compiler with no IDE right? I prefer MSVC.

Also, if he has it already, price means nothing.

The C++ vs C thing was a post for pretty much the whole community... cause I feel as though I am the only person who would rather code in C++ than C.
Posted by fabs on 06:54:00 06-21-2001
Hey Paladin
You that guy from over at Lucky's VB-Gaming site? You know you helped me out on VB-Gaming once
About joining strings:
http://www.delorie.com/djgpp/doc/libc-2.02/libc_640.html
The function is called strcat and is inside string.h
fabs
Posted by robost86 on 10:01:00 06-21-2001
Console applications are Windows programs using a DOS Window. They are not DOS programs, you can not do DOS programs in VC++.

Turbo C++ has an excellent IDE, and a built-in help, very easy to use. Just press CTRL+F1 when you have the cursor over a keyword, and you get instructions of how to use, tips, return values, description of parameters etc. You can also do this with regular keywords, like switch, case etc., if you have forgotten how to use one of these.

It IS a good IDE, but for some reason, I don't it. I write in DOS edit, and compile with the commandline compiler. If I need help, I use HelpPC, a good reference for C, Assembler and some other PC-related topics.

Borland's IDE makes it very easy to compile and run. Just press CRTL+F9 to run the program.

Of course price means nothing for Paladin, I am just thinking about new users when I tell about free compilers.

Well, I don't care if you prefer writing in C++, I just think everybody should know C. That makes you a better C++ programmer I think. If you think it's better to write in C++, nothing wrong with that, it's often better than plain C.
Posted by _Paladin_ on 13:06:00 06-22-2001
of course this maybe very very newbyish to robost, but at the moment i'm pretty sure, that VB is the best programming language to create stable windows based application in a very short time! you might say that VB isn't a real language, but i just loved it... its simplicity for example. i don't want to start a flame war, and i'm not going to answer any more posts about C/C++ or ASM rulez. of course you are right, but VB helped me a lot on learning to programm. because it's object oriented (at least a bit )...

i'm using MSVC++, because it was the first C++ compiler i could get... of course i didn't pay for it i didn't pay for VB either... but i would never have started programming, when i would have had to buy these tools!

i also downloaded Visual Assist from WholeTomatoSoftware... it enhances the auto word completition of MSVC++, and does a lot more things to it... now VC++ looks more and more like VB and that's just what i need...

but why am i using C++ at all?
some time ago i started to learn DX for VB... i wrote a terrain/landscape engine, that renders outdoor environments, but it's so slow with VB! that's the biggest downside of VB! i also learned some OpenGL and now i'm trying to port that small engine from VB & DX to C++ & OGL...

so wish me luck )

to fabs: well, yes i'm that paladin from lucky's board... i bookmarked the link to this page some time ago, and now i'm here. in fact i was looking for a page, were i can ask lots of newbie question about C++, without disturbing any pros

Paladin

PS: under my name thare stands "Newbie" ) is this rank given by the moderators, or by the board? will it leave, when i post more? just curious...
Posted by fsvara on 13:30:00 06-22-2001
"Newbie" that's an automatic ranking given by the board based on how much you ahve posted =)

How many posts you need for which rank is explained in the board's FAQ.
Posted by robost86 on 16:58:00 06-22-2001
Hmm... Sorry if I said something that made you angry in any way. I don't say VB is bad, I say Delphi is better. I've tried VB too, but I find Delphi better. Creating programs in Delphi is also a very easy and a very fast way to do Windows programs. btw, I don't see BASIC as a 'non-real' language, I often use BASIC myself (QB). Delphi uses object pascal, kind of C++ with Pascal syntax, and you can do everything you can do in VB in Delphi.

Not all kinds of programs can be written in VB, and VB programs are slow. For some programs, VB is good, for some programs, C++ is good, and for some program assembler is good.

If you learn C++, you can do almost anything.

You say you use MSVC++ because that's your first compiler... My first compiler was DJGPP, but now I use several other compilers and not DJGPP. Turbo C++ can only do DOS programs, MSVC++ can only do windows programs. If you want to do DOS programs, you need both.
Posted by robost86 on 17:02:00 06-22-2001
Btw, you never have to pirate Borland products, they are giving them away for free. I have got Turbo C++ 1, Borland C++ 5.5, Delphi 3 professional, and lots of other stuff for free from Borland.
Posted by _Paladin_ on 17:15:00 06-22-2001
hmmm... well, i wasn't really angry...

i'd really liked to learn delphi at school... when i was new there, they had delphi on every comp. but because most others in my class don't even understand the logic behind a program, we never got away from pascal. most of them didn't understand a thing...

Paladin
Posted by _Paladin_ on 17:21:00 06-22-2001
now, that i got your attention again could you please show how to get the current SystemTime?

i tried GetSystemTime...

but i can't get the Time into a string! i hate C++ string!
every time i try to put at least the wHour Member of the LPSYSTEMTIME structure into an int variable windows throws some freaky exception faults at me

man, i'd really like to learn more about strings, but where?

Paladin
Posted by SilentStrike on 19:16:00 06-22-2001
Here is some general information on the standard string class.

http://www.msoe.edu/eecs/ce/courseinfo/stl/string.htm
Posted by robost86 on 19:24:00 06-22-2001
LPSYSTEMTIME isn't a struct, it's a pointer to a struct. Use SYSTEMTIME instead, like this:

SYSTEMTIME Time;
char TimeStr[80];

GetSystemTime(&Time);
sprintf(TimeStr,"Hour: %u, Second: %u\n",SYSTEMTIME.wHour,SYSTEMTIME.wSecond);

That should work =)
Posted by _Paladin_ on 11:22:00 06-23-2001
thanks for da help

Paladin