making a basic program in c++ to output your inputs (easy)

Post here about scripting and programming for HaloPC (audio, network, ai, etc.)
Post Reply

was this a good tutorial

yes
1
100%
no
0
No votes
 
Total votes: 1

stephen10101





Posts: 140
Joined: Sat Jul 29, 2006 9:11 pm
Location: Klein, TX

making a basic program in c++ to output your inputs (easy)

Post by stephen10101 »

ok, so you want to learn how to program in c++, or at least im guessing that because you clicked here. well either way i'm going to show you how from scratch. fist open visual c++ express edition (i will have instructions for dev-c++ below the code) , because that is what i will be using, you can get it for free on microsoft.com. now from the start page go to file - new - project - win32 - win32 console application. name it tutorial program. then press ok and a settings thing will come up. click next then under aditional options click empt project and then finish. then go to open and open tutorial program.sln. the screen should look the same for the most part, except there will be a green arrow at the top middle of the page... for now it doesnt matter, it will later though. click on project - add class - c++ - c++ class - name it tutorial_program. now go to file - open - tutorial_program.cpp. erase EVERY THING IN THE CPP FILE ONLY. leave the h file alone. then in the cpp file put this code:

Code: Select all

#include <iostream> //For cout
#include <cstring>  //For the string functions

using namespace std;

int main()
{
  char food[50];
  char name[50];
  char lastname[50];
  char fullname[100]; // Big enough to hold both name and lastname
  int age;
  
 
  cout<<"Please enter your name: ";
  cin.getline ( name, 50 );
  cout<<"Enter your last name: ";
  cin.getline ( lastname, 50 );
  cout<<"please enter your age: ";
  cin>> age;
  cin.ignore();
  cout<<"Enter your favorite food: ";
  cin.getline ( food, 50 );
  fullname[0] = '\0';            // strcat searches for '\0' to cat after
  strcat ( fullname, name );     // Copy name into full name
  strcat ( fullname, " " );      // We want to separate the names by a space
  strcat ( fullname, lastname ); // Copy lastname onto the end of fullname
  cout<<"Your full name is "<< fullname<< ", you are "<< age << ", and your favorite food is "<< food << "." << "\n";
  cin.get();
}
then click on build solution. then click on the green arrow at the top middle of the screen to debug and run it..

now in dev-c++ all you do is go to file - new - project - console app. then you name it at erase everything and put the code in. all you do after that is go to execute - compile. when that finished then you go to execute - run.

now to break it down so you actually know what you did

fom the #include <iostream> to the int main() i wont explain because that would be redundant.

everything after a "//" is irrelevant... it is just notes.

the first sections with the char and int stuff is just were i defined variables the [50] and [100] are how big the input can be.

the stuff that comes after cout<< is computer outouts.

the cin.getline is used for inputing chars (charactar variables) .

the cin>> is for imputting integers, doubles, and strings.

the ( name, 50 ) part of the cin.getline ( name, 50 ); is telling the program to get the first 50 charactars of what was inputted.

the cin.ignor(); tells it to ignor the enter key after you input your age so the program wont terminate.

read the notes on the fullname[0] and strcat things and if you dont get it pm me. I think it is pretty explanitory though.

the cin.get(); at the very end is so that you can read the last output statement and then press any key when you want the program to terminate... without that line the program would close when you press enter after you input the last input in the program.

i saved the most basic part for last... you put a ; after every line in between the curly braces ({}).

i think that covers it all... if you dont get something and need help, then feel free to pm me. i hope this has helped. have a good day!
Last edited by stephen10101 on Sun Sep 24, 2006 8:31 am, edited 3 times in total.
User avatar
xbox7887




Socialist Coagulator Decryptor Advisor
Eureka Commentator Wave Scorched Earth

Posts: 2160
Joined: Mon Dec 27, 2004 6:19 pm
Location: New Lenox, Illinois
Contact:

Post by xbox7887 »

Code: Select all

Put your code here instead
stephen10101





Posts: 140
Joined: Sat Jul 29, 2006 9:11 pm
Location: Klein, TX

Post by stephen10101 »

sorry about that, i forgot... thanks for reminding me.
User avatar
dos mes





Posts: 2158
Joined: Thu Dec 29, 2005 9:58 pm
Location: Syracuse, NY

Post by dos mes »

Thanks for the tut! I'll definitely take a closer look tonight when I'm not so busy. I've been looking to start with some C++, should be great :)
stephen10101





Posts: 140
Joined: Sat Jul 29, 2006 9:11 pm
Location: Klein, TX

Post by stephen10101 »

no prob. thanks for the appreciation! :D
Post Reply