Thread: First Program using 5 Different Tutorials on Cprogramming.com

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    13

    First Program using 5 Different Tutorials on Cprogramming.com

    Ok hello C++ gurus Im here to learn C++ and cprogramming is really helping me with the basics. Once I feel comfortable maybe I can pick up a book or two on C++. My main reason for learning C++ is for clienthooking for games. Finding the offsets and reversing the functions so u can hook to the client and do anything u want, such as drawing text, and rendering the playermodels or creating a aimbot so the crosshair automatically locks on to the enemy

    Tutorials Used: [Thank you Cprogramming.com maybe I should make a C++ Blog hmmm.]
    Code:
    Intro to C++ The basics of C+
    Loops in C++
    Functions Functions...all about them, making and using the critters
    Strings About character arrays
    Source Code:
    Code:
    #include "stdafx.h"
    #include <iostream> // preprocessor file so we can use functions from iostream
     
    
    using namespace std; //this tells the compiler to use a group of functions that are apart of the library std. enables us to use functions such as cout and cin.
    void getplayers(); // define the function............we have it.
    int player; // a int which will hold how many players are on the server
    int main() //the main function where the program starts at.
    {
    	
    	char serverip[16]; // a string that will hold 16 characters.. A IP address can be no longer than 16 chars ex. 111.111.111.111 so we dont need anymore
    	
    	cout<<"Enter the IP im connecting to:"; // used to output text.
    	cin>> serverip; // read a value into serverip etc the IP
    	cin.ignore(); // function that reads and discards a character. 
    	cout<<"Connecting to "<< serverip <<"\n";
    cin.get(); //runs in input and expects the user to hit the return key. 
    cout<<"Finding Players..";
    cout<<"\n";  //clear a new line \n is not working on the outcall so lets just make a clear line so the last line is not caught up with the display of players.. cheap fix I guess but it works.
    
    getplayers(); //time to call in the function
    }
    
    
    void getplayers() // a simple function
    
    {
    
    
    	for (int x = 0;x < 10; x++) // a simple loop, lets loop intill we find all the players on the server.
    	{
    		cout<<"Player "<< x <<endl; //output the player and his ID ex. Player 1
    		player++;  //since this is going to loop intill int x is equal to 10 lets just add it.
    	
    
    }
    
    	cin.get();
    cout<<"There are " << player <<" players on this server.";
    	cin.ignore();
    
    }
    Last edited by Clinthill98; 09-14-2009 at 03:58 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Instead of
    Code:
    cout<<"\n";  //clear a new line
    I would suggest
    Code:
    cout<< endl ;  //clear a new line 
    or 
    cout<< '\n';  //clear a new line
    but the endl (when written in caps, ENDL), would be better.

    And at some point, you'll need to create some variables.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I suppose it will be a very long way from here to online games.

    Code:
    	char serverip[16]; // a string that will hold 16 characters.. 
            //A IP address can be no longer than 16 chars ex. 111.111.111.111 so we dont need anymore
    	
    	cout<<"Enter the IP im connecting to:"; // used to output text.
    	cin>> serverip; // read a value into serverip etc the IP
    The comment and the code don't agree. cin will happily attempt to read any number of characters that the user chooses to input.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    Quote Originally Posted by anon View Post
    I suppose it will be a very long way from here to online games.

    Code:
    	char serverip[16]; // a string that will hold 16 characters.. 
            //A IP address can be no longer than 16 chars ex. 111.111.111.111 so we dont need anymore
    	
    	cout<<"Enter the IP im connecting to:"; // used to output text.
    	cin>> serverip; // read a value into serverip etc the IP
    The comment and the code don't agree. cin will happily attempt to read any number of characters that the user chooses to input.
    are u referring to char serverip[16] ? I tryed using just char serverip; it only reads 1 character..

  5. #5
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by Clinthill98 View Post
    are u referring to char serverip[16] ? I tryed using just char serverip; it only reads 1 character..
    Yes, he is, and no, of course that doesn't work.

    cin reads as much as you input. cin.getline() or cin.get() with the right arguments will do what you want, though.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Clinthill98 View Post
    are u referring to char serverip[16] ? I tryed using just char serverip; it only reads 1 character..
    That's true. However, if you do use serverip[16] and someone types
    ;lkajs;dfja;sdjf;asjf;asdfj;asjkf;askfj;asdkfjlasd jfhlashflasjhf;asdjf;ahgksjhfakshflasdflashfd
    then you're going to get a lot more than 16 characters stored ... somewhere, hopefully not on top of something you want to keep.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    Quote Originally Posted by tabstop View Post
    That's true. However, if you do use serverip[16] and someone types
    ;lkajs;dfja;sdjf;asjf;asdfj;asjkf;askfj;asdkfjlasd jfhlashflasjhf;asdjf;ahgksjhfakshflasdflashfd
    then you're going to get a lot more than 16 characters stored ... somewhere, hopefully not on top of something you want to keep.
    Yeah your right it doesnt cap it. So why does it only read one if its defined char serverip; because that defines 1 character and if i want 16 than use char serverip[16];

    EDIT maybe it was only C outing one on the screen then?
    Last edited by Clinthill98; 09-14-2009 at 04:35 PM.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    Quote Originally Posted by IceDane View Post
    Yes, he is, and no, of course that doesn't work.

    cin reads as much as you input. cin.getline() or cin.get() with the right arguments will do what you want, though.
    i got it thanks Ice I was using the wrong args. but my idea was correct thanks I will look at it later I see what u mean now cin>> will return any input but with the right args using cin.get(args) I can make it read only read 16 characters even though its really unnecessary It will give me a better understanding of how it works thanks guys for all your post btw linked list scary me right now :| confusing..
    Last edited by Clinthill98; 09-14-2009 at 04:50 PM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Clinthill98 View Post
    Yeah your right it doesnt cap it. So why does it only read one if its defined char serverip; because that defines 1 character and if i want 16 than use char serverip[16];

    EDIT maybe it was only C outing one on the screen then?
    The general rule is that C++ reads the largest thing that "makes sense" (technical term = greedy). If you've got a character variable, the largest thing that makes sense is a single character. If you've got a character array, the largest thing that "makes sense" is the whole pile of typed letters up to the first space, regardless of how much space is there. (You can overflow other types of variables too, although it doesn't hurt as much; for instance, if you read in an int and you type in 11111111111111111111111111111111111111111111111111 111, then it will read it all in and store ... something ... in your int variable (it won't be that number, because that's larger than you can store in an int variable) -- but int variables automatically use modular arithmetic, so it will get reduced mod (2^32) or whatever the size of your int is.

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    thanks guys I see what your saying now and I fixed it simple solution instead of using cin>> which reads any output the user enters I used cin.getline(serverip, 16) which will only read 16 characters. thanks!

    Code:
    #include "stdafx.h"
    #include <iostream> // preprocessor file so we can use functions from iostream
     
    
    using namespace std; //this tells the compiler to use a group of functions that are apart of the library std. enables us to use functions such as cout and cin.
    void getplayers(); // define the function............we have it.
    int player; // a int which will hold how many players are on the server
    int main() //the main function where the program starts at.
    {
    	
    	char serverip[16]; // a string that will hold 16 characters.. A IP address can be no longer than 16 chars ex. 111.111.111.111 so we dont need anymore
    	
    	cout<<"Enter the IP im connecting to:"; // used to output text.
    	cin.getline(serverip, 16); // fix for cin>> because it reads any output the user enters, now it only reads 16 characters since a IP address can be no longer than 16 chars ex. 111.111.111.111
    	cin.ignore(); // function that reads and discards a character. 
    	cout<<"Connecting to "<< serverip <<"\n";
    cin.get(); //runs in input and expects the user to hit the return key. 
    cout<<"Finding Players.."<<endl; // thanks Dino clears line
    getplayers(); //time to call in the function
    }
    
    
    void getplayers() // a simple function
    
    {
    
    
    	for (int x = 0;x < 10; x++) // a simple loop, lets loop intill we find all the players on the server.
    	{
    		cout<<"Player "<< x <<endl; //output the player and his ID ex. Player 1
    		player++;  //since this is going to loop intill int x is equal to 10 lets just add it.
    	
    
    }
    
    	cin.get();
    cout<<"There are " << player <<" players on this server.";
    	cin.ignore();
    }

  11. #11
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    Your indentation is messy.

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    Quote Originally Posted by noerrorsfound View Post
    Your indentation is messy.
    umm yea is it best to define functions and classes in header files? because it looks all messy right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. C Programming Tutorials
    By F33N in forum C Programming
    Replies: 6
    Last Post: 05-20-2007, 02:04 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM