ClanKiller.com
https://forums.plasmasky.com/

I'm learning c++
https://forums.plasmasky.com/viewtopic.php?f=24&t=1256
Page 1 of 3

Author:  Satis [ Tue May 24, 2005 3:32 pm ]
Post subject:  I'm learning c++

yay, I'm teaching myself c++. So, anyway, I just coded my very first mildly useful console app and thought I'd celebrate. For anyone that remembers my windows messenger batch file, I present my windows messenger c++ executable.

I'm just posting the source code. Don't see the point in posting an executable. :P

Oh, btw, I'm a total C++ newb. For some of you freaks out there, if I'm doing something stupid let me konw while I'm still malleable.

Code:
#include <string>
#include <fstream.h>
using namespace std;

  int main(int argc, char *argv[])
{
  string destined;
  string message;
  string execution ("net send ");
  destination:
  cout << "Destination: ";
  getline(cin, destined,'\n');
  if(destined == "\0"){
         cout << "error\n\n";
         system("pause");
         return 1;
         }
  else if(destined == "quit"){
         cout << "quiting\n\n";
         system("if exist \"\\messager.tmp\" del \\messager.tmp");
         return 0;
         }
  system("cls");
  cout << "Type 'quit' to quit.  Type 'new destination' to choose a new endpoint.\nType 'clear' to clear the screen.\n";
  message:
  cout << destined << ": ";
  getline(cin, message, '\n');
  if(message == "\0"){
     goto message;
  }
  else if(message == "quit"){
     cout << "\n\n";
     system("if exist \"\\messager.tmp\" del \\messager.tmp");
     return 0;
  }
  else if(message == "new destination"){
     goto destination;
  }
  else if(message == "clear"){
     system("cls");
     goto message;
  }
 execution = "net send " + destined + " " + message + " > \\messager.tmp";
 system(execution.c_str());
 //parse file
          string readfile;
          ifstream file ("\\messager.tmp");
          if (! file.is_open()){
               cout << "Error opening file";
          }
          else {
               getline (file, readfile, '\n');
               readfile = readfile.substr(0, 23);
               if(readfile == "The message was success"){
                    cout << "Message Sent\n\n";
                    file.close();
               }
               else{
                    cout << "There was an error sending the message\n\n";
                    file.close();
                    goto destination;
               }
           }
  goto message;
  end:
  system("pause");
  return 0;
}

Author:  Rinox [ Tue May 24, 2005 3:38 pm ]
Post subject: 

It's crap!! Full of errors and completely lacking in shortcuts!

Of course, I have no idea what I'm taking about. Just wanted to say 'Go Satis' without being too friendly. :)

Author:  RB [ Tue May 24, 2005 4:43 pm ]
Post subject: 

Well, Sat, I won't comment it much. You should almost allways avoid using of labels-goto statements. Insted, in this case, you can very good use while with break and continue. Take look at the code down, it is supposed to be equivalet of code of yours, through I'm not 100% sure.

I'd suggest you to start with the simplier things, so you can first learn what is available for use. As well - take a look at the documantation for all avalilable methods, whatever C++ template/object you use. You can take a look at the places such as this

http://www.cs.waikato.ac.nz/~marku/progcomp/

for example.

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

main()
{   
   string destined, message, execution("net send ");
   int retval = 0;

   while(true)
   {
      // taking the destination /////////////////////////
      
      if(destined.empty())
      {
         cout << "Destination: ";
         getline(cin,destined,'\n');

         if(destined.empty())
         {
            cout << "error\n\n";
            system("pause");
            retval = 1;
            break;
         }
         else if(destined == "quit") break;

         system("cls");
         cout << "Type 'quit' to quit.   Type 'new destination' to choose a new endpoint.\nType 'clear' to clear the screen.\n";
         continue;
      }

      // mesagging //////////////////////////////////////

      cout << destined << ": ";
      getline(cin, message, '\n');

      if(message.empty()) continue;
      else if(message == "quit") break;
      else if(message == "new destination")
      {
         destined = "";
         continue;
      }
      else if(message == "clear")
      {
         system("cls");
         continue;
      }

      execution = "net send " + destined + " " + message + " > \\messager.tmp";
      system(execution.c_str());
 
      //parse file

      string readfile;
      
      ifstream file("\\messager.tmp");
      if(!file.is_open())
      {
         cout << "Error opening file";
      }
      else
      {
         getline (file, readfile, '\n');
         readfile = readfile.substr(0, 23);
         if(readfile == "The message was success")
         {
            cout << "Message Sent\n\n";
            file.close();
         }
         else
         {
            destined = "";
            cout << "There was an error sending the message\n\n";
            file.close();
            continue;
         }
      }
   }

   if(!retval)
   {
      cout << "quitting\n\n";
      system("if exist \"\\messager.tmp\" del \\messager.tmp");
      system("pause");
   }

   return retval;
}

Author:  Mole [ Tue May 24, 2005 4:59 pm ]
Post subject: 

I FUCKING HATE YOU.

I too am teaching my self c++. Look what you managed to "whip up"?! I took me long enough to do an addlibs program!

Code:
//
// Program to convert temperature from Celsius degree
// units into Fahrenheit degree units:
// Fahrenheit = Celsius * (212 - 32)/100 + 32
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int addlibone()
{
    string a;
    string b;
    string c;

        cout << "You have chosen addlib one." "\n";
        cout << "Enter a naming word: " "\n";
        cin >> a;
        cout << "Enter a describing word: " "\n";
        cin >> b;
        cout << "Enter an mamal (plural) " "\n";
        cin >> c;
        cout << "I am a " << a;
        cout << ". " "\n";
        cout << "I am very " << b;
        cout << ". " "\n";
        cout << "My mother sleeps with " << c;
        cout << ". " "\n";
       
       
}
int addlibtwo(void)
{
    string libone;
    string libtwo;
    string libthree;
    string libfour;
   
        cout << "You have chosen addlib two." "\n";
        cout << "Enter your first name: " "\n";
        cin >> libone;
        cout << "Enter an animal: " "\n";
        cin >> libtwo;
        cout << "Enter a word in all CAPS" "\n";
        cin >> libthree;
        cout << "Enter a doing word (ending with 'ing'" "\n";
        cin >> libfour;
        cout << "My name is " << libone;
        cout << ". " "\n";
        cout << "I am related to a " << libtwo;
        cout << ". " "\n";
        cout << "My dad yelled " << libthree;
        cout << " as he was " << libfour;
        cout << " the " << libtwo;
        cout << ". " "\n";
       
}
       


int main(int nNumberofArgs, char* pszArgs[])
{
int choice;
    cout << "Welcome to smeeps first addlib program!" "\n";
    cout << "©(Copyright) Steven Fellows 2005" "\n";
    cout << "choose a number, 1 or 2 (3 to exit)" "\n";
    cin >> choice;
switch(choice)
{
    case 1:
        addlibone();
        break;
    case 2:
        addlibtwo();
        break;
       
    default:
        cout << "duh... you broke it" "\n";
        break;
   
}
   
   
   
   
    //wait until user is ready before terminating the program
    // to all user to see the program results
    system("pause");
    return 0;
}
   


Took me forever! But I'm working on a small game right now. I guess it helps that you have some coding knowledge to begin with, where as I have none. And goto statements are bad. Use "while" instead, to exit "while" use "break;"

edit: Shit, some one already posted that. That's what you get for skimming.

I'm having problems with include files etc, which I'm slowly working out, but bleh to it for now.

Author:  RB [ Tue May 24, 2005 5:21 pm ]
Post subject: 

Oh, forgot to give one fine online reference http://www.cppreference.com/ :)

Author:  Mole [ Tue May 24, 2005 6:57 pm ]
Post subject: 

These guys are helpfull too
http://cboard.cprogramming.com/forumdisplay.php?f=3

Author:  derf [ Wed May 25, 2005 4:21 am ]
Post subject: 

lol GFree PwNz Satis!

Author:  ElevenBravo [ Wed May 25, 2005 7:17 am ]
Post subject: 

/golf clap

Author:  RB [ Wed May 25, 2005 8:54 am ]
Post subject: 

derf wrote:
lol GFree PwNz Satis!

Code:
class object {
//...
};

//...
map<string,object*> objects;

//...
objects["Gfree"]->helpifasked( objects[ ... ] );

Nothing more :)

Author:  Satis [ Wed May 25, 2005 10:16 am ]
Post subject: 

goto is bad? :cry: I was reliving my BASIC days.

Author:  derf [ Wed May 25, 2005 11:44 am ]
Post subject: 

Yet again, i fail to understand one of Gfree's posts.

Author:  Franny [ Wed May 25, 2005 4:09 pm ]
Post subject: 

don't worry your not alone...

Author:  RB [ Thu May 26, 2005 5:34 am ]
Post subject: 

Löl, guys, it was not my intention... just relax 8-). That was the point of it.

Author:  derf [ Thu May 26, 2005 6:35 am ]
Post subject: 

*fists GFreeman*

:lol:

Author:  Satis [ Thu May 26, 2005 10:32 am ]
Post subject: 

fists? :shock:

Anyway...y'know...c++ kinda sucks. Maybe it's just a lack of great documentation, but php is so much more malleable. I keep running into data incompatability problems with C++ (using functions that requre char when I have a string and vice versa) and I have a hell of a time figuring out how to convert back and forth. Maybe there're functions for this, but damned if I can find them. And why use char at all, when string is so much easier to work with?

Anyway, I may just build some functions of my own to convert between the two data types. I think my biggest frustration right now is figuring out the functions in c++ to do the things I want to do. Once my knowledge expands some, hopefully I'll groove into it like I did with php.

Page 1 of 3 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/