Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CC-1237: Use autoflush using std::unitbuf for c++ repos #74

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
// }
}
Loading