Jump to content

question for all C++ programmers


tamir
 Share

Recommended Posts

I tried alot but i don't understand how to use variables in expressions when i create application project in C++.

you probably didn't understand my problem, but it will help alot if you can show me how to create simple MessageBox and make it contents: "hello world" but make the "world" read from a variable.

here's what i mean: (in autoit)

$var = "world"
MsgBox(0, "", "hello " & $var)

so make this in C++...

thanks in advance for the helpers!

Link to comment
Share on other sites

You could use the sprintf() function first to build the desired message, then call MessageBox() with your resulting string.

Be sure that your string buffer is large enough to hold any possible value in your "world" variable.

Yes yes yes, there it was. Youth must go, ah yes. But youth is only being in a way like it might be an animal. No, it is not just being an animal so much as being like one of these malenky toys you viddy being sold in the streets, like little chellovecks made out of tin and with a spring inside and then a winding handle on the outside and you wind it up grrr grrr grrr and off it itties, like walking, O my brothers. But it itties in a straight line and bangs straight into things bang bang and it cannot help what it is doing. Being young is like being like one of these malenky machines.

Link to comment
Share on other sites

thank you all!! now i understnad how it works :)

Larry, in your example you wrote (char*)"Hello", what (char*) means?

and besides, i learned that * declares a pointer, but i see in many places that it has other uses - what are they?

Link to comment
Share on other sites

and besides, i learned that * declares a pointer, but i see in many places that it has other uses - what are they?

<{POST_SNAPBACK}>

placing * infront of a pointer tells it to use the contents of the pointer.

Good for testing strings before using them:

int CheckAString(char *teststring)
{
   if(!teststring) return -1;
   if(*teststring)
   {
      printf("the string is <%s>\n",teststring);
   }
   else
   {
      printf("string was empty\n");
   }
   return 0;
}

If you dont do the first if statement, calling this function with

CheckAString(NULL);

will halt the program when it runs

The second if is checking if there is anything in the string

Start -> Programs -> AutoIt v3 -> AutoIt Help File -> Index -> (The Function you are asking about)----- Links -----DllStruct UDFsRSA Crypto UDFs
Link to comment
Share on other sites

thanks for the explanation :)

i have another question:

when i declare a function ( void Func(...) {} ) what to write instead of the "..." so i can use it like this: Func("text"); ?

Edited by tamir
Link to comment
Share on other sites

thanks for the explanation  :)

i have another question:

when i declare a function ( void Func(...) {} ) what to write instead of the "..." so i can use it like this: Func("text"); ?

<{POST_SNAPBACK}>

void Func(char *variable)

{

// play with variable

}

Start -> Programs -> AutoIt v3 -> AutoIt Help File -> Index -> (The Function you are asking about)----- Links -----DllStruct UDFsRSA Crypto UDFs
Link to comment
Share on other sites

what pointers have to do with it? and why it can't be just (char var) ?

<{POST_SNAPBACK}>

Because of the way C deals with strings (a special case of its pointer/array approach).

char var
denotes a one-byte unsigned integer variable.

char* var
points to a one-byte unsigned integer variable. This variable is the first character in a C string. The string consists of this var and all bytes at subsequent memory locations until there is a byte having the value 0 (This can probably be said more precisely, but I hope it is clear what I mean). This is called a 0-terminated string.

The uninitiated should stay away from C strings, arrays and pointers. In this day and age the only reason to deal with C is if you're programming embedded devices. There are many pitfalls, e.g. referencing an array out of bounds.

The string class from the C++ standard library is more intuitive & therefore easier to work with. And if you somehow need the string in C format (ie. a char*), use string's c_str() method.

If you insist on using C strings directly, you had better read Kerningan & Ritchie's "The C Programming Language". I think the 2nd edition is the current one. Be prepared for headaches.

Ignorance is strength.

Link to comment
Share on other sites

A good question. With a few exceptions, valid C code is also valid C++ code. The question I answered had to do with the C style strings (already the first reply in this thread referred to a C approach, namely using sprintf()).

This is what you asked for in the initial post:

#include <string>
using namespace std;

int main()
{
    const string hello("Hello ");
    string world("world");
    <MESSAGEBOXFUNCTION>((hello + world).c_str());
}

Instead of <MESSAGEBOXFUNCTION> you must insert whatever the message box function is called in your programming environment. C++ does not have built in message boxes, but many C++ tools have.

The example was made with Visual C++ 6.0 as a Win32 console application. I removed a bunch of VC++ autogenerated code to show only the code pertinent to your question (the message box function I used is called AfxMessageBox and takes a char* argument).

Ignorance is strength.

Link to comment
Share on other sites

but u talking about VC++, it's not the same as C++, right?

because there is no "string" structure in C++

<{POST_SNAPBACK}>

I strongly urge you to find some books in whatever manner you can (Purchase, library, download, et cetera). Your fumbling around completely in the dark without proper materials and asking some extremely basic questions that could be answered if you had the appropriate material. You will get nowhere very very slowly if you attempt to learn C++ in the manner you currently are (i.e. asking basic questions on a forum). If you want to learn C++, you'll get books, otherwise, you're wasting enormous amounts of your time and small amounts of time for the people who attempt to help you.
Link to comment
Share on other sites

I strongly urge you to find some books in whatever manner you can (Purchase, library, download, et cetera).  Your fumbling around completely in the dark without proper materials and asking some extremely basic questions that could be answered if you had the appropriate material.  You will get nowhere very very slowly if you attempt to learn C++ in the manner you currently are (i.e. asking basic questions on a forum).  If you want to learn C++, you'll get books, otherwise, you're wasting enormous amounts of your time and small amounts of time for the people who attempt to help you.

<{POST_SNAPBACK}>

yea, use me as an example though :)
FootbaG
Link to comment
Share on other sites

but u talking about VC++, it's not the same as C++, right?

because there is no "string" structure in C++

<{POST_SNAPBACK}>

The C++ Standard Library (aka STL), which includes the string class, is part of the ANSI C++ Standard. Using C++ without STL is like having sex without a partner. You can make it work, but it will never be great.

This thread has lots of links & refs to books & other resources. To name a few: C++ FAQ Lite will answer many newbie questions & stay useful even when you become proficient in C++. "The C++ Programming Language" by Bjarne Stroustrup is indispensable, though in my case I had to work with C++ for some months before I was able to grok that book.

Here's a quote from the C++ FAQ Lite:

Some people never make it. You don't have a chance unless you are teachable and have personal drive. As a bare minimum on "teachability," you have to be able to admit when you've been wrong. As a bare minimum on "drive," you must be willing to put in some extra hours (it's a lot easier to learn some new facts than it is to change your paradigm [i.e., to change the way you think; to change your notion of goodness; to change your mental model of the world of technology">

).

Ignorance is strength.

Link to comment
Share on other sites

actually i've looked a little in tutorials and stuff in the internet and i pretty much understand the questions i asked.

i learned few programming languages before, and in most of them i had stupid questions at the start but after like 1/2 weeks i looked at them and couldn't believe i asked them...

so far i didn't had to buy a book, just read few articles, build some programs and everything run smoothly.

the main problem is that in all the languages i learned, there were no different data types, so i got a little confused when i first saw it.

i think i'm doing pretty well (i learned alot since last post) and i hope this thread won't have any more questions from me :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...