- Stackoverflow - a fascist web-site. - 8 Updates
- "Bjarne Stroustrup announces C++ Core Guidelines" - 1 Update
- io.h, reading contents of a directory, ubuntu 14.04 - 3 Updates
- using #define vs const int , in error codes - 10 Updates
- std::list with custom allocator - 1 Update
- About my SemaCondvar and SemaMonitor - 1 Update
- cmsg cancel <n2b5ua$goj$6@dont-email.me> - 1 Update
Geoff <geoff@invalid.invalid>: Nov 15 04:13PM -0800 On Sat, 14 Nov 2015 10:01:14 -0800 (PST), Vlad from Moscow >This code does not satisfy the question because in the question variable key is defined the following way >char key[5]="april"; >So your code is wrong and does not solve the problem described in the question. On the contrary, I answered the question posed in the title: How to compare strings. My solutions addressed his misunderstanding of how C strings are initialized and how to compare strings for exact match. The char key[5] = "april" is a frequent novice mistake when initializing C strings and the question is clearly from a novice who is intending to use strings as arrays rather than character arrays. So he forgets the extra null character in the key, then he gets a C string from scanf and tries to compare it to the key invalid C string in key using ==, probably because he comes to C from another language that knows what to do with == when comparing strings, such as C++, for instance, and is surprised when his program doesn't behave as he expected when handling what he thinks are strings. Incidentally, you missed an opportunity to criticize my C solution since it uses fgets and fails to take into account the '\n' returned in the string. This was a carry-over from some experimentation I was doing and was posted by mistake. Correct C solution: #include <stdio.h> #include <string.h> int main(void){ char key[] = "april"; // auto size array as string char ckey[10]; printf("Enter the key: "); scanf("%9s", ckey); // strings must match exactly. if(strlen(key) == strlen(ckey) && !strcmp(key, ckey)) { printf("Correct.\n"); } else { printf("Wrong.\n"); } return 0; } |
Geoff <geoff@invalid.invalid>: Nov 15 04:17PM -0800 On Sat, 14 Nov 2015 09:56:21 -0800 (PST), Vlad from Moscow >> Your answer was not absolutely correct. It fails to say "Wrong." when >> the input does not exactly match the key. A serious defect. >It is interesting to me when does it fail to say "wrong" when the input does not exactly match the key?:) Your solution fails to say "Wrong" and says "Correct" when the input matches the first five characters but doesn't exactly match. Input such as aprila, aprilaaaaaaaaaa, all produce "Correct when in fact this would be erroneous since the arrays don't exactly match. |
Vlad from Moscow <vlad.moscow@mail.ru>: Nov 16 12:25AM -0800 On Monday, November 16, 2015 at 3:17:52 AM UTC+3, Geoff wrote: > matches the first five characters but doesn't exactly match. Input > such as aprila, aprilaaaaaaaaaa, all produce "Correct when in fact > this would be erroneous since the arrays don't exactly match. I am sorry but you are very low qualified programmer. You are even unable to understand what is written in questions and what you can do is only read titles of questions.:) Usually bubbies also are able to read only titles.:) I'll help you. You can enter neither "aprila" nor "aprilaaaaaaaaaa" because the character array used to store the entered string has only 6 characters.:) Such a task occurs very often when for example you read some data from a file or other source and the record contains an identification field. And you need to check that identification field. So usually programs contain valid identification strings that have the required size and compare them with this identification field. Or they need to compare a part of a string with the identification field that for example to be sure that the string starts with the identification's characters. So your answer has nothing common with the code presented in the question. I advice you to think about that titles does not reflect entirely a problem. There is no enough space in the title that to describe a problem. So titles are very often have too common meaning. For example a title can sound like "How to delete an element in a vector".:) How are you going to answer a question with such a title? What is the vector? Is it standard container std::vector? Or is it just an array? Neither the first nor the second! It is a user-defined single-linked list that was called vector in some question.:) It is the content of the question it is the code presented in the question that has to be taken into account. And moreover in fact the title of this question does not contradict to the code example shown in the question. Indeed he need to compare a string with another in fact string that does not have the terminating zero. It can be considered as a task to compare of a substring with a given number of characters with other string that has no more than the given number of characters plus one character for the terminating zero. So you are able to understand neither the question nor the correct code I showed in my answer. Programming is not your speciality.:) |
SG <s.gesemann@gmail.com>: Nov 16 04:09AM -0800 On Monday, November 16, 2015 at 9:25:59 AM UTC+1, Vlad from Moscow wrote: > I'll help you. You can enter neither "aprila" nor "aprilaaaaaaaaaa" > because the character array used to store the entered string has > only 6 characters.:) Of course, a user can enter "aprila" or even "aprilaaaaaaaaaa". You just ignore anything beyond 5 characters by using a short array and "%5s" as scanf string. And that's the issue because it makes your program accept "aprila" and "aprilaaaaaaaaaa" as correct key regardless of whether you store the user input in full or just up to the first five characters in the array named key. While char ckey[5] = "april"; is valid in C, I don't think it's good practice. You did not address the danger of this approach and the intricacies of C string handling w.r.t. null terminators. > [...] > Programming is not your speciality.:) > [...] One major factor in why your answer got downvoted and you got blocked is this kind of attitude of yours, probably. You chose to engage in an edit war about whether or not it's OK to insult other SO members as part of your answer. |
Vlad from Moscow <vlad.moscow@mail.ru>: Nov 16 04:42AM -0800 On Monday, November 16, 2015 at 3:10:25 PM UTC+3, SG wrote: > is this kind of attitude of yours, probably. You chose to engage in > an edit war about whether or not it's OK to insult other SO members as > part of your answer. It is very interesting can you say what are there in the array of 6 characters that contains a string beyond the five characters?:) As for practise then i will repeat for such unexperienced weak programmers as you that it occurs very othen when you deal with records read from fro example a file or network. Usually some raw data contains identification fields. Consider for example a record that contains a field either with "XML " or "HTML" (onlky four characters neither zero character). And you need to identify the record. In this case you can declare a character array of 5 characters initialized it with either "XML",'\0' or "HTML",'\0' and check the identification field as I shwoed in the answer. One more for such unquakified programmers as you. This is valid correct code. You could suggest to redesign the application (though I doubt because the record structure might exist apriori). But in any case it does not mean that my answer is incorrect. My answer was down-voted for two reasons. The first one is that in the West there are very many low-qualified programmers that even do not know that such an initialization for character arrays in C is valid. It is seen very well from the comment to my answer. There were other similar comments but they were deleted by the moderator. And the second reason is the fascism that thrives in the West including the Stackoverflow. It is so obvious that what you are doing is trying to find a reason to accuse me and to invent something that to say that my entirely correct answer is wrong.:) |
Christian Gollwitzer <auriocus@gmx.de>: Nov 16 02:10PM +0100 Am 16.11.15 um 09:25 schrieb Vlad from Moscow: > I am sorry but you are very low qualified programmer. You are even unable to understand what is written in questions and what you can do is only read titles of questions.:) > Usually bubbies also are able to read only titles.:) ... and this is why you got banned from the "fascist" web site. Christian |
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Nov 16 02:12PM +0100 On 11/16/2015 1:42 PM, Vlad from Moscow wrote: >> an edit war about whether or not it's OK to insult other SO members as >> part of your answer. > As for practise then i will repeat for such unexperienced weak programmers as you Google is your friend Vlad, even if you think we're not. Judging from his postings in clc++, Sebastian has quite extensive experience, and is not at all a weak programmer. Googling him & his research bears that out. I think the problem is that you're trying to argue that you were technically right, believing that nobody here UNDERSTAND your answer (just because one or two didn't), when in fact the technical has been cleared up long ago and people such as Sebastian are trying in a very friendly non-adversary way to help you understand what you did wrong. So, what you did wrong: • You focused on a single, minor technical aspect; one that considering the OP's level of expertise was unlikely to be relevant. • You engaged in a flame war. • You did not properly adjust to changing circumstances, such as recognizing that the people you talk to here, as opposed to on SO, UNDERSTAND the technical point about C versus C++ rules etc. So that's what to improve: (1) try much harder to adjust to continually changing circumstances, (2) opt out of flame wars that you are not intentionally trying to prolong, e.g. enlist the help of moderators in such cases, and (3) look beyond your first impression and first obvious issue, take a wider view where you consider the relevance of each thing. Cheers & hth., - Alf |
asetofsymbols@gmail.com: Nov 16 07:08AM -0800 The OP in initial post said: "Is it possible to solve the problem without using other libraries?" The answer is yes So answer that use strcmp or fgets or other different from scand() has to be not full voted... |
Daniel <danielaparker@gmail.com>: Nov 16 07:02AM -0800 > One presumes class data members would be private, else what's the point of having a class? > Given that, putting the data members first would be contrary to the generally accepted public, protected, private ordering with a class. There's at least one practical c++ specific reason for putting the data members first, it makes it easier to line up the constructor initializer lists with the order in which data members are declared, which is a good thing to do in C++. Public, protected, private ordering is just a convention, it's usefulness to the developer is vanishing with modern practices using templates, inline functions etc., try quickly groking any of the standard library or boost classes by glancing at their headers. At the same time, tooling has become quote good for introspecting classes within an editor environment, and for producing class documentation. > It is the same problem of "const"... The problem with const is that it doesn't mean pure, which it would have to mean were compilers able to take advantage of it, or programmers to reason about it. Daniel |
agent@drrob1.com: Nov 16 07:59AM -0500 Hi. I am learning C++ after many years of using modula-2 on windows. I now am working my way thru C++ all in one for dummies, by John Paul Mueller and Jeff Cogswell. On the chapter about getting the contents of a directory, they describe io.h and _findfirst(), _findnext(), and struct _finddata_t. This is not working under ubuntu 14.04. I located a version of io.h in the /usr/src/linux-headers tree, copied it to my working directory, but I'm still getting a lot of undeclared symbol errors. How do I read the contents of a directory in Ubuntu 14.04? Thanks, Rob |
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Nov 16 02:17PM +0100 > in the /usr/src/linux-headers tree, copied it to my working directory, > but I'm still getting a lot of undeclared symbol errors. > How do I read the contents of a directory in Ubuntu 14.04? Sounds like an ungood book, because those `_xxx` identifiers are internal to the implementation, i.e. they're compiler and platform specific. For portable code, use the Boost file system library. It will probably be part of C++17. Cheers & hth., - Alf |
scott@slp53.sl.home (Scott Lurndal): Nov 16 02:44PM >in the /usr/src/linux-headers tree, copied it to my working directory, >but I'm still getting a lot of undeclared symbol errors. >How do I read the contents of a directory in Ubuntu 14.04? man readdir |
Paavo Helde <myfirstname@osa.pri.ee>: Nov 15 05:44PM -0600 jacobnavia <jacob@jacob.remcomp.fr> wrote in > Remember, this is C++, not C. Everything must be a lot more complex > than it needs to! >:-) In C++ one can choose the exact level of complexity one wants. Literal magic constants are valid C++ and have lots of drawbacks. The #define constants are valid C++ and have lots of drawbacks. The enum constants are valid C++ as well and have some drawbacks. Enum classes are valid C++ 11 and have other kind of drawbacks (the complexity). In C++ one can choose the needed level of safety-maintainability-simplicity as desired all over the full scale, that's its bless and curse. Short one-off hacks could best do with direct numeric literals, whereas in large long-living projects robustness, safety, ease-of-maintenance issues trump all other concerns like the effort of writing some more lines for each enum. The thing is, one-off hacks can be written in any language, most of which are simpler than C++, while for large long-living projects C++ is one of the few truly viable options. Cheers Paavo |
"Öö Tiib" <ootiib@hot.ee>: Nov 15 04:30PM -0800 On Monday, 16 November 2015 00:19:37 UTC+2, jacobnavia wrote: > I can now say > if (val &(Sent|Forwarded)) > to test for some bit. Too cryptic. I like it better to read as 'if (val[Sent] or val[Forwarded])'. Code: #include <bitset> #include <iostream> int main() { // Jacob's enum with magic numbers removed enum DocState { Archived, Pending, Received, Sent, Forwarded, Count_DocStates }; // typedef the combined bitfield type DocStates typedef std::bitset<Count_DocStates> DocStates; // we have our bifield "val" DocStates val; // we can put some flags into it val.set(Archived).set(Pending); // Irrelevant crap that might be useful sometimes with such values std::cout << "bitfield of val: " << val << '\r' << "number of bits: " << val.count() << '\r' << "val reversed: " << ~val << '\r'; // what Jacob wanted was // if (val &(Sent|Forwarded)) // I consider it about as good to read if (val[Sent] or val[Forwarded]) { std::cout << "It was sent or forwarded \r"; } else { std::cout << "It wasn't sent or forwarded \r"; } } Output: bitfield of val: 00011 number of bits: 2 val reversed: 11100 It wasn't sent or forwarded |
David Brown <david.brown@hesbynett.no>: Nov 16 01:51AM +0100 On 15/11/15 23:43, jacobnavia wrote: > I am enumerating the different states that a message can have: archived, > sent, whatever. > I do not see why you suppose that it is not an enumeration! No, you are defining names for a set of bits - that is not the same as an enumeration. However, an "enum" is often the clearest and neatest way to achieve this in C, and I think it is not unreasonable to do it that way. You are right that a C++ enum class can't do that, because you can't convert the enumeration constants to integers. Fair enough, a C++ enum class won't cover that usage. But for more normal enumerations, an enum class is a stronger and better scoped alternative to using the old, weak enums. So in C++, use enum classes where possible, and old enums otherwise. (Or use a selection of constexpr const's inside a class or struct.) |
Ian Collins <ian-news@hotmail.com>: Nov 16 03:05PM +1300 jacobnavia wrote: >> } > That's 13 lines of code to be done for each of those enums to do what > plain enum gives me for free. To be fair, the operators could be on one line and only one of them is required for your example... So that's only one extra line (not to mention your "13 lines" included an example!). Most enum types don't require logical operators and they don't need to be converted to int. > Great Progress! Indeed it is. Being able to specify the size of an enum type is something that has been missing for way too long so has giving enumerators their own scope. > Look this is C++, I am no expert. I will just go on using enums... Which is one of the beauties of C++: you have that choice. -- Ian Collins |
Martin Shobe <martin.shobe@yahoo.com>: Nov 15 10:17PM -0600 On 11/15/2015 6:51 PM, David Brown wrote: > enums. So in C++, use enum classes where possible, and old enums > otherwise. (Or use a selection of constexpr const's inside a class or > struct.) Is everyone forgetting that you can overload the & operator (and friends)? Martin Shobe |
David Brown <david.brown@hesbynett.no>: Nov 16 08:38AM +0100 On 16/11/15 05:17, Martin Shobe wrote: >> otherwise. (Or use a selection of constexpr const's inside a class or >> struct.) > Is everyone forgetting that you can overload the & operator (and friends)? It's a balance between convenience and type safety. Yes, you /can/ make an enum class and overload & and |, or perhaps make a conversion operator to int. But that's quite a bit of extra coding compared to simply using an old-fashioned enum. Is it really worth that extra effort? With C++, you can choose either way - whatever makes most sense for /your/ code. |
Christian Gollwitzer <auriocus@gmx.de>: Nov 16 10:54AM +0100 Am 15.11.15 um 23:37 schrieb Ian Collins: > class was to prevent inadvertent conversion to int. Given bit fields > are unsigned types, enum class allows you to specify the underlying > type. This is still misusing enums for bitfields. There is a syntax for bitfields in C++ struct foo { bool sent : 1; bool read : 1; }; and then you can test for multiple conditions without the bitoperator hack: foo x; if (x.sent && x.read) { do_something(); } I would expect that the compiler can optimize this to a bitoperation. Christian |
Martin Shobe <martin.shobe@yahoo.com>: Nov 16 08:26AM -0600 On 11/16/2015 1:38 AM, David Brown wrote: > simply using an old-fashioned enum. Is it really worth that extra > effort? With C++, you can choose either way - whatever makes most sense > for /your/ code. As I tend to prefer the type safety, I do consider adding the couple of lines of code needed worth the effort. I understand that others might not consider it worth the effort and I'm find with that. My response was intended to address the claim that, "a C++ enum class won't cover that usage." (I also had read the other threads that mentioned the possibility at that point.) Martin Shobe |
David Brown <david.brown@hesbynett.no>: Nov 16 03:33PM +0100 On 16/11/15 15:26, Martin Shobe wrote: > intended to address the claim that, "a C++ enum class won't cover that > usage." (I also had read the other threads that mentioned the > possibility at that point.) Fair enough. I should have written that a C++ enum class won't cover that usage without additional code. (It is also possible to static_cast the enum class element to an int or other underlying type directly in the code. But that's ugly.) |
scott@slp53.sl.home (Scott Lurndal): Nov 16 02:41PM >> and C++. >I think you missed the point. In both C and C++ a better alternative to >#defines for error codes exists and has existed for decades: a simple enum. Although a simple enum is a signed integer. I'd prefer unsigned values to be declared as unsigned and one can't do that with enum, unless one is using C++11 or higher. |
Juha Nieminen <nospam@thanks.invalid>: Nov 16 09:43AM > size_type > max_size() const _GLIBCXX_NOEXCEPT > { return _M_get_Node_allocator().max_size(); } Curiously, in libc++ (which is mainly used by clang) it's defined as: size_type max_size() const _NOEXCEPT {return numeric_limits<difference_type>::max();} which, I believe, is the correct implementation. --- news://freenews.netfront.net/ - complaints: news@netfront.net --- |
Ramine <ramine@1.1>: Nov 15 06:57PM -0800 Hello, I feel that i must explain to you how do work my inventions that are my SemaCondvar and SemaMonitor objects, you will find those classes inside the SemaCondvar.pas file inside the zip file, SemaCondvar and SemaMonitor are new and portable synchronization objects , SemaCondvar combines all the charateristics of a semaphore and a condition variable and SemaMonitor combines all charateristics of a semaphore and an eventcount , they only use an event object and a very fast and efficient and portable FIFO fair Lock , so they are fast and they are FIFO fair. When you set the first parameter of the constructor to true it will add the characteristic of a Semaphore to the condition variable or to the Eventcount, so the signal will not be lost if the threads are not waiting for the SemaCondvar or SemaMonitor objects, but when you set the first parameter of the constructor to false it will not behave like a Semaphore because if the threads are not waiting for the SemaCondvar or SemaMonitor the signal will be lost.. Now you can pass the SemaCondvar's or Semamonitor's initialcount and SemaCondvar's or SemaMonitor's MaximumCount to the construtor, it's like the windows Semaphore`s InitialCount and the Semaphore's MaximumCount. Like this: t:=TSemaMonitor.create(true,0,4); You have 5 options in the defines.inc file for setting the kind of locks, just look inside defines.inc , if you want to set it for the Mutex that is energy efficient because it blocks the threads, uncomment the option Mutex, if you want to set it for my node based scalable Lock, uncomment the option MLock, if you want to set it for my scalable array based lock called AMLock just uncomment the option AMLock inside defines.inc, if you want to set it for Ticket Spinlock just uncomment the option TicketSpinlock ,If you want to set it for Spinlock just uncomment the option Spinlock. That's all. You can download my SemaMonitor and SemaCondvar from: https://sites.google.com/site/aminer68/light-weight-semacondvar-semamonitor and from: https://sites.google.com/site/aminer68/semacondvar-semamonitor Please feel free to port it to C++... Thank you, Amine Moulay Ramdane. |
bleachbot <bleachbot@httrack.com>: Nov 16 12:56AM +0100 |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment