I have written this code as part of a project. if my variable Name has the value "Amazon", how can i change this function so that the output is say Amazon.BAS if x=1.?
char* read(int x)
{
string s3;
switch (x)
{
case 1: s3=".BAS";break;
case 2: s3=".PAR";break;
case 3: s3=".DAT";break;
case 4: s3=".OBS";break;
case 5: s3=".RCH";break;
default:cout<<"wrong argument in read(arg)\n";
};
//Make string s1 contain value held by variable Name;
string s1;
stringstream out;
out << this->Name;
s1 = out.str();
string aFileS =s1+s3;
//convert filename string to Char
char aFile[100];
memset( aFile, '\0', 100 );//sets all values in char-array Radfile to "\0"
aFileS.copy( aFile, aFileS.size() );
cout<<aFile<<endl;
return aFile;
}
The function is supposed to return a char[] aFile which is actually a filename that is used in another function. s3 contains different file extensions and the extension called depends on the argument is read(arg). This way i dont have to re-write the read function over and over for the different objects but i just change the argument.
the last half converts string back to char[] since fstream myfile(afile,ios::in) only accepts char[] for afile. and char[] + char[] was not possible.