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

  • SEARCH
  • Home
  • 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 8751443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:03:01+00:00 2026-06-13T13:03:01+00:00

I am teaching myself C/C++ at the moment, and I got the exercise (from

  • 0

I am teaching myself C/C++ at the moment, and I got the exercise (from the book I am reading) to write a program that could make an output like this:

Enter your first name: Flip
Enter your last name: Fleming
Here’s the information in a single string: Fleming, Flip

Using Structures. But my output comes out like this:

Enter your first name: Flip
Enter your last name: Fleming
Here’s the information in a single string: , 

Here is the code. It’s fairly short and simple so it shouldn’t be hard to read 🙂

#include <iostream>
#include <cstring>

using namespace std;

struct Person {
    char* firstName;
    char* lastName;
};

char* getName(void);

int main() {
    Person* ps = new Person;
    cout << "Enter your first name: ";
    char* name;
    name = getName();
    ps->firstName = name;
    cout << "Enter your last name: ";
    char* lastname;
    lastname = getName();
    ps->lastName = lastname;
    cout << "Here's the information in a single string: "
            << ps->lastName << ", " << ps->firstName;
    delete ps;
    delete name;
    delete lastname;

    return 0;
}

char* getName() {
    char temp[100];
    cin >> temp;
    cin.getline(temp, 100);
    char* pn = new char[strlen(temp) + 1];
    strcpy(pn, temp);

    return pn;
}
  • 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-13T13:03:02+00:00Added an answer on June 13, 2026 at 1:03 pm

    First, the immediate problem is that you read twice from std::cin: first with operator>>, and then with getline. Pick one or the other.

    But let’s simplify your code a bit. There are simply too many sources of error. Pointers are tricky because they might point to the wrong thing, or you might forget to delete objects, or delete them twice. C-style char arrays as strings are bad because, well, they’re not strings, and they don’t behave like strings.

    So let’s use the standard library’s string class:

    #include <iostream>
    #include <string>
    
    struct Person {
        std::string firstName;
        std::string lastName;
    };
    
    std::string getName(void);
    
    int main() {
        Person ps;
        cout << "Enter your first name: ";
        std::string name = getName();
        ps.firstName = name;
        cout << "Enter your last name: ";
        std::string lastname = getName();
        ps.lastName = lastname;
        cout << "Here's the information in a single string: "
                << ps.lastName << ", " << ps.firstName;
    }
    
    std::string getName() {
        std::string temp;
        std::getline(cin, temp);
        return temp;
    }
    

    This is a fairly simple, almost mechanical substitution, basically just replacing char* by std::string, and removing the bits that are no longer necessary.

    Of course, as pointed out in a comment, I’ve omitted all forms of error checking, which a real program should definitely do.

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

Sidebar

Related Questions

I'm teaching myself programming and today's challenge is to write a program that can
I am teaching myself Ruby on Rails. I would like to make website that
Im teaching myself java and the book i'm looking at just got around to
I'm teaching myself Python 3.2 and I'm trying to make a program to match
I'm teaching myself CSS and HTML, and I've come across something that seems like
I'm teaching myself Objective-C from a book ( Cocoa programming for mac OS X
I'm trying to teaching myself how to program by building programs/scrips that will be
Teaching myself the flow of data from mySQL -> PHP -> JSON / Javascript
I am familiar with Java and at the moment I am teaching myself PHP.
I've been teaching myself C++ and someone told me that C++ does not have

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.