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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:43:22+00:00 2026-06-09T13:43:22+00:00

In the following program, I am reading a 6-length string dd and storing its

  • 0

In the following program, I am reading a 6-length string dd and storing its ascii code in an integer array ipc of size 3×2. The problem is that the values stored in ipc are wrong and they change themselves later when I reprint them. I am surprised how can there be such a clear problem with such a simple code. (I am using Code::Blocks 10.05 on Win7 x64)

#include<iostream>

using namespace std;

int main()
{ char dd[5];
  int ipc[2][1];
  cin.get(dd,6);
  for(int i=0;i<3;i++)
  { for(int j=0;j<2;j++)
    { ipc[i][j]=int(dd[j+2*i]);
      cout<<ipc[i][j]<<endl;
    }
  }
  cout<<"------"<<endl;
  for(int i=0;i<3;i++)
  { for(int j=0;j<2;j++)
    { cout<<ipc[i][j]<<endl; }
  }
}

If input given is 123456, the output is:

49
50
51
52
53
2
------
49
51
51
53
53
2

Any sort of help will be very much appreciated. Thank you.

  • 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-09T13:43:24+00:00Added an answer on June 9, 2026 at 1:43 pm

    The array declaration is incorrect and the code is going out-of-bounds on the array causing undefined behaviour. Declaration should be changed from:

    int ipc[2][1]; 
    

    to:

    int ipc[3][2];
    

    Additionally, cin.get() will read count - 1 characters, so:

    cin.get(dd, 6);
    

    will only read 5 characters, not 6. If the user enters 123456 only 12345 will be read. The cin.get() will also append a null character, (as commented by tinman). To correct increase the size of dd and the number of characters to be read:

    char buf[7];
    cin.get(buf, 7);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider following program: static void Main (string[] args) { int i; uint ui; i
I am reading a program which contains the following function, which is int f(int
When the following program is fead the following input (reading from cin): 1 1
The following program returns local and, according to the tutorial I m reading, it
When I'm reading the C++ standard, it seems that the following code is perfectly
I have a problem with reading a bunch of files using the following C#
I have a program that contains the following piece of code for constructing an
In the program I created the following logic for reading the data from the
Using the following code while reading in a .xls file, where s is the
The following program expects user input in the mixed fraction format 'whole_numbernumerator/denominator' and assigns

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.