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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:30:05+00:00 2026-05-25T21:30:05+00:00

My question revolves around the program shown below (environment is Mac Xcode). #include <iostream>

  • 0

My question revolves around the program shown below (environment is Mac Xcode).

#include <iostream>
int main () {
    char nameOne [5];
    std::cin  >> nameOne; // input: BillyBobThorton
    std::cout << nameOne; // output: BillyBobThorton

    char nameTwo [5] = "BillyBobThorton"; // compile error, initializer string too long
    std::cout << nameTwo;
    return 0;
}

I have a char array of length 5, so I would expect the maximum amount of characters I could store in this array to be 4 (plus the null terminating char). And this is indeed the case when I attempt to store a string to the nameTwo variable. However, when I use an array of characters as the variable to store user input, the array length is outright ignored and the array seemingly expands to accomodate the extra characters.

Why is this the case, and is there perhaps a more appropriate way to store user input to an array of characters?

  • 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-25T21:30:06+00:00Added an answer on May 25, 2026 at 9:30 pm

    Is there perhaps a more appropriate way to store user input to an array of characters?

    Yes! The most appropriate way in C++ is to use std::string. This will prevent your user overrunning the end of the buffer you’ve allocated and corrupting your stack. If you only want to display a certain number of characters, you can limit on output (or during some validation routine) using std::string::substr(). Here’s an example.

    #include <iostream>
    #include <string>
    
    int main ()
    {
        std::string nameOne;
        std::cin >> nameOne; // input: BillyBobThorton
        std::cout << nameOne.substr(0, 5); // output: Billy
    
        const std::string nameTwo = "BillyBobThorton";
        std::cout << nameTwo.substr(0, 5); // output: Billy
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question revolves around CSS Fixed Layout vs a Float Layout that extends to
My question revolves around the use of curl_multi_exec in PHP. I am using code
I'm pondering the design of an application where the main feature revolves around the
Okay basically, I'm designing and developing a fairly complicated website which revolves around the
Databinding in WPF/Silverlight revolves around dependency properties, DataContext objects and DataSource objects. As far
(When I say STL, I'm talking about the template library that revolves around containers,
The IT department I work in as a programmer revolves around a 30+ year
Question as stated in the title.
Question is pretty self explanitory. I want to do a simple find and replace,
Question Alright, I'm confused by all the buzzwords and press release bingo going on.

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.