We are still actively working on the spam issue.

Difference between revisions of "C++ for new friends"

From InstallGentoo Wiki
Jump to: navigation, search
(needs moar tabs, stylistic fixups (CONSISTENCY!), et al)
('std::endl' writes a newline and flushes stdout, there is no reason to use it in the example, plus it just adds more confusion.)
 
(5 intermediate revisions by 5 users not shown)
Line 15: Line 15:
 
{
 
{
 
// std::cout is sending the text to your screen
 
// std::cout is sending the text to your screen
// std::endl creates a new line
 
 
// note: 'std::' means you are using a member of the namespace 'std'
 
// note: 'std::' means you are using a member of the namespace 'std'
 
// more on that later.
 
// more on that later.
std::cout << "This text will appear on your sceen" << std::endl;
+
std::cout << "This text will appear on your sceen\n";
std::cout << "This text will appear on your sceen" << std::endl;
+
std::cout << "This text will appear on your sceen\n";
 
// note: all statements must end with a ; such as above
 
// note: all statements must end with a ; such as above
  
Line 25: Line 24:
 
return 0;
 
return 0;
 
}
 
}
 
 
 
</nowiki>
 
</nowiki>
  
Line 35: Line 32:
  
 
  <nowiki>
 
  <nowiki>
 
 
#include <iostream>
 
#include <iostream>
  
Line 55: Line 51:
  
 
// just to prove everything works we will print these to the screen
 
// just to prove everything works we will print these to the screen
std::cout<< "BIL "<<bill<<std::endl;
+
std::cout << "BIL " << bill << '\n';
std::cout<< "TOM "<<tom<<std::endl;
+
std::cout << "TOM " << tom << '\n';
std::cout<< "JIM "<<jim<<std::endl;
+
std::cout << "JIM " << jim << '\n';
std::cout<< "TOMMY "<<tommy<<std::endl;
+
std::cout << "TOMMY "<< tommy << '\n';
 
// note: tom will appear as 1 or 0 depending on the true/false state  
 
// note: tom will appear as 1 or 0 depending on the true/false state  
  
 
return 0;
 
return 0;
 
}
 
}
 
 
 
</nowiki>
 
</nowiki>
  
Line 93: Line 87:
 
tommy = 'X';
 
tommy = 'X';
 
// because bill is equal to 5 tommy will become X
 
// because bill is equal to 5 tommy will become X
}
+
}
  
 
if(bill == 6)
 
if(bill == 6)
Line 101: Line 95:
 
}
 
}
  
std::cout<< "TOMMY "<<tommy<<std::endl;
+
std::cout << "TOMMY " << tommy << '\n';
  
 
return 0;
 
return 0;
 
}
 
}
 
 
 
 
</nowiki>
 
</nowiki>
  
Line 127: Line 118:
 
while (bill != 0)
 
while (bill != 0)
 
{
 
{
std::cout<< "TOMMY "<<tommy<<std::endl;
 
 
//while bill is not equal to 0 the loop will run and print out tommy
 
//while bill is not equal to 0 the loop will run and print out tommy
 +
std::cout << "TOMMY " << tommy << '\n';
 +
 +
//the loop will also reduce bill by 1  each time it runs:
 
bill--;
 
bill--;
//the loop will also reduce bill by 1  each time it runs
 
 
//thanks to the decrement operator "bill--" bill could also be Incremented with "bill++"
 
//thanks to the decrement operator "bill--" bill could also be Incremented with "bill++"
 
}
 
}
Line 136: Line 128:
 
return 0;
 
return 0;
 
}
 
}
 
 
</nowiki>
 
</nowiki>
  
 
[[Category:Programming]]
 
[[Category:Programming]]
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]
 +
[[Category:HowTo]]
 +
[[Category:Programming languages]]

Latest revision as of 13:45, 19 November 2023

It is recommended to use an IDE, for a tutorial on how to set one up click HERE
Other great resource for learning C++ from the ground up.


Printing text to the screen

//Text that starts with "//" is ignored by the compiler 
//This is the library that allows you to use the "cout" and "endl" objects
#include <iostream>

// int main is where you will be writing your code example
// int main(){code goes here}
int main()
{
	// std::cout is sending the text to your screen
	// note: 'std::' means you are using a member of the namespace 'std'
	// more on that later.
	std::cout << "This text will appear on your sceen\n";
	std::cout << "This text will appear on your sceen\n";
	// note: all statements must end with a ; such as above


	return 0;
}



Using Data

#include <iostream>

int main()
{
	// Numbers in C (or C++) can be stored in variables
	// Each type can store a different type of data
	// For example an int is created by typing keyword int followed by a name, for this example bill
	int bill; //this int can store Integers -3 -2 -1 0 1 2 3 etc
	bool tom; //can store true or false
	float jim; //can store a decimal
	char tommy; //this takes characters such as 'x' or 'b'

	// variables can be created with no data and set later in the program such as here
	bill = 5;
	tom = true;
	jim = 1.5;
	tommy = 'b';

	// just to prove everything works we will print these to the screen
	std::cout << "BIL " << bill << '\n';
	std::cout << "TOM " << tom << '\n';
	std::cout << "JIM " << jim << '\n';
	std::cout << "TOMMY "<< tommy << '\n';
	// note: tom will appear as 1 or 0 depending on the true/false state 

	return 0;
}


Conditions

#include <iostream>

int main()
{

	int bill = 5;
	char tommy = 'b';
	// the if statement will look if the condition inside its brackets is true,
	// if it is it will run the block of code below
	// we are using the Equal to operator "==" to check if bill is equal to 5

	// other operators include 
	// !=	Not equal to
	// >	Greater than
	// <=	Less than or equal to
	// >=	Greater than or equal to

	if(bill == 5)
	{
		tommy = 'X';
		// because bill is equal to 5 tommy will become X
		}

	if(bill == 6)
	{
		tommy = 'Y';
		//because bill is not equal to 6 this code is ignored
	}

	std::cout << "TOMMY " << tommy << '\n';

	return 0;
}


Loops

#include <iostream>

int main()
{
	int bill = 5;
	char tommy = 'b';

	// like if, the while loop will run code if the condition is true
	// unlike if, while loops do not stop until the condition becomes false
	// below we used the Not equal to operator to check if bill is not equal to 0
	while (bill != 0)
	{
		//while bill is not equal to 0 the loop will run and print out tommy
		std::cout << "TOMMY " << tommy << '\n';

		//the loop will also reduce bill by 1  each time it runs:
		bill--;
		//thanks to the decrement operator "bill--" bill could also be Incremented with "bill++"
	}

	return 0;
}