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!
@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.
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:
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.
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.