Projects I have worked on while attending Wayne State College.
Below are some notes of things I have learned while working on these projects. I will be adding more as I go along.
Check out W3 Schools to learn some of the basics of C++. They have many examples and cover just about everything you need to get started.
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
// Single-line comments
/*
Multi-line comments
*/
string example = "?";
cin >> example;
cout << "This is what you entered: " << example;
If, else if, and else statements can be great when you want to set different paths for the program to take.
int x = 2;
if (x >= 3) {
cout << "Greater than or equal to 3" << endl;
}
else if (x <= 1) {
cout << "Less than or equal to 1" << endl;
}
else {
cout << "The number is 2" << endl;
}
Ternary Operaters are shorter than if statements, but can be harder to read.
int x = 2;
string result = (x >= 3) ? "Greater than or equal to 3\n" : "Less than 3\n";
Switch statements are great when you have multiple expressions based on one variable.
int day = 5;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
default:
cout << "Must be an integer 1-5.\n";
break;
While loops are great when you don't know how many times you want the loop to run.
bool continueLoop = true;
string input = "?";
while (continueLoop) {
cout << "Would you like to continue? ";
cin >> input;
if (input == "no") {
continueLoop = false;
}
}
Do While loops are great when you want the loop to check the condition at the end of the loop. Instead of ending right once the condition is met, the loop will run before ending.
int i = 0;
do {
cout << i << endl;
i++;
if (i == 5) {
cout << "Loop done";
}
}
while (i < 5);
For loops are great when you know how many times you want the loop to run. They are also great when working with vectors and arrays.
for (int i = 0; i < 8; i++) {
cout << i << endl;
}
Break and Continue can be used to go back to the start of a loop or to exit the loop.
for (int i = 1; i <= 2; i++) {
cout << i << endl;
for (int j = 10; j <=13; j++) {
cout << j << endl;
if (i == 2 && j == 12) {
cout << "Break\n";
break;
}
if (i == 2) {
cout << "Continue\n";
continue;
}
cout << "End of j loop\n";
}
cout << "End of i loop\n";
}
cout << "Program done\n";
cout << "Arrays & Vectors";
cout << "Structures";
cout << "References";
cout << "Pointers";
cout << "Functions";
cout << "Classes";
You can use throw to output a reference number.
// Block of code to try
try {
int age = 15;
if (age >= 18) {
cout << "Access granted - you are old enough.";
}
else {
// Throw an exception when a problem arises
throw 505;
}
}
// Block of code to handle errors
catch (int myNum) {
cout << "Access denied - You must be at least 18 years old.\n";
cout << "Error number: " << myNum;
}
You can also use "..." in the catch if you don't know the throw type.
// Block of code to try
try {
int age = 15;
if (age >= 18) {
cout << "Access granted - you are old enough.";
}
else {
// Throw an exception when a problem arises
throw 505;
}
}
// Block of code to handle errors
catch (...) {
cout << "Access denied - You must be at least 18 years old.\n";
}
Its best to close a file once you're done working with it.
// Create a text file
ofstream {"filename.txt"};
// Create file object
ofstream MyFile;
// Open a text file
MyFile.open("filename.txt");
// Write to the file
MyFile << "Test write" << endl;
// Close the file
MyFile.close();
// Create file object
ofstream MyFile;
// Open a text file using append instead of overwrite
MyFile.open("filename.txt", ios::app);
// Write to the file
MyFile << "Test write" << endl;
// Close the file
MyFile.close();
// Create a text string, which is used to output the text file
string myText;
// Read from the text file
ifstream MyFile("filename.txt");
// Use a while loop together with the getline() function to read the file line by line
while (getline(MyFile, myText)) {
// Output the text from the file
cout << myText;
}
// Close the file
MyFile.close();
Check out W3 Schools to learn some of the basics of C. They have many examples and cover just about everything you need to get started.
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
// Single-line comments
/*
Multi-line comments
*/
char example[30];
scanf("%s", example);
printf("This is what you entered: %s", example);
This will read past spaces.
char example[30];
fgets(example, sizeof(example), stdin);
printf("This is what you entered: %s", example);
// Could also use puts
puts(example);
printf("Error Handling");
Its best to close a file once you're done working with it. You can find more file modes and information here.
// Create pointer of FILE type
FILE *fptr;
// Use appropriate location, w for write mode
fptr = fopen("C:\\filename.txt","w");
fclose(fptr);
// Create pointer of FILE type
FILE *fptr;
// Use appropriate location, w for write mode
fptr = fopen("C:\\filename.txt","w");
if(fptr == NULL) {
printf("Error!");
exit(1);
}
fprintf(fptr,"Test write\n");
fclose(fptr);
// Create pointer of FILE type
FILE *fptr;
// Use appropriate location, a for append mode
fptr = fopen("C:\\filename.txt","a");
if(fptr == NULL) {
printf("Error!");
exit(1);
}
fprintf(fptr,"Test write\n");
fclose(fptr);
// Create pointer of FILE type
FILE *fptr;
char fileText[30];
// Use appropriate location, r for read mode
if ((fptr = fopen("C:\\name.txt","r")) == NULL){
printf("Error! opening file");
// Program exits if the file pointer returns NULL.
exit(1);
}
while(fgets(fileText, 30, fptr)) {
printf("%s", fileText);
}
fclose(fptr);
Check out W3 Schools to learn some of the basics of C#. They have many examples and cover just about everything you need to get started.
using System;
class Program {
static void Main(string[] args) {
Console.WriteLine("Hello World!");
}
}
You can find more information on documentation style comments here.
// Single-line comments
/*
Multi-line comments
*/
/// <summary>
/// Documentation style comments
/// </summary>
string example = Console.ReadLine();
Console.WriteLine("This is what you entered: " + example);
Console.WriteLine("Classes");
Console.WriteLine("Exceptions");
Its best to close a file once you're done working with it.
Console.WriteLine("File IO");
Check out W3 Schools to learn some of the basics of Java. They have many examples and cover just about everything you need to get started.
The class must have the same name as the java file.
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
// Single-line comments
/*
Multi-line comments
*/
/**
* Javadoc documentation style comment
*/
Scanner scan = new Scanner(System.in);
String example = scan.nextLine();
System.out.println("This is what you entered: " + example);
System.out.println("Classes");
// Block of code to try
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
}
// Block of code to handle errors
catch (Exception e) {
System.out.println("Something went wrong.");
}
The finally block will be executed either way.
// Block of code to try
try {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
}
// Block of code to handle errors
catch (Exception e) {
System.out.println("Something went wrong.");
}
// Block of code to be executed either way
finally {
System.out.println("The 'try catch' is finished.");
}
Throw can be used to specify the exception type.
int age = 17;
if (age < 18) {
throw new ArithmeticException("Access denied - You must be at least 18 years old.");
}
else {
System.out.println("Access granted - You are old enough!");
}
Its best to close a file once you're done working with it.
try {
File myFile = new File("filename.txt");
if (myFile.createNewFile()) {
System.out.println("File created: " + myFile.getName());
}
else {
System.out.println("File already exists.");
}
}
catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
try {
FileWriter myWriter = new FileWriter("filename.txt");
myWriter.write("Test write\n");
myWriter.close();
System.out.println("Successfully wrote to the file.");
}
catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
To append text, just pass "true" in FileWriter's constructor.
try {
FileWriter myWriter = new FileWriter("filename.txt", true);
myWriter.write("Test write\n");
myWriter.close();
System.out.println("Successfully wrote to the file.");
}
catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
try {
File myFile = new File("filename.txt");
Scanner scan = new Scanner(myFile);
while (scan.hasNextLine()) {
String data = scan.nextLine();
System.out.println(data);
}
scan.close();
}
catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
File myFile = new File("filename.txt");
if (myFile.exists()) {
System.out.println("File name: " + myFile.getName());
System.out.println("Absolute path: " + myFile.getAbsolutePath());
System.out.println("Writeable: " + myFile.canWrite());
System.out.println("Readable " + myFile.canRead());
System.out.println("File size in bytes " + myFile.length());
}
else {
System.out.println("The file does not exist.");
}
File myFile = new File("filename.txt");
if (myFile.delete()) {
System.out.println("Deleted the file: " + myFile.getName());
}
else {
System.out.println("Failed to delete the file.");
}
File myFolder = new File("C:\\Users\\MyName\\Test");
if (myFolder.delete()) {
System.out.println("Deleted the folder: " + myFolder.getName());
}
else {
System.out.println("Failed to delete the folder.");
}
Check out W3 Schools to learn some of the basics of Python. They have many examples and cover just about everything you need to get started.
Python uses new lines to complete commands, unlike how other languages often use semicolons. Python also relies on indentation instead of curly-brackets like many other languages use.
You can start like this.
print("Hello World!")
Or you can start with a main. This is better if being imported.
def main():
print("Hello World!")
if __name__ == "__main__":
main()
#Single-line comments
'''
Multi-line comments
'''
"""Single-line Docstring documentation style comments"""
'''
Also used for multi-line Docstring documentation style comments
'''
example = input("Enter anything:")
print("This is what you entered: " + example)
print("Classes")
You can specify a block for a certain type of error.
try:
print(x)
except NameError:
print("Variable x is not defined")
except:
print("Something else went wrong")
You can use an else, which will be executed if there are no errors.
try:
print("Hello")
except:
print("Something went wrong")
else:
print("Nothing went wrong")
You can uses finally, which will be executed either way.
try:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")
You can use raise to throw an exception if a condition occurs.
x = -1
if x < 0:
raise Exception("Sorry, no numbers below zero")
You can specify what type of error as well.
x = "hello"
if not type(x) is int:
raise TypeError("Only integers are allowed")
Its best to close a file once you're done working with it.
This will return an error if the file exists.
#The "x" specifies to create the file
myFile = open("filename.txt", "x")
myFile.close()
This can also be used to create a file and will overwrite the contents of the file.
#The "w" specifies to overwrite the file
myFile = open("filename.txt", "w")
myFile.write("Test write\n")
myFile.close()
This can also be used to create a file and will append to the file.
#The "a" specifies to append to the file
myFile = open("filename.txt", "a")
myFile.write("Test write\n")
myFile.close()
This will read everything from the file, but you can pass an integer to specify how many characters you want to read.
#The "r" specifies to read from the file
myFile = open("filename.txt", "r")
print(myFile.read())
#print(myFile.read(5))
myFile.close()
You can also read line-by-line with the two examples below.
#The "r" specifies to read from the file
myFile = open("filename.txt", "r")
print(myFile.readline())
myFile.close()
#The "r" specifies to read from the file
myFile = open("filename.txt", "r")
for x in myFile:
print(x)
myFile.close()
It is best to check if the file exists to avoid an error.
import os
if os.path.exists("filename.txt"):
os.remove("filename.txt")
else:
print("The file does not exist")
import os
os.rmdir("myfolder")
Check out this YouTube tutorial to learn some of the basics of Markdown. This video will walk you through a worksheet to help you get started.
Check out this markdown file to see some of the basics for GitHub.