Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C++ Cheat Sheet, Cheat Sheet of C programming

C++ Cheat Sheet: Functions, arrays, switch statements and getting input & outputting.

Typology: Cheat Sheet

2021/2022

Uploaded on 07/05/2022

allan.dev
allan.dev 🇦🇺

4.5

(85)

1K documents

Partial preview of the text

Download C++ Cheat Sheet and more Cheat Sheet C programming in PDF only on Docsity! C++ Cheat Sheet ● Getting input & outputting: ○ cout << “This is output!” << endl; --> Will output: This is output! ○ To get input from the user, you have two options: ■ cin: Used directly under a cout statement asking for input, and stores the value the user enters into a variable: string a; cout << “Enter a name” << endl; cin >> a; This will enter what the user enters into a. Cin does not allow spaces in the entered value! ● getline function: Allows you to take an entire entered string with spaces: string a; cout << “Enter a name” << endl; getline(cin, a); This will enter what the user enters into a, and will allow for there to be a space in the name ● Functions: ○ Allow you to perform tasks outside of main. There are three important factors to implementing functions that are essential to remember: ■ Function Declaration​: Defines the return type, name, and parameters of the function. Put function definitions at the top of your code right under the libraries so that they are defined for the entire program to reference. Syntax​: return_type function_name(parameters); <--semicolon needed! Ex) void playStory(int myName, int myStrength); ● Function Implementations​: These are the actual functions themselves, which hold the code that executes. The signature of the function implementation and declaration must match exactly(parameters and everything) or you will get an error. Syntax​: return_type function_name(parameters) <--no semicolon! { //code written here } Ex) void playStory(int myName, int myStrength) { //code written here } ● Function Calls​: After your functions are implemented correctly, you can call them from main or other functions to execute the code written inside of them. Function calls have more simplistic syntax than declarations and implementations: Syntax​: function_name(parameters); Ex) playStory(myName, myStrength); Notice that the return type does not need to be written, and the types of the parameters don’t need to be written either! ● Arrays: ○ Allow you to store multiple values of the same type in one place. ○ Creation: int arr[] = {1, 2, 3}; —> creates an integer array with the values 1, 2, and 3 ○ Alternate creation: int arr[3]; —> creates an integer array with three spots, but it will be empty until you enter values ○ Putting user input into an array: int length; cout << “Enter the length for your array” << endl; cin >> length; int arr[length]; —>creates an array of size length cout << “Enter the values for your array” << endl; for(int i = 0; i < length; i++){ cin >> arr[i]; } ● Outputting the contents of an array: cout << “Outputting your array” << endl; for(int i = 0; i < length; i++){ cout << arr[i]; } ● Switch Statements: ○ Allow you to make a more compact version of conditional statements, but with limitations. Switch statements can only be made on characters(ex: ‘a’) or integers(ex: 1). Syntax: int choice; cout << “Choose an option: (1) or (2)” << endl; cin >> choice; switch(choice){ case 1: //code break; —>NEED to have a break, or your switch statement will run the cases under it until it reaches a break! case 2: //code; Break; default: —> the default will run if neither of the cases are run //code here optional break;
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved