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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:49:22+00:00 2026-06-15T02:49:22+00:00

Why is the call to c2.view() printing out both the ID and name for

  • 0

Why is the call to c2.view() printing out both the ID and name for the customer ID?

I have stared at this for a while and couldn’t find a cause. I have either missed something really obvious or I don’t understand how cstrings work 🙂

Customer.h

#ifndef CUSTOMER_H
#define CUSTOMER_H
 class Customer
 {
 private:
     char accountID[6];
     char name[30];
 public:
     Customer();
     Customer(char[], char[]);
     void view();
     Customer operator=(const Customer&);

 };
#endif

Customer.cpp

#include <string>
#include <iostream>
#include "Customer.h"
using namespace std;



Customer::Customer()
{
    strcpy(accountID, "");
    strcpy(name, "");
}

Customer::Customer(char acc[], char n[])
{
    strcpy(accountID, acc);
    strcpy(name, n);
}

void Customer::view()
{
    cout << "Customer name: " << name << endl;
    cout << "Customer ID: " << accountID <<endl;
}

Customer Customer::operator=(const Customer& right)
{
    strcpy(accountID, right.accountID);
    strcpy(name,  right.name);
    return* this;
}

Driver.cpp

#include <iostream>
#include "Customer.h"
using namespace std;

int main()
{
    char id[] = "123456";
    char n[] = "Bob";
    Customer c1;
    Customer c2(id, n);
    c1.view();
    c2.view();
    system("pause");
    return 0;
}

Output:

Customer name:
Customer ID:
Customer name: Bob
Customer ID: 123456Bob
Press any key to continue . . .
  • 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-15T02:49:24+00:00Added an answer on June 15, 2026 at 2:49 am

    You are passing a string with seven characters:

    char id[] = "123456"; // one more character for null termination '\0'
    

    but your array is of size 6. So when you print accountId, you go beyond the '6' character and print out whatever is next to it, which in this case happens to be the contents of name.

    Save yourself a lot of trouble by using std::strings instead of character arrays.

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

Sidebar

Related Questions

I have two partial view and I want to call one of them in
I have a view that I'm creating in the loadview like this: - (void)loadView
I'm trying to return a JSON response from a Django view call via an
I implemented 4 views in AppDelegate depends on requirement it will call the view.I
I was wondering when to call from the view a Custom View Helper like
I am developing an application that currently has a View Controller (call it ViewControllerX).
hello how can i call in the current view, a method implemented in the
How to call codeigniter controller function from view? When i call the function in
In a view, let's call it firstView I created a secondView as follows and
Is it possible to view the complete list of call stack for a given

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.