Basic starting inf on the C++ main file:
#include <stdio.h>
#include <stdlib.h>
These 2 are necessary when using Microsoft visual basic.
#include <string.h> = allows strings to be used in the program
#include <time.h> = uses time to generate random numbers
Main code starts with:
void main ()
{
}
while("Variable") keeps the program from exiting the applet window, unless the variable = 0
if conditions for while are met, the process ends.
all lines except after "if", "while", "void", and "else" have to be followed by a ";"
Some basic commands used are:
(By the way, 'x' is my way of saying to insert a variable there.)
"int" 'x' = Defines to the computer an integer that is represented by 'x'.
"char" 'x' = Defines a character that is represented by 'x'.
"float" 'x' = Uses a mathmatical equation with defined ints.
ex: float adverage
adverage = ('x' + 'y' + 'z') / 3
"printf"("Insert text here") = displays the inserted text.
"scanf"("%'y' ", &'x')= Scans imput from the keyboard. 'y' = 'c' for characters, 'i' for integers. 'x' is what the imput is assigned to.
"&" tells the computer that to insert at "%[x]"
"if"("conditions")
{
}= Sets conditions and then runs a process between brackets.
"else"= sets conditions to occur if "if" does not function. Can be followed by another if statement.
All of these should turn blue when yped, except printf and scanf
DO NOT CAPITALIZE UNLESS IT's IN THE QUOTES!
Here's an example of a simple Greater or Less game.
Copy this into a C++ window. The colored font following "//" are notes.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
int r = rand() % 100;
int score = 1;
int a = -1;
char rk;
srand( time(0) );
//random string based off the time the program was started
r = rand() %100;
while( a != 9999 )
//as long as the inserted nuer is not 9999, the program will keep running
{
printf("Pick a number between 1-100\n");
scanf("%i%c", &a, &rk);
score = score + 1;
if (a == r)
//if the guess is equal to the randomly generated number, then the victory conditions below are displayed
{
printf("Correct\n");
score = score - 1;
}
if (a > r)
//if the guess is greater than the randomly generated number, then the failure conditions below are displayed
{
printf("wrong\n");
printf("lower\n");
}
if (a < r)
//if the guess is greater than the randomly generated number, then the failure conditions below are displayed
{
printf("wrong\n");
printf("higher\n");
}
printf("Your score is...%i\n",score);
}
while(1);
}
Arrays:
you can add "['x']" after "char" or "int" to say that there are 'x' amount of variables. These can be referenced by typing in "variable"['x'] and from there, the value of that can be set or changed.
By stating "Variable['x']", the 'x' variable is designated.
Array orders start from 0, so variable[12] will go up to 11.
Strings:
Strings work very similarly to arrays, with the main difference being that they are exclusively for letters and are often used for names and command nouns.
Useful constructions
for(int 'x' = 0; 'x' < '#'; 'x'++)
{
}= 'x' starts with a value of 0. As long as 'x' is less than '#'(how high you want x to go.), 'x' will equal 'x' + 1. The 1 will be added after the process .
Just as a side note:
GAMES TAKE FOREVER!
Comments (0)
You don't have permission to comment on this page.