Functions that return char[]

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;
}


Don't post your topic in multiple forums
can't you just do something like this?

string newvariable;
string name;

cin >> name;
case 1: newvariable = name +".BAS";
cout << newvariable;
break;

I might be missing something because I'm not sure what you are trying to do in the last half.




Last edited on
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.
You know string has a function .c_str() which turns it into a char array. You could use that and save time.
used it and loved it. thanks
seems it's been answered, but for future reference, it is much appreciated when code is posted within code tags. regards.

ps i'm not being condescending or anything, it just makes it easier for people to help you :)
Last edited on
Topic archived. No new replies allowed.