I am working on a simple program, and i am using Visual C++ Express Edition.
Everything is fine, i am processing the integers and i am copying them into an integer array. But the problem is when i m trying to access that integer array for further processing or to display them. It shows me weird numbers on the screen.
I have looked on to almost everywhere about it.
I did try initializing the array by

int bin_array[80]={};

In that case, all the values are wiped off the array and its simply 0 in every location.
Anyone can help me out here.

If you need help, you need to show us the RELEVANT code. Your problem sounds like you are trying to access invalid elements.

I believe that int bin_array[80]={}; doesn't actually initialize, it still only declares. Try int bin_array[80]={0}; instead.

Also, post back with the rest of your relevant code. Your problem is most likely elsewhere in your code. You're probably not handling the index value correctly, but we can't be sure unless you show us.

If you need help, you need to show us the RELEVANT code. Your problem sounds like you are trying to access invalid elements.

I believe that int bin_array[80]={}; doesn't actually initialize, it still only declares. Try int bin_array[80]={0}; instead.

Also, post back with the rest of your relevant code. Your problem is most likely elsewhere in your code. You're probably not handling the index value correctly, but we can't be sure unless you show us.

ahmm.... Alright. Here is the code.

#include"stdafx.h" 
#include<iostream.h>
#include<string>
#include<stdio.h>
#include<conio.h>
using namespace std;

int main()
{
	int remainder=0,count=0,j=0;
	string octal_store;
	int bin_store[80];
	
	cout << "Give Octal code: ";
	cin >> octal_store;
	for(int i=0;i<octal_store.size();i++)
	{
		octal_store[i]=(octal_store[i]-48);
		do
		{
			j=octal_store.size();
			remainder=octal_store[i]%2;
			octal_store[i]=octal_store[i]/2;
			bin_store[j--]=remainder;
			cout<<bin_store[j]; // Prints the Binary Number -- This is where it is screwing up

		}while(octal_store[i]>0);
		
		
	}
	return 0;
}

Since your program includes "stdafx.h" you are most likely using MS-VC++. It is easy to see what is going on if you take the time to use the debugger.

Every time your for loop iterates, you reset "j" to the length of the input string. Move this initialization statement to the line before your loop so that it only executes once.

Additionally, you are using the post-decrement operator on "j" when you assign "remainder" to "bin_store". This causes the value of j to be different in your assignment statement and your display statement. For example, if you enter "017" as your octal, you assign the first binary digit ("bit") to "bin_store[3]", but you display "bin_store[2]". You either need to change this to the pre-decrement operator, or perform the decrement after your display statement.

Ulitmately, however, the bigger problem is that a binary digit can't represent as much information as an octal digit. As a result, a binary number is going to have more digits than the same number in octal format. I think you should completely re-think how you set up and use "j" and "bin_store" to prevent array boundary issues, specifically, attempting to access negative array indexes. Your code will be much safer, and your bits will be in correct order, if you start "j" at (0), then increment it after the display statement.

I am very glad you replied to me.
Well, as far as i know, i do have my logic right here. If the array prints out well, I should be having the right Binary Number.
But not only in this program and also in others which i am working on right now. Whenever i try to input into integer array and later try to display the array. It freaks me out with so many numbers.

For Example if i entered in - "5".
I did have a output something like this "-858993460-858993460-858993460".
But the answer i am expecting is "101".
Now i think is there's some problem with the array storage.
What do you think?

No, you don't have your logic right. That's the problem. You seem to be converting from oct to bin correctly, but you are not storing and displaying the result correctly. Your problem is the storage and display. Until you correct those, you are going to continue to get invalid results. I suggest you have another look at my previous post.

-858993460 is a "junk" number that integers default to in VC++ when you declare them. The value keeps appearing because you are accessing uninitialized memory. To eliminate this junk data, you must initialize them to (0). Again, see my previous post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.