Difficult One Flashcards

1
Q

You’ve entered bad information using cin
How do you clear the input buffer to discard invalid input

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is everything you can use with fstream?

A

ifstream - input
i=eyes
read

ofstream - output
o=mouth
write

fstream - everything
f=f*ck it, you do what you want

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What can you add to your lib.h file to make sure it’s only read once?

A

pragma once <- place this at the top

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you create a variable in lib.h and define it somewhere else

A

extern uint32_t command;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you append to a file?

A

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 &laquo_space;text;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you delete everything, even the lines from a file?

A

std::ofstream file(“todo.txt”, std::ofstream::trunc)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Enter a string for cin

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Show a simple class with two attributes

create an object (instance of that class) and give values to the attributes

A

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 &laquo_space;myObj.myNum &laquo_space;“\n”;
cout &laquo_space;myObj.myString;
return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Create a template

A

<template>

T add(T a, T b) {
return a + b;
}

template is like auto
</template>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Create a function that takes in an array reference

A

RAW
void printArray(int (&arr)[5])
{
for (int i = 0; i < 5; i++)
{ cout &laquo_space;arr[i] &laquo_space;” “;
}
cout &laquo_space;endl;
}

STANDARD ARRAY
void printArray(const array<int, 5>& arr)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Read a file line by line and send it to a deque

A

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)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Clear the screen

A

include <stdlib.h></stdlib.h>

system(“cls”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

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 &laquo_space;myObj.getSalary();
return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

class Vehicle {
public:
string brand = “Ford”;
void honk() {
cout &laquo_space;“Tuut, tuut! \n” ;
}
};

// Derived class
class Car: public Vehicle {
public:
string model = “Mustang”;
};

int main() {
Car myCar;
myCar.honk();
cout &laquo_space;myCar.brand + “ “ + myCar.model;
return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Create a namespace for a function and use it

A

namespace ONE
{
void myFunc()
{
cout &laquo_space;“this”;
}
}

ONE::myFunc()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

If someone inputs a char instead of an int, how do you create a statement that doesn’t let it go through

A

if(std::cin.fail())

17
Q

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

A

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;