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 6968401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:26:49+00:00 2026-05-27T16:26:49+00:00

it’s a basic program for array of pointer to objects. #include <iostream> using namespace

  • 0

it’s a basic program for array of pointer to objects.

#include <iostream>
using namespace std;

class city
{
protected:
    char *name;
    int len;
public:
    city()
    {
        len=0;
        name= new char[len+1];
    }
    void getname(void)
    {
        char *s;
        s= new char[30];
        cout<< "enter city name";
        cin >> s;
        len= strlen(s);
        name = new char[len+1];
        strcpy(name, s);
    }
    void printname(void)
    {
        cout<< name <<"\n";
    }
};

compiler says problem is in the “cout<< name <<“\n”;”

int main()
{
    city *cptr[10];

    int n=1;
    int option;

    do
    {
        cptr[n]= new city;
        cptr[n]->getname();
        n++;
        cout<< "continue? yes=1, no=0. select now?";
        cin>> option;
    }
    while(option);

    cout<< endl<< endl;
    for (int i=1;i<=n;i++)
    {
        cptr[i]-> printname();
    }

    cin.ignore();
    getchar();
    return 0;
};

There’s also a warning(this warning is not an issue)

warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:\program files\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'

I tried strcpy_s to remove the warning, but the word is unrecognized.

  • 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-05-27T16:26:50+00:00Added an answer on May 27, 2026 at 4:26 pm

    cptr is an array of character pointers. And the size of the array is fixed to 10:

    city *cptr[10];
    

    This makes 0 to 9 as the valid index into the array. But your do-while loop does not perform this check. If the user keeps continuing by inputting 1 you’ll go and write beyond the array.

    And array index in C++ start with 0 and not 1 so

    for (int i=1;i<=n;i++)
    

    should be:

    for (int i=0;i<n;i++)
    

    And

    int n=1;
    

    should be

    int n=0;
    

    Also consider using strncpy in place of strcpy.

    Also you are leaking memory by not freeing the memory allocated to s. You need to free it by calling delete:

    char *s;
    s= new char[30];
    // use it
    delete[]s;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload

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.