• Re: Isn't that beauty ? (no it's not)

    From Bonita Montero@Bonita.Montero@gmail.com to comp.lang.c,comp.lang.c++ on Thu Mar 12 15:03:22 2026
    From Newsgroup: comp.lang.c++

    Am 12.03.2026 um 15:00 schrieb DFS:
    On 3/12/2026 2:24 AM, Bonita Montero wrote:
    There was a programming-"contest" on comp.lang.c and I wanted to show
    the simpler code in C, here it is:

    #include <iostream>
    #include <sstream>
    #include <iomanip>
    #include <optional>
    #include <algorithm>

    using namespace std;

    static optional<size_t> parse( const char *str );

    int main( int argc, char **argv )
    {
    -a-a-a-a-aif( argc < 3 )
    -a-a-a-a-a-a-a-a return EXIT_FAILURE;
    -a-a-a-a-aoptional<size_t>
    -a-a-a-a-a-a-a-a pRows = parse( argv[1] ),
    -a-a-a-a-a-a-a-a pCols = parse( argv[2] );
    -a-a-a-a-aif( !pRows || !pCols )
    -a-a-a-a-a-a-a-a return EXIT_FAILURE;
    -a-a-a-a-asize_t rows = *pRows, cols = *pCols;
    -a-a-a-a-aoptional<size_t> pClip( rows * cols );
    -a-a-a-a-aif( argc >= 4 && !(pClip = parse( argv[3] )) )
    -a-a-a-a-a-a-a-a return EXIT_FAILURE;
    -a-a-a-a-asize_t clip = min( *pClip, rows * cols );
    -a-a-a-a-astreamsize width = (ostringstream() << clip).str().length();
    -a-a-a-a-afor( size_t row = 1; row <= min( rows, clip ); ++row )
    -a-a-a-a-a{
    -a-a-a-a-a-a-a-a bool head = true;
    -a-a-a-a-a-a-a-a for( size_t value = row; value <= clip; value += rows, head =
    false )
    -a-a-a-a-a-a-a-a-a-a-a-a cout << " "sv.substr( head, !head ) << right <<
    setw( width ) << value;
    -a-a-a-a-a-a-a-a cout << endl;
    -a-a-a-a-a}
    }

    static optional<size_t> parse( const char *str )
    {
    -a-a-a-a-aistringstream iss( str );
    -a-a-a-a-asize_t ret;
    -a-a-a-a-aiss >> ret;
    -a-a-a-a-aif( !iss || !iss.eof() )
    -a-a-a-a-a-a-a-a return nullopt;
    -a-a-a-a-areturn ret;
    }

    C++ really rocks since you've to deal with much less details than in C.



    Is your strategy to just ignore reality, and keep making bogus claims
    that - for this challenge at least - you can't support?

    Where's your check for parsing errors ? Your code would be longer with
    that.
    But real C++ code is usually multiple times shorter, mostly because of
    generic programming. C really sucks since you have to flip every bit
    on your own.




    #include <stdlib.h>
    #include <stdio.h>

    int main(int argc, char *argv[]) {
    -a-a-a-aif (argc < 3 || argc > 4) {
    -a-a-a-a-a-a-a printf("Enter 2 or 3 arguments:\n$./prog rows columns [stop]\n");
    -a-a-a-a-a-a-a return 0;
    -a-a-a-a}

    -a-a-a-aint rows = atoi(argv[1]);
    -a-a-a-aint cols = atoi(argv[2]);
    -a-a-a-aint max-a = (argc == 4) ? (atoi(argv[3])) : (rows * cols);
    -a-a-a-achar cw[12];
    -a-a-a-aint colwidth = sprintf(cw,"%d ",rows * cols);

    -a-a-a-afor (int r = 1; r <= rows; r++) {
    -a-a-a-a-a-a-a if (r <= max) {
    -a-a-a-a-a-a-a-a-a-a-a int nbr = r;
    -a-a-a-a-a-a-a-a-a-a-a printf("%*d", colwidth, nbr);
    -a-a-a-a-a-a-a-a-a-a-a for (int i = 0; i < (cols - 1); i++) {
    -a-a-a-a-a-a-a-a-a-a-a-a-a-a-a ((nbr += rows) <= max) ? (printf("%*d", colwidth,
    nbr)) : (0) ;
    -a-a-a-a-a-a-a-a-a-a-a }
    -a-a-a-a-a-a-a-a-a-a-a printf("\n");
    -a-a-a-a-a-a-a }
    -a-a-a-a}

    -a-a-a-areturn 0;

    }

    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From tTh@tth@none.invalid to comp.lang.c,comp.lang.c++ on Thu Mar 12 15:27:50 2026
    From Newsgroup: comp.lang.c++

    On 3/12/26 15:03, Bonita Montero wrote:

    But real C++ code is usually multiple times shorter, mostly because of generic programming. C really sucks since you have to flip every bit
    on your own.

    C++ really sucks because you can't know who flip the bits.
    --
    ** **
    * tTh des Bourtoulots *
    * http://maison.tth.netlib.re/ *
    ** **
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Bonita Montero@Bonita.Montero@gmail.com to comp.lang.c,comp.lang.c++ on Thu Mar 12 15:34:23 2026
    From Newsgroup: comp.lang.c++

    Am 12.03.2026 um 15:27 schrieb tTh:
    On 3/12/26 15:03, Bonita Montero wrote:

    But real C++ code is usually multiple times shorter, mostly because of
    generic programming. C really sucks since you have to flip every bit
    on your own.

    -a-a C++ really sucks because you can't know who flip the bits.

    Absolutely not, C++ can anyhing you want if you need that. But in C++
    you often doesn't need that. Bad programmers which don't understand
    that are rare since C++ has very high skill demands.

    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Bonita Montero@Bonita.Montero@gmail.com to comp.lang.c,comp.lang.c++ on Thu Mar 12 16:10:41 2026
    From Newsgroup: comp.lang.c++

    Am 12.03.2026 um 15:43 schrieb DFS:
    On 3/12/2026 10:13 AM, Bonita Montero wrote:
    Here, do that in C:

    static optional<size_t> parse( const char *str )
    {
    -a-a-a-a-asize_t ret;
    -a-a-a-a-aif( from_chars_result fcr = from_chars( str, str + strlen( str ), >> ret ); (bool)fcr.ec || *fcr.ptr )
    -a-a-a-a-a-a-a-a return nullopt;
    -a-a-a-a-areturn ret;
    }


    Explain in detail what it does.

    If that isn't self-explanatory stick with C.

    I've got a task for you: Do the same in C:

    #include <iostream>
    #include <regex>
    #include <fstream>
    #include <vector>
    #include <algorithm>
    #include <vector>

    using namespace std;

    int main( int argc, char **argv )
    {
    if( argc < 2 )
    return EXIT_FAILURE;
    ifstream ifs( argv[1] );
    static regex rxNameTel( "^\\s*\"([^\"]*)\"\\s*\"([^\"]*)\"\\s*$" );
    struct name_tel { string name, tel; };
    vector<name_tel> phoneList;
    while( !ifs.eof() )
    {
    string line;
    getline( ifs, line );
    match_results<string::const_iterator> sm;
    if( regex_match( line, sm, rxNameTel ) )
    phoneList.emplace_back( string( sm[1].first, sm[1].second ), string(
    sm[2].first, sm[2].second ) );
    }
    sort( phoneList.begin(), phoneList.end(),
    []( const name_tel &left, const name_tel &right ) { return left.name <
    right.name; } );
    for( name_tel &phone : phoneList )
    cout << "\"" << phone.name << "\"\t\"" << phone.tel << "\"" << endl;
    }

    1. Read a file and parse it with ne mentioned regex-pattern.
    2. Split both parts of every line in two strings.
    3. Sort the "vector" according to the first string.
    4. Print it.

    I guess you don't manage to do that with less than five times the work.
    Every external lib allowed.

    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Bonita Montero@Bonita.Montero@gmail.com to comp.lang.c,comp.lang.c++ on Thu Mar 12 16:44:01 2026
    From Newsgroup: comp.lang.c++

    Am 12.03.2026 um 16:10 schrieb Bonita Montero:

    -a-a-a-astatic regex rxNameTel( "^\\s*\"([^\"]*)\"\\s*\"([^\"]*)\"\\s*$" );

    Easier to read as a C++ raw string: R"~(^\s*"([^"]*)"\s*"([^"]*)"\s*$)~"
    The regex is between ~( and )~.

    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From scott@scott@slp53.sl.home (Scott Lurndal) to comp.lang.c,comp.lang.c++ on Thu Mar 12 17:32:08 2026
    From Newsgroup: comp.lang.c++

    Bonita Montero <Bonita.Montero@gmail.com> writes:
    Am 12.03.2026 um 15:43 schrieb DFS:
    On 3/12/2026 10:13 AM, Bonita Montero wrote:
    Here, do that in C:

    static optional<size_t> parse( const char *str )
    {
    -a-a-a-a-asize_t ret;
    -a-a-a-a-aif( from_chars_result fcr = from_chars( str, str + strlen( str ),
    ret ); (bool)fcr.ec || *fcr.ptr )
    -a-a-a-a-a-a-a-a return nullopt;
    -a-a-a-a-areturn ret;
    }


    Explain in detail what it does.

    If that isn't self-explanatory stick with C.

    I've got a task for you: Do the same in C:

    #include <iostream>
    #include <regex>
    #include <fstream>
    #include <vector>
    #include <algorithm>
    #include <vector>

    using namespace std;

    int main( int argc, char **argv )
    {
    if( argc < 2 )
    return EXIT_FAILURE;

    A silent failure implies to the interactive user
    that the command succeeded. You should print
    a responsive message before returning a failure code.

    cerr << "Usage: " << argv[0] << " " <phonelist>" << eol;

    (although I would prefer fprintf(stderr, ...)).


    ifstream ifs( argv[1] );

    You didn't bother to check if the file was opened.
    You should print a responsive message and exit if the streams
    failbit is set.

    static regex rxNameTel( "^\\s*\"([^\"]*)\"\\s*\"([^\"]*)\"\\s*$" );
    struct name_tel { string name, tel; };
    vector<name_tel> phoneList;
    while( !ifs.eof() )
    {
    string line;
    getline( ifs, line );
    match_results<string::const_iterator> sm;
    if( regex_match( line, sm, rxNameTel ) )
    phoneList.emplace_back( string( sm[1].first, sm[1].second ), string(
    sm[2].first, sm[2].second ) );
    }
    sort( phoneList.begin(), phoneList.end(),
    []( const name_tel &left, const name_tel &right ) { return left.name <
    right.name; } );
    for( name_tel &phone : phoneList )
    cout << "\"" << phone.name << "\"\t\"" << phone.tel << "\"" << endl;
    }

    1. Read a file and parse it with ne mentioned regex-pattern.
    2. Split both parts of every line in two strings.
    3. Sort the "vector" according to the first string.
    4. Print it.

    This is certainly a task that can be done simply using a shell
    script and the standard POSIX utility set. awk(1) would be
    a good starting point; it's possible that it can be done with
    a single invocation of the posix 'sort' utility.

    Of course, the lack of any in-line documentation (e.g. comments) is
    a typical defect in your C++ code.

    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Bonita Montero@Bonita.Montero@gmail.com to comp.lang.c,comp.lang.c++ on Fri Mar 13 00:56:21 2026
    From Newsgroup: comp.lang.c++

    Am 12.03.2026 um 18:32 schrieb Scott Lurndal:

    You didn't bother to check if the file was opened.

    That's not necessary to compare it against a equal solution in C.


    This is certainly a task that can be done simply using a shell
    script and the standard POSIX utility set. awk(1) would be
    a good starting point; it's possible that it can be done with
    a single invocation of the posix 'sort' utility.

    It's comparison of C against C++.

    Of course, the lack of any in-line documentation (e.g. comments) is
    a typical defect in your C++ code.

    Complete idiot.

    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Bonita Montero@Bonita.Montero@gmail.com to comp.lang.c,comp.lang.c++ on Fri Mar 13 01:14:00 2026
    From Newsgroup: comp.lang.c++

    Am 12.03.2026 um 18:32 schrieb Scott Lurndal:

    You didn't bother to check if the file was opened.

    If the file couldn't been opened nothing happens.
    No difference to a check.
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Bonita Montero@Bonita.Montero@gmail.com to comp.lang.c,comp.lang.c++ on Fri Mar 13 01:27:52 2026
    From Newsgroup: comp.lang.c++

    I shortened my code a bit.
    Do that:

    #include <iostream>
    #include <regex>
    #include <fstream>
    #include <vector>
    #include <algorithm>

    using namespace std;

    int main( int argc, char **argv )
    {
    if( argc < 2 )
    return EXIT_FAILURE;
    ifstream ifs( argv[1] );
    static regex rxNameTel( R"~(^\s*"([^"]*)"\s*"([^"]*)"\s*$)~" );
    struct name_tel { string name, tel; };
    vector<name_tel> phoneList;
    smatch sm;
    for( string line; getline( ifs, line ); )
    if( regex_match( line, sm, rxNameTel ) )
    phoneList.emplace_back( sm[1].str(), sm[2].str() );
    sort( phoneList.begin(), phoneList.end(),
    []( name_tel &left, name_tel &right ) { return left.name < right.name;
    } );
    for( name_tel &phone : phoneList )
    cout << "\"" << phone.name << "\"\t\"" << phone.tel << "\"" << endl;
    }

    Have a closer look at the regex.
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Chris M. Thomasson@chris.m.thomasson.1@gmail.com to comp.lang.c,comp.lang.c++ on Fri Mar 13 11:54:23 2026
    From Newsgroup: comp.lang.c++

    On 3/12/2026 4:56 PM, Bonita Montero wrote:
    Am 12.03.2026 um 18:32 schrieb Scott Lurndal:

    You didn't bother to check if the file was opened.

    That's not necessary to compare it against a equal solution in C.


    This is certainly a task that can be done simply using a shell
    script and the standard POSIX utility set.-a awk(1) would be
    a good starting point; it's possible that it can be done with
    a single invocation of the posix 'sort' utility.

    It's comparison of C against C++.

    Of course, the lack of any in-line documentation (e.g. comments) is
    a typical defect in your C++ code.

    Complete idiot.


    Scott is the opposite of idiot.
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Bonita Montero@Bonita.Montero@gmail.com to comp.lang.c,comp.lang.c++ on Sat Mar 14 06:01:53 2026
    From Newsgroup: comp.lang.c++

    Am 13.03.2026 um 21:11 schrieb DFS:
    On 3/12/2026 8:27 PM, Bonita Montero wrote:
    I shortened my code a bit.
    Do that:

    #include <iostream>
    #include <regex>
    #include <fstream>
    #include <vector>
    #include <algorithm>

    using namespace std;

    int main( int argc, char **argv )
    {
    -a-a-a-a-aif( argc < 2 )
    -a-a-a-a-a-a-a-a return EXIT_FAILURE;
    -a-a-a-a-aifstream ifs( argv[1] );
    -a-a-a-a-astatic regex rxNameTel( R"~(^\s*"([^"]*)"\s*"([^"]*)"\s*$)~" );
    -a-a-a-a-astruct name_tel { string name, tel; };
    -a-a-a-a-avector<name_tel> phoneList;
    -a-a-a-a-asmatch sm;
    -a-a-a-a-afor( string line; getline( ifs, line ); )
    -a-a-a-a-a-a-a-a if( regex_match( line, sm, rxNameTel ) )
    -a-a-a-a-a-a-a-a-a-a-a-a phoneList.emplace_back( sm[1].str(), sm[2].str() ); >> -a-a-a-a-asort( phoneList.begin(), phoneList.end(),
    -a-a-a-a-a-a-a-a []( name_tel &left, name_tel &right ) { return left.name < >> right.name; } );
    -a-a-a-a-afor( name_tel &phone : phoneList )
    -a-a-a-a-a-a-a-a cout << "\"" << phone.name << "\"\t\"" << phone.tel << "\""
    << endl;
    }

    Have a closer look at the regex.


    Got a massive set of compile errors:

    = -std=c++20
    --- Synchronet 3.21d-Linux NewsLink 1.2
  • From Bonita Montero@Bonita.Montero@gmail.com to comp.lang.c,comp.lang.c++ on Sat Mar 14 07:23:45 2026
    From Newsgroup: comp.lang.c++

    Am 14.03.2026 um 06:49 schrieb DFS:

    Your output is messy.-a What you want to do is iterate the data and find
    the longest name, then pad spaces after the name so the phone numbers
    line up.

    No, alphabetically ordered. My code exactly does that.

    88 LOC
    16.84MB executable
    perfect output


    $ ./dfs names-numbers-unsorted-montero.txt
    -a1. Anna Becker-a-a-a-a-a-a-a-a-a 0170-2233445
    ...


    My output is the same without line numbers:

    "Anna Becker" "0170-2233445"
    "Anna Fischer" "0341-9988776"
    "Anna M|+ller" "0987-6543210"
    "Ben Meier" "0341-5566778"
    "Ben Richter" "069-3344556"
    "Clara Hofmann" "0157-2233445"
    "Clara Zimmermann" "040-5566778"
    "David Schulz" "030-9988776"
    "Emma Bauer" "0157-9988776"
    "Emma Wolf" "040-5566778"
    "Felix Hoffmann" "0711-5566778"
    "Felix Neumann" "0170-2233445"
    "Hannah Wagner" "0221-5566778"
    "Jan Hoffmann" "0711-3344556"
    "Jana Zimmer" "030-6677889"
    "Jonas Klein" "0171-4455667"
    "Jonas Klein" "030-4455667"
    "Julia Neumann" "0221-3344556"
    "Julia Schulz" "0228-4455667"
    "Laura Fischer" "040-9876543"
    "Laura Schulze" "0228-1122334"
    "Lea Richter" "0341-1122334"
    "Lea Wagner" "0151-3344556"
    "Lena Fischer" "0151-6677889"
    "Leon Krause" "089-1122334"
    "Leon Zimmermann" "0711-1122445"
    "Leonie Klein" "0341-5566778"
    "Lukas Hofmann" "089-6677889"
    "Lukas K||nig" "069-7788990"
    "Marie Becker" "040-4455667"
    "Marie Richter" "0221-7788990"
    "Max Mustermann" "0123-4567890"
    "Maximilian Keller" "030-1122334"
    "Mia Keller" "089-6677889"
    "Michael Braun" "0170-9988776"
    "Moritz Wolf" "0157-4455667"
    "Nina Krause" "0341-4455667"
    "Paul Sch|nfer" "0228-3344556"
    "Paul Wolf" "030-2233445"
    "Peter Schmidt" "030-1234567"
    "Philipp K||nig" "089-9988776"
    "Sarah Braun" "040-7788990"
    "Sarah Lehmann" "040-2233445"
    "Simon Meier" "030-7788990"
    "Sophie Neumann" "0711-3344556"
    "Sophie Wagner" "089-2233445"
    "Tim Becker" "0151-1112223"
    "Tim Richter" "030-6677889"
    "Tim Sch|nfer" "0711-5566778"
    "Tom Bauer" "0171-1122334"

    But your code is 101 lines of code, my 25. C really sucks.
    --- Synchronet 3.21d-Linux NewsLink 1.2