Difficult One Flashcards
You’ve entered bad information using cin
How do you clear the input buffer to discard invalid input
import limits
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), ‘\n’);
The doctor yells CLEAR! and brings IGOR back to life.
IGOR’s hands go NUMB UNDER his gloves so he uses his LIMBS to STREAM his favorite show superSIZE me. He tries playing with his mighty MAX() toy that has 2 figureines to get his feeling back.
What is everything you can use with fstream?
ifstream - input
i=eyes
read
ofstream - output
o=mouth
write
fstream - everything
f=f*ck it, you do what you want
What can you add to your lib.h file to make sure it’s only read once?
pragma once <- place this at the top
How do you create a variable in lib.h and define it somewhere else
extern uint32_t command;
How do you append to a file?
std::ofstream MyFile(“todo.txt”, std::ios::app);
The proFeSsor grabs his TEXTbook, tries to read it, but instead gets on his IPHONE and looks at his favorite APP
REMEMBER JESUS FISHING
std::string text;
std::ofstream MyFile(“test.txt”, std::ios::app);
std::getline(std::cin, text);
MyFile «_space;text;
How do you delete everything, even the lines from a file?
std::ofstream file(“todo.txt”, std::ofstream::trunc)
Enter a string for cin
might need “limits” library here
std::getline(std::cin, to_do);
to_do += ‘\n’
Jesus asks you to get a line so he can fish.
After you give it to him he is so thankful that his puts his hands together, one side holds a rock the other holds your sins. He places your sins in the rock and then tosses it in the water.
Show a simple class with two attributes
create an object (instance of that class) and give values to the attributes
class MyClass {
public:
int myNum;
sting myString;
};
public is and access specifier which specifies the members (The attributes and methods)
int main() {
MyClass myObj;
myObj.myNum = 15;
myObj.myString = “Some text”;
cout «_space;myObj.myNum «_space;“\n”;
cout «_space;myObj.myString;
return 0;
}
Create a template
<template>
T add(T a, T b) {
return a + b;
}
template is like auto
</template>
Create a function that takes in an array reference
RAW
void printArray(int (&arr)[5])
{
for (int i = 0; i < 5; i++)
{ cout «_space;arr[i] «_space;” “;
}
cout «_space;endl;
}
STANDARD ARRAY
void printArray(const array<int, 5>& arr)
Read a file line by line and send it to a deque
std::deque<std::string> items;
std:uint32_t to_do;
std::fstream MyFile(“todo.txt”)
std::string MyText;
while(getline(MyFile, MyText))
{
items.push_back(MyText)
}
Clear the screen
include <stdlib.h></stdlib.h>
system(“cls”)
Create an employee class that has a private attribute “salary” declared but not defined.
Create two public get and set methods.
Set should set salary to whatever the user wants to set it to.
Make the get method give the user back the modified salary attribute
class Employee {
private:
// Private attribute
int salary;
public:
// Setter
void setSalary(int s) {
salary = s;
}
// Getter
int getSalary() {
return salary;
}
};
int main() {
Employee myObj;
myObj.setSalary(50000);
cout «_space;myObj.getSalary();
return 0;
}
Create a base class of Vehicle that has a public attribute of brand = Ford
and a method that print “honk honk”
Create a derived class that inherits from it with a model of Mustang public attribute
Create an object from the derived class and make it honk and then print its brand and model
class Vehicle {
public:
string brand = “Ford”;
void honk() {
cout «_space;“Tuut, tuut! \n” ;
}
};
// Derived class
class Car: public Vehicle {
public:
string model = “Mustang”;
};
int main() {
Car myCar;
myCar.honk();
cout «_space;myCar.brand + “ “ + myCar.model;
return 0;
}
Create a namespace for a function and use it
namespace ONE
{
void myFunc()
{
cout «_space;“this”;
}
}
ONE::myFunc()
If someone inputs a char instead of an int, how do you create a statement that doesn’t let it go through
if(std::cin.fail())
Perform exception handling where you can have 3 different options for bad input including but not limited to:
invalid_argument
out_of_range
anything else
include <bits/stdc++.h>
using namespace std;
int main() {
// Code that might throw an exception try { int choice; cout << "Enter 1 for invalid argument, " << "2 for out of range: "; cin >> choice; if (choice == 1) { throw invalid_argument("Invalid argument"); } else if (choice == 2) { throw out_of_range("Out of range"); } else { throw "Unknown error"; } } // executed when exception is of type invalid_argument catch (invalid_argument e) { cout << "Caught exception: " << e.what() << endl; } // executed when exception is of type out_of_range catch (out_of_range e) { cout << "Caught exception: " << e.what() << endl; } // executed when no matching catch is found catch (...) { cout << "Caught an unknown exception." << endl; } return 0;