• Forum
  • Lounge
  • Write a C++ program that prompts the use

 
Write a C++ program that prompts the user to enter 5

Pages: 123
Recent post title on SO (now deleted).

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.
42,
Boo! That program won’t compile until a HHGTTG sequel!

I’ll accept it.


Here’s my time waster:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// “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";
}

Meh. Sorry. Dumb “challenge”.
I gotta admit that slice of code is rad and most excellent for being so verbose and fluffed up to the point of incomprehensibility.

Very good choice!
If I enter: "12345", it replies: "Thank you."

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.
Last edited on
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...
Actually there are a couple of spaces after the 42 & comma, so it does have a connection to 5. In a meta-theological sense.

That's my story and, dammit, I'm stickin' to it!
A bit of C++20 magic and obfuscation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cmath>
#include <iostream>
#include <numbers>

struct two_t { };
template <class T>
constexpr auto operator^(T base, two_t) { return base * base; }

int main()
{
   using namespace std::numbers;
   constexpr two_t ²;

   std::cout << "The answer is " <<
      (((std::sin(e) ^ ²) + (std::cos(e) ^ ²)) +
       std::pow(e, ln2) + std::sqrt(pi) * inv_sqrtpi +
       ((std::cosh(pi) ^ ²) - (std::sinh(pi) ^ ²)) +
       sqrt3 * inv_sqrt3 * log2e * ln2 * log10e * ln10 *
       pi * inv_pi + (phi * phi - phi)) *
      ((sqrt2 * sqrt3) ^ ²) << '\n';
}
untitled:3:10: fatal error: 'numbers' file not found
#include <numbers>
         ^~~~~~~~~
1 error generated.

Crap...it won't work. Is <numbers> a Windows/DOS thing, like <conio.h> or <windows.h>?
Last edited on
That code snippet is chock full of C++ 20 mathematical constants:

https://en.cppreference.com/w/cpp/numeric/constants

I modified the snippet -- shortened it -- for one single, solitary item of output. An allusion to my previous comments.

I especially like the templated operator^ over-ride. Sweet trick.
D'oh! No wonder! TextMate's compiler is stuck on C++11 unfortunately, so I didn't catch that.

[Edit] Well shoot me in the foot! It still gives me the error.
$ c++ -std=c++2a -o a.out -Wall -Wextra -pedantic untitled.cc
  untitled.cc:3:10: fatal error: 'numbers' file not found
  #include <numbers>
           ^~~~~~~~~
  1 error generated.
$

Hmm, what about c++2b?
$ c++ -std=c++2b -o a.out -Wall -Wextra -pedantic untitled.cc
  error: invalid value 'c++2b' in '-std=c++2b'
  1 error generated.
$

Well! This is aggravating.
Last edited on
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:

  g++ -std=c++20 ...
Last edited on
I even mentioned right off the code I slapped up is chock full of C++20 magic. :|
...yes, but knowing what that means is magic in itself... many people who aren’t even beginners don’t know how to frob standards compliance flags.
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.
agent max wrote:
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.
With Visual Studio 2019 or 2022 there is at least one feature of C++20, <ranges>, that requires using -std=c++latest instead of -std=c++20.

1>The contents of <ranges> are available only in c++latest mode with concepts support;
1>see https://github.com/microsoft/STL/issues/1814 for details.

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.

Well, no longer true since C++20 apparently.
https://www.fluentcpp.com/2020/02/11/reverse-for-loops-in-cpp/

Neato! I throw together a quick batch of test code and BLAMMO! VS went all loopy with -std=c++20. *pout*

M'ok, I read the above "warning", and I enabled -std=c++latest. Voila! The frikkin' thing works!

Using Code::Blocks with the latest MinGW64 from MSYS2 and I don't need to go latest. -std=c++20 works.

For anyone interested here's my Frankenstein Monster test code (It also tests the new C++20 numeric constants):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
#include <ranges>
#include <numbers>

int main()
{
   std::string cat { "cat" };

   std::cout << cat << '\n';

   // https://en.cppreference.com/w/cpp/language/range-for
   for (const auto& itr : cat) { std::cout << itr << ' '; }
   std::cout << '\n';

   // https://www.fluentcpp.com/2020/02/11/reverse-for-loops-in-cpp/
   for (const auto& itr : cat | std::views::reverse) { std::cout << itr << ' '; }
   std::cout << "\n\n";

   // range-based for loops work with regular arrays as well
   int arr[] { 5, 10, 15, 20, 25 };

   for (const auto& itr : arr) { std::cout << itr << ' '; }
   std::cout << '\n';

   for (const auto& itr : arr | std::views::reverse) { std::cout << itr << ' '; }
   std::cout << '\n';

   std::cout << "\nPi: " << std::numbers::pi << '\n';
}
cat
c a t
t a c

5 10 15 20 25
25 20 15 10 5

Pi: 3.14159
TheIdeasMan wrote:
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
$
Last edited on
Pages: 123