It is currently Tue Apr 16, 2024 9:56 am



Reply to topic  [ 31 posts ]  Go to page Previous  1, 2, 3  Next
I'm learning c++ 
Author Message
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
derf wrote:
*fists GFreeman*

:lol:

I hate Flamin' Fist's pansies. They almost allways miss :P ... well Freddie, old buddy, maybe you'll once take from me a good fisting lecture you ever dreamed of :lol:

What to say Satis, imo - C++ rocks. If you put here some of your doubts, maybe you'll receive the solutions of them.

_________________
++


Thu May 26, 2005 10:44 am
Profile WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16650
Location: On a slope
Reply with quote
Post 
nah, I'm sure I just lack the knowledge so far. My two major complaints right now:

1. I can grab environment variables.

char * thevar = getenv("homepath");

but it puts it in a char. I don't know how to easily manipulate chars. A string is great to manipulate.

thevar = thevar + " " + "whatever the hell I want" + anothervar;

can't do that with a char. I know there are some concat functions and whatnot, but they're really limited. And I don't know how to convert from char to string.

2. switch case and strings.....

they don't work! how irritating:
ie

Code:
string error_message("An error message");
switch(error_message){
                case "An error message":
                             cout << "Something broke!";
                             return 1;
                             break;
                case "Normal Program Termination":
                             cout << "You quit";
                             return 0;
                             break;
}


Either I'm doing somethign wrong/misreading error message, or swtich/case can only use single char or integer inputs.

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Thu May 26, 2005 11:14 am
Profile WWW
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
It was your 4200th post. :)

1. Really don't see a problem.

Code:
string x = getenv("homepath");


2. Lol, yup that won't work. Why you wouldn't try this:

Code:
#include <iostream>
using namespace std;

enum error_message {

   An_error_message,
   Normal_Program_Termination

};

main()
{
   error_message x = An_error_message;
   
   switch( x )
   {
      case An_error_message:
         cout << "Something broke!";
         return 1;

      case Normal_Program_Termination:
         cout << "You quit";
         return 0;
   }

   return 0;
}


or just this

Code:
#include <iostream>
using namespace std;

#define An_error_message 0x00f
#define Normal_Program_Termination 0x010

main()
{
   int x = An_error_message;
// ...   

Anyway, in ANSI/ISO C++ anything (so break as well) after return is actionless.

_________________
++


Thu May 26, 2005 11:34 am
Profile WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16650
Location: On a slope
Reply with quote
Post 
as for the error message thing...

it'd just be nice to be able to define the full text of the error message at the time I create the error exception. I can easily make up cryptic numeric error codes and be done with it, but the whole going back and cross-referencing error codes with their meanings is a pain in the ass I try to avoid. AT this point, the program's small, so it's no big deal. I'm speaking more from my php experience.

Anyway, the enum thing looks promising, though it would still require me to define my error message separate from where I actually cause it. Bleh, anyway, better than an error code.

As for the environment thing, the big reason for the pain in the ass is that I don't know how to take a char and easily use it for other stuff. something along these lines is what I'm really shooting for.

char * thepath = getenv("homepath");
thepath = thepath + "local settings\temp";
execution = "net send " + destination + " " + message + " > \"" + thepath + "temporary.dat\"";
system(execution);

ie, I'm trying to grab the homedirectory environment variable so I can write my temporary file into the current user's temporary directory, where it belongs, rather than the root directory of the drive. Since the path gets stored in a char, I can't easily concatenate additional strings to the end of it. Also, you can't run the system() command on a char, it has to be a string. So even if I managed to concat all the extra crap I needed onto the char, I couldn't use it in the system function. Hence why I'm starting to hate char.

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Thu May 26, 2005 12:36 pm
Profile WWW
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
GFreeman wrote:
derf wrote:
*fists GFreeman*

:lol:

I hate Flamin' Fist's pansies. They almost allways miss :P ... well Freddie, old buddy, maybe you'll once take from me a good fisting lecture you ever dreamed of :lol:


I really think we should stop the fisting comments. :lol:

_________________
"Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson


Thu May 26, 2005 1:13 pm
Profile
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
Tip: If you wish it to print "" you should name it "";

Satis wrote:
Anyway, the enum thing looks promising, though it would still require me to define my error message separate from where I actually cause it. Bleh, anyway, better than an error code.

Have you seen how work throw - catch syntax? You can use enum in start, but later you'd wish to use whole objects. If you don't wish some extra efficiency, of course. But, then, why not to try Java?

The other: what about this?
Code:
#include <iostream>
#include <string>
using namespace std;

void f(const string &destination,const string &message)
{
   string   thepath = getenv("homepath");

   system( (
   
         string("net send ") +
         destination + " " +
         message + " > "" + thepath + "local settings\\temp" + "temporary.dat""
   
      ).c_str()
   );
}

main()
{
   f("Derf","I serve the Flaming fist!");

   return 0;
}


Anyway, it is awfuly slow. A bit faster?
Code:
#include <iostream>
#include <string>
using namespace std;

void f(const string &destination,const string &message)
{
   string   thepath = getenv("homepath");

   system( (
   
      (((((

         string("net send ")) +=
         destination + " ") +=
         message) += " > "") += thepath) += "local settings\\temp\\temporary.dat""
   
      ).c_str()
   );
}

main()
{
   f("Derf","I serve the Flaming fist!");

   return 0;
}


It is better but still not fast enough. Well, we're going to limit the length of the message to, ... to, ... about N chars.
Code:
#include <iostream>
#include <string>
using namespace std;

#include <string.h>

#define N 500

unsigned int strl(const char *); // custom strlen

char *buffer = 0;
const char * hpath;

void f(const string &destination,const string &message)
{
   char * buff = buffer + 9;

   *buff = 0;

   strcat(buff,destination.c_str());
   buff += destination.length();
   
   strcat(buff," ");
   buff += 1;

   strcat(buff,message.c_str());
   buff += message.length();

   strcat(buff," > "");
   buff += 4;

   strcat(buff,"local settings\\temp\\temporary.dat"");

   system(buffer);
}

main()
{
   hpath = getenv("homepath");

   if(buffer) delete [] buffer;
   buffer = new char[ strl(hpath) + N + 47 + 1 ];

   *buffer = 0;
   strcat(buffer,"net send ");

   f("Silke","I will slit your throat I will.");

   delete []buffer;
   return 0;
}

unsigned int strl(const char * a)
{
   const char * b = a;
   while(*b) b++;
   return b - a;
}

I know the result is crap since I don't know to use the "net send" but who cares.

Can it be better? It can, but leave it for now so. It is enough. Now we can shape it in one class (which is the point of C++ wtf).

Code:
#include <iostream>
#include <string>
using namespace std;

#include <string.h>

unsigned int strl(const char *);

class messenger {

   public:
      messenger(unsigned int len):
         hpath( getenv("homepath") ),buffer(0),len(len)

      { init(); }

      messenger(unsigned int len,const string&destination):
         hpath( getenv("homepath") ),
         dest(destination),
         buffer(0),
         len(len)
      { init(); }

      ~messenger()
      { delete [] buffer; }

      void sendamessage(const string&);
      void setdestination(const string&);

   private:
      unsigned int len, start;
      char * buffer;
      const char * hpath;
      string dest;

      void init();
      
      messenger& operator=(const messenger&);
      messenger(const &messenger);
};

void messenger::init()
{
   buffer = new char[ strl(hpath) + len + 47 + 1 + dest.length() ];
   
   *buffer = 0;
   strcat(buffer,"net send ");
   start = 9;

   strcat(buffer+start,dest.c_str());
   start += dest.length();

   strcat(buffer+start," ");
   start += 1;
}

void messenger::sendamessage(const string& message)
{
   char * buff = buffer + start;

   strcat(buff,message.c_str());
   buff += message.length();

   strcat(buff," > "");
   buff += 4;

   strcat(buff,"local settings\\temp\\temporary.dat"");

   cout << buffer;
}

void messenger::setdestination(const string&newdest)
{
   if(dest != newdest)
   {
      dest = newdest;
      init();
   }
}

main()
{
   messenger f(500,"GFreeman");

   f.sendamessage("hehe");
   cout << endl;

   f.setdestination("Derf");
   f.sendamessage("hihi");
   
   return 0;
}

unsigned int strl(const char * a)
{
   const char * b = a;
   while(*b) b++;
   return b - a;
}

I know I should offer some explainations with these codes but let it for later.

_________________
++


Thu May 26, 2005 3:13 pm
Profile WWW
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
Well, let forget the last two for now. I'm awfuly tired now.

_________________
++


Thu May 26, 2005 3:31 pm
Profile WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16650
Location: On a slope
Reply with quote
Post 
lol, I understood some of that. I'll have to reread it when I'm not so tired.

btw, the getenv has to be a char, hence my problems. I can make the rest happen otherwise. Though I don't understand objects yet. Nor what this 'speed' thing you talk of is.

ie, I would LIKE it to be string thepath = getenv("homepath");
but it has to be char * thepath = getenv("homepath");, which blows since char is such a bitch to work with.

another point, (and I haven't even touched on this yet), is that homepath returns a full system path, it

thepath = "\documents and settings\satis"

which means I would have to parse the path and add in some extra \ to escape the existing \ before running it through a command string. Not sure how to do that, but I'm sure it's possible. IE, some command string to convert it to

thepath = "\\documents and settings]\\satis"

bleh, anyway, my head is starting to hurt.

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Thu May 26, 2005 4:01 pm
Profile WWW
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
I'm not sure I understand your request. Will take a look later, if someone other don't answer.

So, every strnig object has a mathod .c_str() which gives the null-terminated char with content of the string. If you need your commands to be chars only because of int system(const char*) command, then it can solve your prob.
Code:
string command = "dir";
command += " ";
command += "/";
command += "p";

system( command.c_str() );

_________________
++


Thu May 26, 2005 4:16 pm
Profile WWW
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
derf wrote:
I really think we should stop the fisting comments. :lol:

Yes. :) We should start with the deeds. :D

_________________
++


Fri May 27, 2005 6:28 am
Profile WWW
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
the deeds?

_________________
"Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson


Fri May 27, 2005 7:36 am
Profile
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
derf wrote:
the deeds?

The fisting, dear. :) Quit talking. Let see how it really works.

Quote:
'speed' thing you talk of is.

Well, program can be more efficient (need less time to be executed) and less efficient (need more time to be executed). Using operator+ on strings is awfuly more slower than using operator+=. Cos a+b will make one absolutely new string (who is unnamed by you, but it exist). a+b+c will firstly make one unnamed from a+b and then from this unnamed and c will make the another one unnamed. So if you wish to add n strings, in fact you'll have n-1 absolutely new unnamed strings made, before you get the result. The operator+= is a much better cos it don't make new objects. It is just supposed to add the new string to the current. It is a bit better - but more compliceted to write. For example:

Code:
string a,b="def",c=" ",d="ghi",e="jkl";
a = a + b + c + d + e;

Code:
string a,b="def",c=" ",d="ghi",e="jkl";
(((a+=b)+=c)+=d)+=e;


Do the same thing but the second is more efficient. Brackets in the second are necessary cos of priprity. Otherwise, you could get the unexpected results.

Well, this can e useful for you as well:
Code:
string a="abc", b="def", c = "ghi";
system( (a+b+c).c_str() );

Here is in fact called the c_str() of the final unnamed object.

At end - there is using the pure char and null-terminated strings which can be a bit more efficient, but it is more advanced. Everything depend on what you wish to do.

Satis wrote:
ie, I would LIKE it to be string thepath = getenv("homepath");

Do it, Sat. It will work just fine. :) Array of chars from getenv will be accepted as an argument for the constructor of string object. It this DOT'T WORK for you, then change the compiler. What do you use anyway?

_________________
++


Fri May 27, 2005 8:15 am
Profile WWW
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
Image

_________________
"Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson


Fri May 27, 2005 11:53 am
Profile
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16650
Location: On a slope
Reply with quote
Post 
lol, that's an hilarious pic.

Gfree...yea, string * blah = getenc("homepath");

doesn't work. The compiler is gcc. I'm using dev-c++ as my IDE, because it's free. I prefer an IDE over a text editor because I don't have to deal with all the crazy linking compiling whatever crap you'd have to do otherwise. Not to mention the crazy command lines. :)

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Fri May 27, 2005 12:36 pm
Profile WWW
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
@Satis
Code:
string blah = getenv("homepath");

not
Code:
string * blah = getenv("homepath");

I use Borland's C++ Compiler 5.5. It is free, it is ANSI/ISO and it is widely used for schooling purposes, as I know.

http://www.borland.com/products/downloa ... ilder.html

You need JUST compiler.

_________________
++


Fri May 27, 2005 1:04 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 31 posts ]  Go to page Previous  1, 2, 3  Next

Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.