It is currently Fri Mar 29, 2024 1:44 am



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

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 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;
}

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


Tue May 24, 2005 3:32 pm
Profile WWW
Minor Diety
User avatar

Joined: Mon Mar 31, 2003 7:23 am
Posts: 14878
Location: behind a good glass of Duvel
Reply with quote
Post 
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. :)

_________________
"I find a Burger Tank in this place? I'm-a be a one-man cheeseburger apocalypse."

- Coach


Tue May 24, 2005 3:38 pm
Profile
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
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;
}

_________________
++


Tue May 24, 2005 4:43 pm
Profile WWW
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 5:09 pm
Posts: 4003
Location: Walsall, West Mids, UK
Reply with quote
Post 
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.

_________________
Games to complete:
GTA IV [100%] (For Multiplayer next!)
Fallout 3 [50%]
Rock Band [35%]
http://www.cafepress.com/SmeepProducts


Tue May 24, 2005 4:59 pm
Profile WWW
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
Oh, forgot to give one fine online reference http://www.cppreference.com/ :)

_________________
++


Tue May 24, 2005 5:21 pm
Profile WWW
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 5:09 pm
Posts: 4003
Location: Walsall, West Mids, UK
Reply with quote
Post 
These guys are helpfull too
http://cboard.cprogramming.com/forumdisplay.php?f=3

_________________
Games to complete:
GTA IV [100%] (For Multiplayer next!)
Fallout 3 [50%]
Rock Band [35%]
http://www.cafepress.com/SmeepProducts


Tue May 24, 2005 6:57 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 
lol GFree PwNz Satis!

_________________
"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


Wed May 25, 2005 4:21 am
Profile
King
User avatar

Joined: Wed Apr 16, 2003 2:18 pm
Posts: 1976
Location: Sexy Town
Reply with quote
Post 
/golf clap

_________________
Contrary to popular belief, America is not a democracy, it is a Chucktatorship.
Image


Wed May 25, 2005 7:17 am
Profile ICQ YIM
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
derf wrote:
lol GFree PwNz Satis!

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

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

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

Nothing more :)

_________________
++


Wed May 25, 2005 8:54 am
Profile WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
goto is bad? :cry: I was reliving my BASIC days.

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


Wed May 25, 2005 10:16 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 
Yet again, i fail to understand one of Gfree's posts.

_________________
"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


Wed May 25, 2005 11:44 am
Profile
Marquis
User avatar

Joined: Tue Jun 10, 2003 11:08 am
Posts: 940
Location: over there
Reply with quote
Post 
don't worry your not alone...

_________________
Give a man a fire, he'll be warm for a night.
Set a man on fire, he'll be warm for the rest of his life. - Satis Clankiller


Wed May 25, 2005 4:09 pm
Profile
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
Löl, guys, it was not my intention... just relax 8-). That was the point of it.

_________________
++


Thu May 26, 2005 5:34 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 
*fists GFreeman*

: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 6:35 am
Profile
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
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.

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


Thu May 26, 2005 10:32 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 31 posts ]  Go to page 1, 2, 3  Next

Who is online

Users browsing this forum: No registered users and 9 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.