-5

I need to make a function (no argument) that will return a char array. The length of the array will be defined by the user. I can not use the gets() function.

char get()
{
    int size;

    char *str=new char[size];
    int i,l;

    cout<<"enter size";
    cin>>size;

    for(i=0;i<size;i++)
    {
        str[i]=getche();

    }

    str[size]='\0';

    return str[size];
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 3
    You need to read a textbook ASAP. – Slava Aug 02 '17 at 18:47
  • 1
    `int size; char *str=new char[size];` what is the value of `size` here? – NathanOliver Aug 02 '17 at 18:48
  • Return a `std::string` or a `std::vector`. *Don't* return a pointer to a C-style array of `char`. *Please*! – Jesper Juhl Aug 02 '17 at 18:49
  • 1
    @Slava - I agree - one of these: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Jesper Juhl Aug 02 '17 at 18:51
  • 1
    Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Aug 02 '17 at 19:04
  • @Jesper: good edits. I suggest that additionally if you see chatty material ("I am new here") or any kind of urgent begging, it can be trimmed. – halfer Aug 02 '17 at 19:10
  • @NathanOliver the size value is give by the user as to "Request memory for the variable". telling how many of them need be for the array. – Saad Shaukatt Aug 02 '17 at 19:19
  • @SaadShaukatt Not at that point You're using `size` before you ever fill it in from the user. – NathanOliver Aug 02 '17 at 19:22
  • @NathanOliver yea and.....that is my bad should use before hand . still need a proper return construct for the function which i am confused for . – Saad Shaukatt Aug 02 '17 at 19:32

1 Answers1

1

Following code may be useful:

char* get()
{
    int size;

    cout<<"enter size";
    cin>>size;

    char *str=new char[size + 1]; // +1 for `\0` character

    for(int i=0;i<size;i++)
    {
        str[i]=getche();
    }

    str[size]='\0';

    return str;
}

To get string size in calling function(for e.g. in main()), use strlen() function which is available in cstring header file.

cse
  • 4,066
  • 2
  • 20
  • 37
  • thank you , but i need to carry the rerun to another function to be used. as such for this code int length(char* str) { char i; for(i = 0; str[i] != '\0'; ++i); // printf("Length of string: %d", i); return i; } – Saad Shaukatt Aug 02 '17 at 20:23
  • Since a function can return only 1 value. So either you can return the string or length of string. I preferred to return string because you can calculate length in calling function (as stated above). But if you wants to return the length then you can change the function signature to `int get(char** str)` and change use of `str` variable accordingly. If you don't wants the content of `str` variable then present code is ok except return the `size-1` from above code. – cse Aug 03 '17 at 06:48