Help!!- create output from input file name

please help! having trouble getting the final result, it is not subtracting the input suffixes

A common string processing operation is constructing an output file name from an input file name. Write a program outName.cpp that prompts and reads an “input” file name and constructs an “output” file name from the input file name by replacing the suffixes “.in” or “.input” or “.data” with the suffix “.output”. Your program will not actually open any files for reading or writing. It is only creating a file name.

#include <iostream>
#include <cmath>
#include <fstream>
#include <string>

using namespace std;

// FUNCTION PROTOTYPE FOR construct_output
void construct_output(string & in_name, string & out_name)
// Two parameters: in_name and out_name (input and output file names respectively
// Parameters are passed by reference
{
int length(0); // Length of string
length=in_name.length(); // Sets length equal to characters in input file name
out_name=in_name; // Sets characters of both input and output file names equal


if(in_name.substr(length,3)==".in")
{
out_name.substr(length,3);
out_name.replace(length,3,".output");
}
else if(in_name.substr(length,6)==".input")
{
out_name.substr(length,6);
out_name.replace(length,6,".output");
}
else if(in_name.substr(length,5)==".data")
{
out_name.substr(length,5);
out_name.replace(length,5,".output");
}
else
{
out_name.insert(length,".output");
}

return;
}



int main()
{
string input_filename, output_filename; //Declares input and output file names

// Read in the input file name
cout<<"Enter input file name: ";
cin>>input_filename;

construct_output(input_filename, output_filename);
cout<<"Output file name: "<<output_filename<<endl;

return 0;
}




please help! having trouble getting the final result, it is not subtracting the input suffixes
Last edited on
Topic archived. No new replies allowed.