Need Opinion of C++ exercise

Hi, so i'm doing a C++ exercise from my text book and i'm a little confused of what it is asking me to do, curious if anyone can help me.

the exercise is as follows:


When comparing strings, the comparison is not always satisfactory. For example, "file10" < "file2", even though we would prefer it to come afterwards. Produce a num_less function that, when comparing two strings that are identical except for a positive integer at the end, compares the integers. For example, num_less("file12", "file2") should return true, but num_less("file12", "file11") and num_less("file2", "doc12") should return false. Use a helper function that returns the starting position of the number, or –1 if there is none. (Horstmann, 2017-08-12, p. EX5-20)




What i am confused about is the examples they give;

example 1: num_less("file12", "file2") is true
example 2: num_less("file12", "file11") is false
example 3: num_less("file2", "doc12") is false



they mention that "file10" < "file2" and would like "file10" to come after "file2" so if i'm reading this art correctly,isn't this saying we go in order of the last number listed at the end of the file?

but if that's the case then how is example 1 true?

so then i though, ok, maybe the order of the numbers goes from just the last digit, but then the wanting of "file10" coming after "file2" wouldn't make sense since 0 is less then 2.

am i missing something? i'm sure this is a simple exercise and i its just going over my head in what they actually want? how would you take this problem, any thoughts would be appreciated!

Last edited on
Based on the problem description, the example is clearly wrong.

Should be

example 1: num_less("file2", "file12") is true

or

example 1: num_less("file12", "file2") is false

So I don't think you're missing anything!?

Andy

PS The function name "num_less" is poor; it doesn't say what it does.
Last edited on
Looks like the book has errors. Assume that they want the intuitive 3 < 12 < 21.
Last edited on
If it's Brief C++: Late Objects, 3rd Edition you're using, you could report the bug here:

Big C++/Brief C++ 3rd Edition
Bug Report Page
https://horstmann.com/bigcpp/bugs.html

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

int main()
{
    std::string str1{"file2"};
    std::string str2{"file12"};
    
    if(str1 <= str2)
        std::cout << str1 << " is less than " << str2 << '\n';
    else
        std::cout << str1 << " is greater than " << str2 << '\n';
    
    if( str1.compare(str2) <= 0)
           std::cout << str1 << " is less than " << str2 << '\n';
       else
           std::cout << str1 << " is greater than " << str2 << '\n';
    
    return 0;
}


There is no error. The comparison is character by character, position by position.

2 and 12 are sub-strings and are not read as numbers.
Last edited on
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
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
#include <vector>
#include <utility>
using namespace std;

//------------------------------------------------

void splitString( const string &input, string &str, int &num )
{
   int p = input.find_first_of( "0123456789" );
   if ( p == string::npos )            // no number found; use -1
   {
      str = input;
      num = -1;
   }
   else
   {
      str = input.substr( 0, p );
      num = stoi( input.substr( p ) );
   }
}

//------------------------------------------------

bool num_less( const string &a, const string &b )
{
   string astr, bstr;
   int anum, bnum;

   splitString( a, astr, anum );
   splitString( b, bstr, bnum );

   if ( astr != bstr ) return astr < bstr;
   return anum < bnum;
}

//------------------------------------------------

int main()
{
   vector< pair<string,string> > tests{ { "file12", "file2" }, { "file12", "file20" }, { "file12", "file11" }, { "file2", "doc12" } };
   for ( auto pr : tests )
   {
      cout << "num_less(" << pr.first << "," << pr.second << ") is " << boolalpha << num_less( pr.first, pr.second ) << '\n';
   }
}


num_less(file12,file2) is false
num_less(file12,file20) is true
num_less(file12,file11) is false
num_less(file2,doc12) is false
againtry wrote:
2 and 12 are sub-strings and are not read as numbers.
Read the problem description again. The whole point of the exercise is to compare trailing digits as number.
@dhayden
Bullshit. My program describes the problem exactly. You’ve made the mistake of jumping to the conclusion that it’s a solution, which isn’t being asked for anyway. Maybe MYOFB in future.
This can also serve you well if you are tempted to do such a thing in your own code.
use leading zeros when cooking up bulk file names in this manner.
eg
file000001
file000002
file000003
file000011 //no longer comes before file #3
file000103 //no longer comes before file #3

those will sort correctly when the operating system presents them, without any extra effort. And that is what most people want; they don't want to run a program to list them in order, they want to use dir or ls or a window to see them in order without any foolishness.

its a good exercise: the program you are writing could be used to properly rename the files so they appear in order, and to do that, you need the pieces of what you are building (split it into name and numbers). Its probably doable in pure string / text without splitting it, but I don't know that its any 'better' that way.
Last edited on
againtry wrote:
Bullshit My program describes the problem exactly.
The problem is that the examples of num_less() don't match the description of what it should do. Your program masterfully shows the way strings are normally compared, which doesn't describe the problem at all.

againtry wrote:
There is no error. The comparison is character by character, position by position.
No kidding. Except that the question isn't about normal comparisons.

againtry wrote:
2 and 12 are sub-strings and are not read as numbers.
That's the whole frickin point of the exercise! He's supposed to write a comparison function that DOES compare them as numbers. So like I said before:
Read the problem description again.
againtry wrote:
Maybe MYOFB in future.
Your misleading post on a help forum is the business of all of us who actually try to help people instead of just insulting them.
use leading zeros when cooking up bulk file names in this manner.
eg
file000001
file000002
file000003
file000011 //no longer comes before file #3
file000103 //no longer comes before file #3

This is going off topic, but sometimes I prefix file or directory names with a number to get them in the order I like:

01 January
02 February
...
12 December

or

01 Sunday
02 Monday
03 Tuesday
...

Your misleading post on a help forum is the business of all of us who actually try to help people instead of just insulting them.


Do you seriously think you have some sort of authority over anybody here? I’ve come across some self-appointed prize wankers over time but you take the cake. Get your hand off it grampa.
Do you seriously think you have some sort of authority over anybody here?
Nope. I never claimed to either.
I’ve come across some self-appointed prize wankers over time but you take the cake.
https://en.wikipedia.org/wiki/Ad_hominem
Your petty ego is getting in the way of your miniscule anger management grandpa. You should have let it go at the start and accept the good faith of other commenters without chiming in with your irrelevant and overblown pompous commands to your superiors.
Your solipsism and defective psyche knows no bounds.
Topic archived. No new replies allowed.