Hi! I recently had to write a program in which a user would input a file name, and the program would spit out a version of the file name. Specifically, if the user enter something like "file.input", "file", "file.in" or "file.data", the program is supposed to print out "file.output". Here is what I have thus far:
The break; will always happen so the loop will only run once. The else part will always run if s.substr(i, 5) == ".data" is false. You probably want to use elseif to make it work properly.
I did that, but it seems to be stuck in the loop. I'll input the file name, but it won't return a name. That's why I put the break; at the end of the loop.
Are you sure you should not handle all possible file extensions? If I were to do it for all file extensions I would replace the loop with something like s = s.substr(0, s.find('.')) + ".output";
I was thinking about something like that, where I would use the find function to find the period character. What if the input has no extension, though? Adding the + ".output" would take care of that, no?
Okay, that seemed to work. Thank you! Just out of curiosity, though: Would there be a way to end the loop after checking through the string once, with out using break; ?
If you only want to do it once, you don't need a loop :) Also, if the files ends in ".input", it looks like the code will execute the first if condition "if s == '.in'", and you'll wind up with .outputput. A better strategy here is to find the actual extension in its entirety. Start at the end of the string, and look backwards for a "." character; you can iterate like you've been doing, or use the stl string functions. Then use that location to the end of the string - that should be your extension. Last, I believe your string comparison isn't actual string comparison, it's pointer comparison. You need to use the strcmp (or stricmp) functions.