Run-Time Check Failure #3

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1605 Views - Last Post: 26 February 2012 - 01:53 PM Rate Topic: -----

#1 joannesablad   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 24-April 11

Run-Time Check Failure #3

Posted 24 February 2012 - 12:21 PM

I'm making a program, it converts Decimal to Binary in c++ stack! Here is my code. i'm getting a run time error. it says Run-Time Check Failure #3 - The variable 'n' is being used without being initialized. but i already initialize how do i fix now?


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

void Dec2Bin(void)
{
	Stack<char>  stack(10);
	int n;
	int rem;
	int quot;
	int base=2;
	while(n!=0)
	{
		rem = n%2;
		quot = n/2;
		stack.Push(rem);
		rem=n;
	}
	while(!stack.Is_Empty())
	{
		rem=stack.pop();
		cout<<rem;
	}
}
void main()
{
	Dec2Bin();
	system("pause");
}



Is This A Good Question/Topic? 0
  • +

Replies To: Run-Time Check Failure #3

#2 Karel-Lodewijk   User is offline

  • D.I.C Addict
  • member icon

Reputation: 455
  • View blog
  • Posts: 864
  • Joined: 17-March 11

Re: Run-Time Check Failure #3

Posted 24 February 2012 - 12:23 PM

	int n;
	int rem;
	int quot;
	int base=2;
	while(n!=0)



Where it the variable n initialized between it's declaration at the top and the moment it is first used at the bottom here ?

This post has been edited by Karel-Lodewijk: 24 February 2012 - 12:23 PM

Was This Post Helpful? 0
  • +
  • -

#3 joannesablad   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 24-April 11

Re: Run-Time Check Failure #3

Posted 24 February 2012 - 12:40 PM

View PostKarel-Lodewijk, on 24 February 2012 - 12:23 PM, said:

	int n;
	int rem;
	int quot;
	int base=2;
	while(n!=0)



Where it the variable n initialized between it's declaration at the top and the moment it is first used at the bottom here ?



Ok n is initialize now, why it is saying that Top_of_Stack is undeclared identifier???


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

void Dec2Bin(void)
{
	Stack<char>  stack(10);
	int n=0;
	int rem;
	int quot;
	int base=2;
	while(n!=0)
	{
		rem = n%2;
		quot = n/2;
		stack.Push(rem);
		rem=n;
	}
	while(!stack.Is_Empty())
	{
		cout<<Top_of_Stack;
		stack.pop();
	}
}
void main()
{
	Dec2Bin();
	system("pause");
}



Was This Post Helpful? 0
  • +
  • -

#4 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 794
  • View blog
  • Posts: 2,880
  • Joined: 31-December 10

Re: Run-Time Check Failure #3

Posted 24 February 2012 - 12:51 PM

Nowhere in the code you posted do you declare Top_of_Stack. Maybe if you posted stack.h, we might be able to help.
Was This Post Helpful? 0
  • +
  • -

#5 joannesablad   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 24-April 11

Re: Run-Time Check Failure #3

Posted 24 February 2012 - 12:56 PM

View Postvividexstance, on 24 February 2012 - 12:51 PM, said:

Nowhere in the code you posted do you declare Top_of_Stack. Maybe if you posted stack.h, we might be able to help.


Ok here is stack.h file

#include<iostream>
using namespace std;
template <class Element_Type>
class Stack
{
	private:
		/* This variable is used to indicate stack is      full or not*/
		unsigned int Full_Stack;	
		/* This variable is used to indicate top of the stack */
		int Top_of_Stack;	
		/* This pointer points to the array which behaves as stack, the space for this array is allocated dynamically */
		Element_Type *Stack_Array;
	public:

		Stack(unsigned int Max_Size)
		{
		Full_Stack = Max_Size;
		Top_of_Stack = -1;
		Stack_Array = new Element_Type[Max_Size];  
		}
		/* This Destructor frees the dynamically allocated space to the array */
	 ~Stack()
		{
		delete Stack_Array;
		}
	 bool Is_Full();
	 bool Is_Empty();
	 void Push(Element_Type x);
	 Element_Type pop();
	 Element_Type Top();
	 void Make_Empty();

};
template <class Element_Type>
bool Stack<Element_Type>::Is_Full()
	{	if (Top_of_Stack == Full_Stack-1)
			return true;
		else
			return false;
	}
/*This function Return TRUE if the stack is empty, FALSE  otherwise.*/
template <class Element_Type>
bool Stack<Element_Type>::Is_Empty()
	{	if(Top_of_Stack == -1)
			return true;
		else
			return false;
	}
template <class Element_Type>
void Stack<Element_Type>::Push(Element_Type x)
	{	if(Is_Full())
			cout<<"stack is full";
		else
			Stack_Array[++Top_of_Stack] = x;
	}
template <class Element_Type>
Element_Type	 Stack<Element_Type>::pop()
	{	if(Is_Empty())
			cout<<"stack is empty";
		else
			return Stack_Array[Top_of_Stack--];
			
	}
template <class Element_Type>
void Stack<Element_Type>::Make_Empty()
	{	Top_of_Stack = -1;
	}
template <class Element_Type>
Element_Type Stack<Element_Type>::Top()
	{
		if(Is_Empty())
			cout<<"stack is emepty";
		else
			return Stack_Array[Top_of_Stack];
	}


Was This Post Helpful? 0
  • +
  • -

#6 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 794
  • View blog
  • Posts: 2,880
  • Joined: 31-December 10

Re: Run-Time Check Failure #3

Posted 24 February 2012 - 01:05 PM

Well that's your problem. Top_of_Stack is a private member of class Stack, therefore you can't access it directly. You do have a member function Stack::Top() which returns Top_of_Stack so just call stack.Top() instead of trying to use Top_of_Stack.

This post has been edited by vividexstance: 24 February 2012 - 01:06 PM

Was This Post Helpful? 1
  • +
  • -

#7 joannesablad   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 24-April 11

Re: Run-Time Check Failure #3

Posted 25 February 2012 - 12:43 AM

View Postvividexstance, on 24 February 2012 - 01:05 PM, said:

Well that's your problem. Top_of_Stack is a private member of class Stack, therefore you can't access it directly. You do have a member function Stack::Top() which returns Top_of_Stack so just call stack.Top() instead of trying to use Top_of_Stack.


Ok Thanks, There is no error but why it is not converting Decimal to binary??
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,672
  • Joined: 25-December 09

Re: Run-Time Check Failure #3

Posted 25 February 2012 - 08:11 AM

In the following snippet:
	while(n!=0)
	{
		rem = n%2;
		quot = n/2;
		stack.Push(rem);
		rem=n;
	}


When will this loop ever end?

Jim

This post has been edited by jimblumberg: 25 February 2012 - 10:28 AM

Was This Post Helpful? 0
  • +
  • -

#9 joannesablad   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 24-April 11

Re: Run-Time Check Failure #3

Posted 26 February 2012 - 07:39 AM

View Postjimblumberg, on 25 February 2012 - 08:11 AM, said:

In the following snippet:
	while(n!=0)
	{
		rem = n%2;
		quot = n/2;
		stack.Push(rem);
		rem=n;
	}


When will this loop ever end?

Jim


when we input 4 (decimal) and it should convert it to binary form but why its not converting?

what do u mean by that? this loop will end when it will convert the decimal to binary?

can you fix that code?
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,672
  • Joined: 25-December 09

Re: Run-Time Check Failure #3

Posted 26 February 2012 - 07:43 AM

Where do you ever change the value of n it the snippet I posted above? If you never change n the loop will never end.

Jim
Was This Post Helpful? 0
  • +
  • -

#11 joannesablad   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 24-April 11

Re: Run-Time Check Failure #3

Posted 26 February 2012 - 07:50 AM

View Postjimblumberg, on 26 February 2012 - 07:43 AM, said:

Where do you ever change the value of n it the snippet I posted above? If you never change n the loop will never end.

Jim


Then what value should i give to n?
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,672
  • Joined: 25-December 09

Re: Run-Time Check Failure #3

Posted 26 February 2012 - 08:06 AM

What is the purpose of this loop? What is the purpose of n? What are you actually trying to "push" onto your stack? What are you trying to convert to binary?


Jim
Was This Post Helpful? 0
  • +
  • -

#13 joannesablad   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 24-April 11

Re: Run-Time Check Failure #3

Posted 26 February 2012 - 08:09 AM

View Postjimblumberg, on 26 February 2012 - 08:06 AM, said:

What is the purpose of this loop? What is the purpose of n? What are you actually trying to "push" onto your stack? What are you trying to convert to binary?


Jim



'n' is the input that i will give it through keyboard, its a decimal value. i want to convert it to binary... thats what im doing but not working?
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,672
  • Joined: 25-December 09

Re: Run-Time Check Failure #3

Posted 26 February 2012 - 08:26 AM

I don't see that you are getting any user input in any of the code you supplied. How can you process user input if you never obtain any input from the user? Maybe you should step away from you computer and define the steps required to convert a user input into binary, then once you know what steps to take start to program your program your algorithm.



Jim

This post has been edited by jimblumberg: 26 February 2012 - 08:28 AM

Was This Post Helpful? 0
  • +
  • -

#15 joannesablad   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 24-April 11

Re: Run-Time Check Failure #3

Posted 26 February 2012 - 01:14 PM

View Postjimblumberg, on 26 February 2012 - 08:26 AM, said:

I don't see that you are getting any user input in any of the code you supplied. How can you process user input if you never obtain any input from the user? Maybe you should step away from you computer and define the steps required to convert a user input into binary, then once you know what steps to take start to program your program your algorithm.



Jim


well im getting an input from user in cin>>n; but its still not working.. can u run it and tell me whats the problem?

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

void Dec2Bin(void)
{
	Stack<char>  stack(10);
	int n;
	int rem;
	int quot;
	int base=2;
	cin>>n;
	while(n!=0)
	{
		rem = n%2;
		quot = n/2;
		stack.Push(rem);
		rem=n;
	}
	while(!stack.Is_Empty())
	{
		cout<<stack.Top();
		stack.pop();
	}
}
void main()
{
	Dec2Bin();
	system("pause");
}




Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2