// File prog1_3.cpp - corrected to be conformant (uses string::npos
// rather than -1) 
// the program prompts the user for the pathname of a
// file.  it uses string class operations to identify and output the
// pathname and filename. if the filename has the extension "cpp",
// create and output the name of an executable file whose extension
// "exe" replaces the extension "cpp"

#include <iostream>
#include <string>

using namespace std;

int main()
{
   string pathname, path, filename, executableFile;
   int backslashIndex;     // index of '\' 
   unsigned int dotIndex;  // and '.' - changed type from original

   cout << "Enter the path name: ";
   cin >> pathname;


   // identify index of last '\'. note: because
   // escape codes such as '\n' begin with \,
   // C++ represents \ by '\\'
   backslashIndex = pathname.find_last_of('\\');

   // pathname is characters prior to the last '\'
   path = pathname.substr(0,backslashIndex);

   cout << "Path:       " << path << endl;

   // tail of pathname is the filename
   // end of string represented with string::npos (-1 in original)
   filename = pathname.substr(backslashIndex+1, string::npos);
   cout << "Filename:   " << filename << endl;

   // see if the filename has the extension ".cpp".
   // first find the index of the last '.'. if there
   // is no '.', dotIndex is string::npos (not -1 in original)
   dotIndex = filename.find_last_of('.');

   // test if there is a '.' and the remaining characters are "cpp"
   if (dotIndex != string::npos && filename.substr(dotIndex+1) == "cpp")
   {
      // setup string executable by erasing "cpp" and appending "exe"
      executableFile = filename;
      executableFile.erase(dotIndex+1,3);
      executableFile += "exe";
      cout << "Executable: " << executableFile << endl; 
   }

   return 0;
}
