Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8419041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:29:56+00:00 2026-06-10T02:29:56+00:00

Write a program that asks the user to enter his or her first name

  • 0

Write a program that asks the user to enter his or her first name and then last
name, and that then constructs, stores, and displays a third string, consisting of the
user’s last name followed by a comma, a space, and first name.Use char arrays and
functions from the cstring header file.A sample run could look like this:
Enter your first name: Flip
Enter your last name: Fleming
Here’s the information in a single string: Fleming, Flip

int main()
{

    char * fName,*lName,*fullName;

    fName = new char;
    cin.getline(fName,100);

    lName=new char;
    cin.getline(lName,100);

    fullName=new char[strlen(lName)+strlen(fName)+3];

    strncpy(fullName,lName,strlen(lName));

    fullName[strlen(lName)]=',';
    fullName[strlen(lName)+1]=' ';


    char* dummy=(char*)fullName[strlen(lName)+2];//making a pointer to the char after the ' ' char to start copying the first name

    strncpy(dummy,fName,strlen(fName));

    dummy[strlen(fName)+strlen(lName)+2]='\0';

    cout<<endl<<endl<<dummy<<endl;



    delete fullName;
    delete lName;
    delete fName;
    return 0;
}

why it crashes when i point to a char in the middle of the array and start copying ???

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:29:58+00:00Added an answer on June 10, 2026 at 2:29 am
    fName = new char;
    

    allocates a single char.

    cin.getline(fName,100);
    

    goes way beyond that.

    Use std::string instead. You’ll see that writing C++ code in C++ instead of C makes life a lot easier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a program that asks the user to enter a password
I want to write a program in C or C++ that asks to user
Hello I am trying to write a program in python that asks the user
So i had to write a program that asks for a user input (which
Here is my homework question: Write a program that asks for a film name
I'm writing a program that asks the user for their birthdate and then calculates
I'm writing a program that asks the user to enter their birth date. For
I am supposed to write a program that asks the user for a positive
I've got a problem. i have to write a program that asks the first
I have been trying to write some code that asks the user for several

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.