General C++ Cheat Sheet
General C++ Cheat Sheet
Review
MAIN INFO
Calling for Functions & Namespace
#include
#include
#include
#include
#include
<iostream>
<string>
<cmath>
<cstdlib>
<time.h>
<<<<<-
---------------------------------------------------------------------
Variables
else{
return 0;}
}
int main()
{
cout<<"From (x= 499.98, y= 499.97, z= 499.99) The Maximum is:
"<<maximum(499.98, 499.97, 499.99)<<endl;
int soccerNum = 9;
return 0;
}
---------------------------------------------------------------------
char apple='a';
---------------------------------------------------------------------
Cout
cout<<Hello World!<<endl;
---------------------------------------------------------------------
Cin
int Num1;
cout<<Enter A Number: ;
cin>> Num1
---------------------------------------------------------------------
Else
if (TRUE){
// Execute these statements if TRUE
}
else {
// execute these statements if FALSE
}
converttime(seconds,h,m,s);
---------------------------------------------------------------------
Else If
if (<condition>){
// Execute these statements if TRUE
}
else if (<another condition>){
// execute these statements if <another condition> is TRUE
// and <condition> is FALSE
}
---------------------------------------------------------------------
Switch
switch (code) { //add as many cases.
case 1:
cout<<"3M Corporation"<<endl;
break;
case 2:
cout<<"Maxell Corporation"<<endl;
break;
default:
break;
}
---------------------------------------------------------------------
While Loop
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double amount=1000,interest=.0325;
int count = 1;
Random Numbers
int i=0, j=0, k=0;
int main()
{
for (i=0; i<10;i++) {
int v1,v2,answer;
v1 = rand()% 10;
v2 = rand()% 100 + 1;
v3 = rand()% 30 + 1985;
// v1 in the range 0 to 9
// v2 in the range 1 to 100
// v3 in the range 1985-2014
while (count<=10) {
amount= amount +(amount*interest);
cout<<"Amount at the end of the year"<<count<<"is ($): "<<amount<<endl;
count++;
}
else{
cout<<"INCORRECT!\n<<";
j++;}
---------------------------------------------------------------------
For Loop
#include <iostream>
using namespace std;
int main()
{
double balance=1000.0;
(initialization,limit,count)
for (int i=0;i<10;i+=1){
//balance = balance + (balance*.08)
//balance = balance*1.08
balance *= 1.08;
cout<<"For year "<<i+1<<" your balance is $ <<balance<<endl;
}
return 0;
}
---------------------------------------------------------------------
Functions
double square(double a)
{
return a*a;
}
int main()
{
cout<< "9 squared is " << square(9) << endl;
return 0;
}
double maximum (double x, double y, double z)
{
double percentage=k*10;
if (percentage<80) {
cout<<"You got "<<percentage<<"% correct, Please ask your instructor
for extra help."<<endl;
}
else { cout<<"Good Job! you got "<<percentage<<"% Correct!"<<endl;
return 0;
}
}
---------------------------------------------------------------------
MATH
Add
> +
Remainder > %
Or
> ||
Subtract
> x^2
> pow(x,2)
And > &&
Divide
> /
e^2
> exp(2)
Multiply
> *
Equality
> ==
---------------------------------------------------------------------
EXAMPLES
int main() {
int month,day;
string season;
cout<<"Enter numerical Month: "<<endl;
cin>>month;
C++
Review
else if (month == 7 || month == 8 || month == 9)
season = "Summer";
else
season = "Fall";
#include <time.h>
using namespace std;
int main() {
double i, p=0,tot=0,avg1,num;
p++;
}
avg1=tot/i;
cout<<"Your Average is = "<<avg1<<endl;
else
season = "Winter";
cout<< "Season: " << season << endl;
}
return 0; }
---------------Display Triangle------------
int main()
{
int r=5;
// r = the last row
char star='*';
char space=' ';
for (int i=1; i<=r; i++) // i = the row where that I want to show
first {
return 0; }
//-------------------------REVIEW QUESTION 2------------------------#include <iostream>
#include <cmath>
using namespace std;
double mult(double x, double y)
{
return x*y;
}
for (int s=1; s<=r-i; s++) // s = how many spaces per row
cout<<space;
int main() {
for (int a=1; a<=2*i-1; a++) // a = how many asterixs per row
cout<<star;
cout<<endl; }
------ Display Downward Triangle ---
return 0; }
//-------------------------REVIEW QUESTION 3------------------------#include <iostream>
for (int a=1; a<=2*i-1; a++) // a = how many asterixs per row
cout<<star;
cout<<endl; }
return 0;
}
-------- Pythagoras Theorem -------
int main()
{
for (int a = 1; a <= 500; a++)
{
for (int b = 1; b <= 500; b ++)
{
for (int c = 1; c <= 500; c ++)
{
if ((a*a + b*b) == (c*c))
cout<<"a= "<<a<<", b= "<<b<<", c= "<<c<<endl;
}
}
}
return 0;
}
----- Function to find dates after.. ---
void yrtime(int& days,int& year, int& month, int& day);
int main()
{
cout<<"The maximum in x=3, y=8, z=10 is: "<<maximum(3, 8,
10)<<endl;
return 0; }
//-------------------------REVIEW QUESTION 4------------------------#include <iostream>
using namespace std;
void max_and_avg(double& first,double& second, double& third, double&
max, double& avg)
{
max=first;
if (second>max) {
max=second; }
int main() {
int days,y,m,d;
if (third>max) {
max=thir }
return max;
avg=(first+second+third)/3;
int main() {
double x, y, z, max, avg;
cout<<"Enter first number: ";
cin>>x;
cout<<"Enter second number: ";
cin>>y;
cout<<"Enter third number: ";
cin>>z;
max_and_avg(x, y, z, max, avg);
cout<<"The Highest number you entered is: "<<max<<endl;
max_and_avg(x, y, z, max, avg);
cout<<"And the Average is: "<<avg<<endl;
return 0;}
C++
Review
---------------------------Grades and average------------------------
MAIN INFO
Calling for Functions & Namespace
int main()
{
int const ROW = 3, COL = 2;
---------------------------------------------------------------------
Arrays
int number[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
number[0] number[1] number[2] number[11]
Number
:
&Number
:
&number[0] :
1245020
1245020
1245020
:
:
31
31
Number+1
:
&Number[1] :
1245024
1245024
//you can use max=INT_MIN to get the minimum value of the array
--------------------------------------------------------------------int main()
system("pause");
return 0;
}
int main()
{
const int ROWS=10;
const int COLS=20;
int fmax[ROWS][COLS];
for(int i=0; i<ROWS;i++)
{
for(int j=0; j<COLS;j++)
{
fmax[i][j]=(rand() % 101);
}
}
system("pause");
return 0;
}
--------------------------------------------------------------------#include <iostream>
using namespace std;
int main()
{
int const GRADES = 15;
int max=0,loci=0,locj=0;
for(int i=0; i<ROWS;i++)
{
max=0;
for(int j=0; j<COLS;j++)
{
if (max<fmax[i][j])
{
max=fmax[i][j];
loci=i;
locj=j;
}
}
pt = price;
system("pause");
return 0;
int i;
for(i = 0; i < 7; i++)
cout << *(price + i) << endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main(){
int alternating_sum[10], s=0;
//display array
int main()
{
int a[7] = {1,2,3,4,5,6,7};
display (a,7);
zeropadshift(a, 7, 2 );
display (a,7);
cout<<endl;
for(int i=0; i<10; i++){
//initialize array
//display before shifting
//shifting
//display after shifting
return 0;
}
}
cout<<s<<endl;
return 0;
}
CONTINUED------------------------Next Page---------->
C++
Review
void zeropadshift(int array[], int size, int distance)
{
int temp=0;
if (distance > 0 && distance <= size)
{
for (int i = size - 1; i > distance - 1; i--)
{
array[i] = array[size - distance - temp - 1];
temp++;
}
//array length
system("pause");
return 0;
}
}
int main()
{
string a;
cout << " Enter a string: " << endl;
getline (cin, a);
int count = 0;
for(int i = 0; i < a.length(); i++)
{
if(a[i] == ' ' && a[i + 1] != ' ')
count++;
}
return 0;
}
};
struct stock
{
char name[10];
double pshare, ratio, pvalues;
};
int main()
{
struct time militaryTime;
int hour;
string cycle;
---------------------------------Stocks-----------------------------
int main()
{
stock s[5];
if (militaryTime.hour>=12){
hour=militaryTime.hour-12;
cycle= "pm";
}
else { cycle= "am"; }
int intValues[]={};
pt = *intValues;
for (int i=0; i < 5; i++)
{
cout<<"Enter desired value for array " <<i+1<<": ";
cin>>*(intValues+i);
}
cout<<endl;
for (int i=0; i < 5; i++)
{
cout<<"intValue["<<i<<"] contains the value --> "<<intValues[i]<<endl;
}
return 0;
}
//These loop gives me the value for the COUNT that im using
system("pause");
return 0;
}
#include
#include
#include
#include
<iostream>
<fstream>
<cstdlib>
<string>
C++
Review
}
older(adays, bdays);
max = a[i].Grosspay;
//system("pause");
cin.get();
return 0;
return max;
#include <iostream>
#include <string>
int main()
{
Employee employee[6];//declare
employee[0].Number = 3462;
employee[1].Name = "Robbins";
int main()
{
struct Date { int month; int day; int year; };
//all years have 360 days and each month consists of 30 days
employee[1].Rate = 5.83;
employee[0].Name = "Jones";
employee[0].Rate = 4.62;
employee[0].Hours = 40;
employee[1].Number = 6793;
employee[1].Hours = 38;
employee[2].Number = 6985;
Date start[1];
cout << "Please enter a starting month; " << endl;
cin >> start[0].month;
cout << "Please enter a starting day; " << endl;
cin >> start[0].day;
cout << "Please enter a starting year; " << endl;
cin >> start[0].year;
employee[2].Name = "Smith";
employee[2].Rate = 5.22;
employee[2].Hours = 45;
employee[3].Number = 7834;
Date days[6];
employee[3].Name = "Swain";
int since[5];
employee[3].Rate = 6.89;
for (int i = 0; i < 5; i++)
{
cout << "Please enter a range-to month; " << endl;
cin >> days[i].month;
cout << "Please enter a range-to day; " << endl;
cin >> days[i].day;
cout << "Please enter a range-to year; " << endl;
cin >> days[i].year;
}
employee[3].Hours = 40;
employee[4].Number = 8867;
employee[4].Name = "Timmins";
employee[4].Rate = 6.43;
employee[4].Hours = 35;
employee[5].Number = 9002;
employee[5].Name = "Williams";
employee[5].Rate = 4.75;
C++
Review
employee[5].Hours = 42;
return 0;}
+ grades[i][2]*.3;
}
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
cout << grades[i][j]<<"\t";
}
cout << endl;
}
system("pause");
return 0;
}
-----------------------Skips leading Whitespaces in Each Line---------------------#include "stdafx.h"
#include "string"
#include "stdlib.h"
#include "stdio.h"
#include "sstream"
#include "iostream"
#include "fstream"
using namespace std;
string& trim(string &str, string::size_type pos = 0)
{
static const string delim = " \t";
pos = str.find_first_of(delim, pos);
if (pos == string::npos)
return str;
return trim(str.erase(pos, 1));
}
void Test(void)
{
char *str1 = NULL;
str1 = new char[20];
char str[7];
strcpy_s(str1, 20, "hello world");
strcpy_s(str, "hello");
cout << "strlen(str1):" << strlen(str1) << endl << "strlen(str):" <<
strlen(str) << endl;
printf(str1);
printf("\n");
cout << str << endl;
system("pause");
}
int main(int argc, char* argv)
{
string line;
//char* deleted_file;
ifstream infile("verse.txt");
ofstream outfile("modified_verse.txt");
while (getline(infile, line)){
//deleted_file = const_cast<char*>(line.c_str());
trim(line);
outfile<<line;
}
//Test();