Skip to content

Commit

Permalink
CC-1237: Use autoflush using std::unitbuf for c++ repos
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-gang committed May 28, 2024
1 parent 7702be0 commit 1e3c198
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 154 deletions.
73 changes: 38 additions & 35 deletions compiled_starters/cpp/src/Server.cpp
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
#include <iostream>
#include <string>

bool match_pattern(const std::string& input_line, const std::string& pattern) {
if (pattern.length() == 1) {
return input_line.find(pattern) != std::string::npos;
}
else {
throw std::runtime_error("Unhandled pattern " + pattern);
}
bool match_pattern(const std::string &input_line, const std::string &pattern) {
if (pattern.length() == 1) {
return input_line.find(pattern) != std::string::npos;
} else {
throw std::runtime_error("Unhandled pattern " + pattern);
}
}

int main(int argc, char* argv[]) {
// You can use print statements as follows for debugging, they'll be visible when running tests.
std::cout << "Logs from your program will appear here" << std::endl;
int main(int argc, char *argv[]) {
// Flush after every std::cout / std::cerr
std::cout << std::unitbuf;
std::cerr << std::unitbuf;

if (argc != 3) {
std::cerr << "Expected two arguments" << std::endl;
return 1;
}
// You can use print statements as follows for debugging, they'll be visible
// when running tests.
std::cout << "Logs from your program will appear here" << std::endl;

std::string flag = argv[1];
std::string pattern = argv[2];
if (argc != 3) {
std::cerr << "Expected two arguments" << std::endl;
return 1;
}

if (flag != "-E") {
std::cerr << "Expected first argument to be '-E'" << std::endl;
return 1;
}
std::string flag = argv[1];
std::string pattern = argv[2];

// Uncomment this block to pass the first stage
//
// std::string input_line;
// std::getline(std::cin, input_line);
//
// try {
// if (match_pattern(input_line, pattern)) {
// return 0;
// } else {
// return 1;
// }
// } catch (const std::runtime_error& e) {
// std::cerr << e.what() << std::endl;
// return 1;
// }
if (flag != "-E") {
std::cerr << "Expected first argument to be '-E'" << std::endl;
return 1;
}

// Uncomment this block to pass the first stage
// std::string input_line;
// std::getline(std::cin, input_line);
//
// try {
// if (match_pattern(input_line, pattern)) {
// return 0;
// } else {
// return 1;
// }
// } catch (const std::runtime_error& e) {
// std::cerr << e.what() << std::endl;
// return 1;
// }
}
73 changes: 39 additions & 34 deletions solutions/cpp/01-cq2/code/src/Server.cpp
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@
#include <iostream>
#include <string>

bool match_pattern(const std::string& input_line, const std::string& pattern) {
if (pattern.length() == 1) {
return input_line.find(pattern) != std::string::npos;
}
else {
throw std::runtime_error("Unhandled pattern " + pattern);
}
bool match_pattern(const std::string &input_line, const std::string &pattern) {
if (pattern.length() == 1) {
return input_line.find(pattern) != std::string::npos;
} else {
throw std::runtime_error("Unhandled pattern " + pattern);
}
}

int main(int argc, char* argv[]) {
if (argc != 3) {
std::cerr << "Expected two arguments" << std::endl;
return 1;
}

std::string flag = argv[1];
std::string pattern = argv[2];

if (flag != "-E") {
std::cerr << "Expected first argument to be '-E'" << std::endl;
return 1;
}

std::string input_line;
std::getline(std::cin, input_line);

try {
if (match_pattern(input_line, pattern)) {
return 0;
} else {
return 1;
}
} catch (const std::runtime_error& e) {
std::cerr << e.what() << std::endl;
return 1;
}
int main(int argc, char *argv[]) {
// Flush after every std::cout / std::cerr
std::cout << std::unitbuf;
std::cerr << std::unitbuf;

std::cout << "Logs from your program will appear here" << std::endl;

if (argc != 3) {
std::cerr << "Expected two arguments" << std::endl;
return 1;
}

std::string flag = argv[1];
std::string pattern = argv[2];

if (flag != "-E") {
std::cerr << "Expected first argument to be '-E'" << std::endl;
return 1;
}

std::string input_line;
std::getline(std::cin, input_line);

try {
if (match_pattern(input_line, pattern)) {
return 0;
} else {
return 1;
}
} catch (const std::runtime_error& e) {
std::cerr << e.what() << std::endl;
return 1;
}
}
101 changes: 52 additions & 49 deletions solutions/cpp/01-cq2/diff/src/Server.cpp.diff
Original file line number Diff line number Diff line change
@@ -1,59 +1,62 @@
@@ -1,45 +1,40 @@
@@ -1,48 +1,45 @@
#include <iostream>
#include <string>

bool match_pattern(const std::string& input_line, const std::string& pattern) {
if (pattern.length() == 1) {
return input_line.find(pattern) != std::string::npos;
}
else {
throw std::runtime_error("Unhandled pattern " + pattern);
}
bool match_pattern(const std::string &input_line, const std::string &pattern) {
if (pattern.length() == 1) {
return input_line.find(pattern) != std::string::npos;
} else {
throw std::runtime_error("Unhandled pattern " + pattern);
}
}

int main(int argc, char* argv[]) {
- // You can use print statements as follows for debugging, they'll be visible when running tests.
- std::cout << "Logs from your program will appear here" << std::endl;
-
if (argc != 3) {
std::cerr << "Expected two arguments" << std::endl;
return 1;
}
int main(int argc, char *argv[]) {
// Flush after every std::cout / std::cerr
std::cout << std::unitbuf;
std::cerr << std::unitbuf;

std::string flag = argv[1];
std::string pattern = argv[2];
- // You can use print statements as follows for debugging, they'll be visible
- // when running tests.
std::cout << "Logs from your program will appear here" << std::endl;

if (flag != "-E") {
std::cerr << "Expected first argument to be '-E'" << std::endl;
return 1;
}
if (argc != 3) {
std::cerr << "Expected two arguments" << std::endl;
return 1;
}

- // Uncomment this block to pass the first stage
- //
- // std::string input_line;
- // std::getline(std::cin, input_line);
- //
- // try {
- // if (match_pattern(input_line, pattern)) {
- // return 0;
- // } else {
- // return 1;
- // }
- // } catch (const std::runtime_error& e) {
- // std::cerr << e.what() << std::endl;
- // return 1;
- // }
+ std::string input_line;
+ std::getline(std::cin, input_line);
std::string flag = argv[1];
std::string pattern = argv[2];

if (flag != "-E") {
std::cerr << "Expected first argument to be '-E'" << std::endl;
return 1;
}

- // Uncomment this block to pass the first stage
- // std::string input_line;
- // std::getline(std::cin, input_line);
- //
- // try {
- // if (match_pattern(input_line, pattern)) {
- // return 0;
- // } else {
- // return 1;
- // }
- // } catch (const std::runtime_error& e) {
- // std::cerr << e.what() << std::endl;
- // return 1;
- // }
+ std::string input_line;
+ std::getline(std::cin, input_line);
+
+ try {
+ if (match_pattern(input_line, pattern)) {
+ return 0;
+ } else {
+ return 1;
+ }
+ } catch (const std::runtime_error& e) {
+ std::cerr << e.what() << std::endl;
+ return 1;
+ }
+ try {
+ if (match_pattern(input_line, pattern)) {
+ return 0;
+ } else {
+ return 1;
+ }
+ } catch (const std::runtime_error& e) {
+ std::cerr << e.what() << std::endl;
+ return 1;
+ }
}
1 change: 0 additions & 1 deletion solutions/cpp/01-cq2/explanation.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Study and uncomment the relevant code:

```cpp
// Uncomment this block to pass the first stage

std::string input_line;
std::getline(std::cin, input_line);

Expand Down
73 changes: 38 additions & 35 deletions starter_templates/cpp/src/Server.cpp
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
#include <iostream>
#include <string>

bool match_pattern(const std::string& input_line, const std::string& pattern) {
if (pattern.length() == 1) {
return input_line.find(pattern) != std::string::npos;
}
else {
throw std::runtime_error("Unhandled pattern " + pattern);
}
bool match_pattern(const std::string &input_line, const std::string &pattern) {
if (pattern.length() == 1) {
return input_line.find(pattern) != std::string::npos;
} else {
throw std::runtime_error("Unhandled pattern " + pattern);
}
}

int main(int argc, char* argv[]) {
// You can use print statements as follows for debugging, they'll be visible when running tests.
std::cout << "Logs from your program will appear here" << std::endl;
int main(int argc, char *argv[]) {
// Flush after every std::cout / std::cerr
std::cout << std::unitbuf;
std::cerr << std::unitbuf;

if (argc != 3) {
std::cerr << "Expected two arguments" << std::endl;
return 1;
}
// You can use print statements as follows for debugging, they'll be visible
// when running tests.
std::cout << "Logs from your program will appear here" << std::endl;

std::string flag = argv[1];
std::string pattern = argv[2];
if (argc != 3) {
std::cerr << "Expected two arguments" << std::endl;
return 1;
}

if (flag != "-E") {
std::cerr << "Expected first argument to be '-E'" << std::endl;
return 1;
}
std::string flag = argv[1];
std::string pattern = argv[2];

// Uncomment this block to pass the first stage
//
// std::string input_line;
// std::getline(std::cin, input_line);
//
// try {
// if (match_pattern(input_line, pattern)) {
// return 0;
// } else {
// return 1;
// }
// } catch (const std::runtime_error& e) {
// std::cerr << e.what() << std::endl;
// return 1;
// }
if (flag != "-E") {
std::cerr << "Expected first argument to be '-E'" << std::endl;
return 1;
}

// Uncomment this block to pass the first stage
// std::string input_line;
// std::getline(std::cin, input_line);
//
// try {
// if (match_pattern(input_line, pattern)) {
// return 0;
// } else {
// return 1;
// }
// } catch (const std::runtime_error& e) {
// std::cerr << e.what() << std::endl;
// return 1;
// }
}

0 comments on commit 1e3c198

Please sign in to comment.