Hi, I have to define an interface. The API in my homework is stated below:
int generate_codes(char * ssn, char * student_id);
int denotes 0 or 1 for pass or fail. studentid is an output param should return a 6 digit id. ssn is a 9 digit input param
they school program will take ssn's and use my code to generate the student id.
- now from an API perspective should I not be using const char * for both parameters.
- should the studentid not be passed in by reference? rather than by pointer?
- can someone tell me how i can easily use the pointer in my test app which uses my api to get the pointer such that it prints a std::string from a char *?
my app code looks something like
const char * ssn = "987098765"
const char * studnt_id = new char [7];
int value = -1;
value = generate_codes(ssn,studnt_id);
std::string test(studnt_id);
std::cout<<"student id= "<<test<<" Pass/fail= "<<value<<std::endl;
delete [] studnt_id;
return 0;
I basically got an error about << not being compatible with the right hand side of the operand. When i changed the code to
std::cout<<"student id= "<<test.c_str()<<" Pass/fail= "<<value<<std::endl;
then it worked but i get garbage for the value. not sure how to do get the value form the pointer. THe value inside the function prints just fine. but when i try to print it outside of the function it prints garbage. Inside the above function I do set the studndt_id like so
std::string str_studnt_id = studnt_id;
this is to take a string param to my real funciton that does the code generation. when I am ready with the code and want to return it i do the following:
studnt_id = str_studnt_id.c_str();
should that make the address of the str_studnt point to the address of studnt_id and thus any changes I make to the value that its pointing to it should reflect outside the function? This API is used using char * but my functions use std::string.