Programming Tutorials
C++ Beginner Programming Video Tutorial
Submitted by Baran Ornarli on Thu, 11/13/2008 - 06:39.
In this video, we make a simple guessing game for beginners using C++ and we also explain how to set up your programming environment. This is a very simple tutorial for beginners.
Now if you want to improve your C++ skills, you should try and use a while(1) or infinite loop around your code so that the game restarts when the user loses or wins the guessing game. If you are able to modify your code, well just post your code on InfernoDevelopment Programming Forums and show off your C++ skills so that others can learn.
If you want to see the finished program:
#include <cstdlib> #include <iostream> // The two slashes are comments // this text will not be compiled // the above 2 #include statements // are to bring in 2 standard // libraries from the C++ libraries // the first one is for system() // which is a function. // The second one is for cout, cin // which is output to the screen // and input from the user keyboard using namespace std; // if we don't put up the above // using statement. Then we would // be forced to write cout and cin // like this: std::cout or std::cin // so we're just saying we want to // always use std for cout/cin. // int main is simply the standard // function for all beginning // C++ programs. // the int means integer, because // when function main is finished // it will return an integer type. int main(int argc, char *argv[]) { int guess(0); // we just initialized a // local variable called // guess. C++ is case sensitive. // guess is initialized as 0. // this is the same as // int guess = 0; // this will be our guessing number. int MyGuess(0); // this is another integer at zero. srand(time(NULL)); // srand is a function that // seeds the rand() function // with a time(0) (UNIX time), // basically it will // generate an almost random // starting point for rand(). guess = rand()%10+1; // this will be the number we have to guess // The %10 means 10 is maximum random number. // The +1 means 1 is the minimum random // number. // Now guess contains our random number. // % is actually modulus (divide by 10, get // remainder). cout << "Guess our random number (1-100)\n"; // cout will output text to our console. // pay attention to the arrows facing cout. // We add our string that we want to print. // \n means a new line should appear there. // it's like pressing return/enter on keyboard. cin >> MyGuess; // This will wait for the user to type // a number to the screen. It will save it // as MyGuess. A space or return/enter // will make the code continue. cout << "You guessed " << MyGuess << ", the answer"; cout << " was " << guess << "\n"; // We tell the user the results. if(MyGuess == guess){ // check if MyGuess is guess cout << "You guessed it!\n"; } else { // otherwise cout << "You failed this extremely simple game.\n"; } system("PAUSE"); // This will pause the program. // displaying a message like // "Press any key to continue". // which will end program. return EXIT_SUCCESS; // exit success is just a // representation for integer zero. }
(1 vote)


Post new comment