Lecture 1
Lecture 1
Programming
Junaid Haroon Siddiqui
Welcome
• Hands on course
• in class examples
• assignments
Books
• 2 books
int main()
char a;
do {
a = std::cin.get();
std::cout << a;
} while (a!='x');
return 0;
}
• lets compile and run
• g++ -o example1 example1.cc
• ./example1
Our second
#include <iostream>
#include <fstream>
example
int main()
char a = '-';
do {
a = f.get();
f.close();
} while (a!='x');
return 0;
}
• lets compile and run
• what does echo command do
• few issues like busy wait but lets
leave them on a side for now
So far so good
int main()
{
char a = '-', b;
do {
if (IsKeyboardPressed()) {
b = std::cin.get();
std::cout << b << std::endl;
}
std::ifstream f("test.txt", std::ifstream::in);
if (f.good() && a != f.peek()) {
a = f.get();
std::cout << a << std::endl;
}
f.close();
} while (a!='x' && b!='x');
return 0;
}
•
int onKeyPress()
Refactore
{
char b = std::cin.get();
std::cout << b << std::endl;
return b == 'x';
d
}
int onFileChanged()
{
char a = f.get();
std::cout << a << std::endl;
return a == 'x';
}
int main()
{
char a = '-', b;
do {
if (IsKeyboardPressed()) {
if (onKeyPress())
return 0;
}
std::ifstream f("test.txt", std::ifstream::in);
if (f.good() && a != f.peek()) {
if (onFileChanged())
return 0;
}
f.close();
} while (true);
return 0;
}
Pros and Cons of
this approach
• One thing at a time (more focused)
• Blocking is a huge issue
• Nothing is parallel
• Once you understand the
approach, it scales well
Revise
Next time