The original post was actually for your standard “calculate the average of student test scores” kind of homework, but I very much prefer the literal interpretation of this title.
Have fun. I’ll post my version in a day or two, lol.
// “Write a C++ program that prompts the user to enter 5”
#include <iostream>
#include <regex>
#include <string>
std::string rtrim( const std::string& s ) { return s.substr( 0, s.find_last_not_of(" \f\n\r\t\v")+1 ); }
std::string ltrim( const std::string& s ) { return s.empty() ? s : s.substr( s.find_first_not_of(" \f\n\r\t\v") ); }
std::string trim( const std::string& s ) { return ltrim( rtrim( s ) ); }
int main()
{
while (true)
{
std::cout << "Please enter 5";
std::string s;
getline( std::cin, s );
if (s == std::string( 5, s.c_str()[0] )) break;
s = trim( s );
if (s == std::string( 5, s.c_str()[0] )) break;
if (regex_search( s, std::regex("five|5",std::regex_constants::icase) )) break;
if (regex_match( s, std::regex(R"<>((\S+)(\s+\1){4})<>") )) break;
if (regex_match( s, std::regex(R"<>([^,;:.\?!]+(\s*[,;:.\?!]\s*[^,;:.\?!]+){4})<>") )) break;
std::cout << "\nNope. ";
}
std::cout << "\nThank you.\n";
}
This makes sense because I entered five different numbers, but if I enter ";;;''" (3 semi-colons and 2 single quotes) it replies: "Nope".
I think 3 semi-colons and 2 single quotes is five and it should return: "Thank you."
Maybe I'm pedantic.
Now that I understand what you're asking for I think I'm going to give it a try.
Edit: After further thought, I've decided it's impossible to imagine all the possible ways a person may interpret "Please enter 5". I don't think I'm up for it.
Nope. That’s (3 semi-colons) and (2 single quotes). They may sum to five things, but they are not five — as you could not even describe them as just (5 items).
I had briefly considered adding the ability of the program to perform mathematical operations, but decided that something which in some way transforms to five (via math, for example) is not a five.
.....aaannnd, part of the point of my example program is that the program is is the arbiter of what is meant by “5” — it is the user’s job to figure out what is a 5 and what is not.
For example, entering nothing five times does not count, nor does having a sentence with five ‘E’s, nor anything less direct than repeating the same string five times.
Also, you may make a completely different program. In the context of this thread I had meant for everyone to come up with their own idea of what is meant by the prompt enter 5. I want you to use your imagination to come up with whatever you can interpret the prompt to mean.
Furry Guy chose essentially an answer with reference to Hitchhikers Guide to the Galaxy, where “42” is the answer to life, the universe, and everything. But he also added in that extra comma, which was brilliant just for its ambiguity, both in purpose and the unclarified connotation of sequentiality. It was a whole lot less literal than I had intended (not being a valid C++ program), and if anyone else tries it I will consider the attempt to be lacking and uninspired, but it works! Your first thought is “he didn’t try and just put nonsense”, but then you realize that might be more to it, perhaps beyond any rational sense of understanding besides ultimately being a very fun, excellent answer.
I had just thought that no one found this thread interesting... which may still be true...
Hey, sorry to be so late to the conversation (I'm back).
The problem isn’t the compiler, per se, but your Standard Library, which lacks the <numbers> header.
The solution is the same either way, though — you need to upgrade to the latest version of the GCC (version 10.x or later), after which you can compile using:
Well, I usually compile as C++17 with the -std=c++17, but I guess I needed the c++2a flag, which extends the compiler to C++ 20, or something like that. Something involving C++ 20.
Anyway, I'll see what I can do with regard to a newer compiler, although I use Clang++, not GCC.
C++2a was the red-headed step-child sibling of C++20.
2a was before 20 became the official standard, so might not have all the 20 features, or have implementation details that were suggested but not adopted, etc.
20 really made a jolly jack tar mess of the comparison operators IMO. I'm still trying to wrap my brain cells around the changes.
Anyway, I'll see what I can do with regard to a newer compiler, although I use Clang++, not GCC.
The options are almost the same between g++ and clang++, although each has a few extras the other doesn't have.
So -std=c++20 and -std=c++23 should work.
I recommend using the feature test macros https://en.cppreference.com/w/cpp/feature_test , so the compiler will tell one if using the wrong standard because that feature is not available with that compiler. Also helps for those using a different compiler.
I found out about this when I mucking around with for loops.
M'ok, a regular for loop, or one using iterators can transverse a container forwards or backwards. A range-based for loop only in the forward direction.
The options are almost the same between g++ and clang++, although each has a few extras the other doesn't have.
So -std=c++20 and -std=c++23 should work.
Ideally, yes...
$ c++ -std=c++20 test.cc -o test.exec
error: invalid value 'c++20' in '-std=c++20'
note: use 'c++98' or 'c++03' for 'ISO C++ 1998 with amendments' standard
note: use 'gnu++98' or 'gnu++03' for 'ISO C++ 1998 with amendments and GNU
extensions' standard
note: use 'c++11' for 'ISO C++ 2011 with amendments' standard
note: use 'gnu++11' for 'ISO C++ 2011 with amendments and GNU extensions'
standard
note: use 'c++14' for 'ISO C++ 2014 with amendments' standard
note: use 'gnu++14' for 'ISO C++ 2014 with amendments and GNU extensions'
standard
note: use 'c++17' for 'ISO C++ 2017 with amendments' standard
note: use 'gnu++17' for 'ISO C++ 2017 with amendments and GNU extensions'
standard
note: use 'c++2a' for 'Working draft for ISO C++ 2020' standard
note: use 'gnu++2a' for 'Working draft for ISO C++ 2020 with GNU extensions'
standard
$
But noope. My version of clang is just too old, I guess.
$ c++ -v
Apple LLVM version 10.0.0 (clang-1000.10.44.4)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
$