0

I have a problem with the following code:

#include <iostream>
#include<cstring>
#include <stdio.h>
#include <string.h>
using namespace std;
char* email(char* nam, char* ln,char* dep,char* comp)
{
  char str[260];
  strcpy (str,nam);
  strcat (str,ln);
  strcat (str,"@");
  strcat (str,dep);
  strcat (str,".");
  strcat (str,comp);
  strcat (str,".com");
  return str;
}
int main()
{
    cout << "Hello world!" << endl;
    char nam[50], ln[80], comp[100], dep[30];
char* e_mail = 0;
cout << "Name: ";
cin >> nam;
cout << "Last name: ";
cin >> ln;
cout << "company: ";
cin >> comp;
cout << "Which department: ";
cin >> dep;
e_mail = email(nam,ln,dep,comp);
cout << "email: " << e_mail << "\n has length: " << strlen(e_mail) << "\n";
    return 0;
}

All of these lines above should make an email adress, meanwhile it prints this: image I tried to do this with cout at the end of email function but it spoils the end result and doesn't print it correctly.

8
  • 4
    Why aren't you using std::string in C++? Commented Apr 24, 2020 at 22:38
  • 1
    You're returning a pointer to a local variable. str only exists within the function and is destroyed when the function returns. Therefore the pointer to str which the function is returned becomes invalid. Commented Apr 24, 2020 at 22:39
  • This is homework for university classes and I was given everything but the function i wrote myself Commented Apr 24, 2020 at 22:45
  • It is possible to dynamically allocate memory in the called function and return a pointer to it but that's a bad idea because the caller now has to remember to free the memory. Commented Apr 24, 2020 at 22:50
  • The only part I wrote is: char* email(char* nam, char* ln,char* dep,char* comp) down to int main() and I really can't change the rest of the code otherwise they won't pass this. Commented Apr 24, 2020 at 22:54

3 Answers 3

2
char *str[260];

Is an array of 260 pointers to char, surely you want a 260 chars array:

char str[260];

Furthermore you are returning a local variable that will go out of scope as the function returns leaving a dangling pointer e_mail in main.

An option would be to pass e_mail as an argument of the function:

void email(char *nam, char *ln, char *dep, char *comp, char *str) //str will point to e_mail
{
    strcpy(str, nam);
    strcat(str, ln);
    strcat(str, "@");
    strcat(str, dep);
    strcat(str, ".");
    strcat(str, comp);
    strcat(str, ".com");
}

int main()
{
    char e_mail[260];
    //...
    email(nam, ln, dep, comp, e_mail);
    cout << "email: " << e_mail << "\n has length: " << strlen(e_mail) << "\n";
    return 0;
}

Of course, being C++, you can use std::string instead of C style char arrays.

std::string email(const char *nam, const char *ln, const char *dep, const char *comp)
{
    std::string str;
    str.append(nam).append(ln).append("@").append(dep)
       .append(".").append(comp).append(".com");
    return str;
}

int main()
{
    std::string e_mail;
    //...
    e_mail = email(nam, ln, dep, comp);
    cout << "email: " << e_mail << " has length: " << e_mail.size() << "\n";
    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's not the only problem. main will have a dangling pointer even if you make that change.
I forgot to delete * when I was trying to solve it myself to be honest
1

The problem is that you are returning a pointer to an object that does not live when the function returns.

You can solve the problem using various methods. The simplest one is to return a std::string from the function.

std::string email(char* nam, char* ln,char* dep,char* comp)
{
  char str[260];

  ...

  return std::string(str); // Just to be explicit.
}

and change main to:

int main()
{
    cout << "Hello world!" << endl;
    char nam[50], ln[80], comp[100], dep[30];

    ...

   std::string e_mail = email(nam,ln,dep,comp);
   cout << "email: " << e_mail << "\n has length: " << e_mail.size() << "\n";
   return 0;
}

Comments

1

Ok I figured it out with (https://stackoverflow.com/users/9254539/bessiethecow) help Now it should be just like this:

#include <iostream>
#include<cstring>
#include <stdio.h>
#include <string.h>
using namespace std;
char* email(char* nam, char* ln,char* dep,char* comp)
{
  char*str =new char [260];
  strcpy (str,nam);
  strcat (str,ln);
  strcat (str,"@");
  strcat (str,dep);
  strcat (str,".");
  strcat (str,comp);
  strcat (str,".com");
  return str;
}
int main()
{
    cout << "Hello world!" << endl;
    char nam[50], ln[80], comp[100], dep[30];
char* e_mail = 0;
cout << "Name: ";
cin >> nam;
cout << "Last name: ";
cin >> ln;
cout << "company: ";
cin >> comp;
cout << "Which department: ";
cin >> dep;
e_mail = email(nam,ln,dep,comp);
cout << "email: " << e_mail << "\n has length: " << strlen(e_mail) << "\n";
    return 0;
}

1 Comment

This is certainly a valid solution, my advice, as you are using C++, try to take advantage of it, using C-style code with C++ is like using a Ferrari to go grocery shoping.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.