Problem with stacks
Hm. Can't make it work. Can'st make it to convert decimal to binary and push it to the stack and output to the screen properly. Help
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
void ConvertNumber (int decimalNum, stackType<int> mystack); //takes in the decimal number to be converted and returns the binary representation in mystack.
int number;
int main()
ifstream infile;
ofstream outfile;
infile.open("inFile.txt");
if(!infile)
{
cout << "Error!!! Can't open" << endl;
return 1;
}
outfile.open("outFile.txt");
if(!outfile)
{
cout << "Error!!! Can't open" << endl;
return 1;
}
stackType<int> mystack(50);
stackType<int> copyStack(50);
stackType<int> dummyStack(100);
mystack.initializeStack();
while(infile>>number)
{
if (number>0)
{
ConvertNumber(number, mystack);
mystack.push(number);
}
else
cout << "ERROR : negative number not valid";
}
copyStack = mystack; //copy stack into copyStack
while (!mystack.isEmptyStack())
{
cout << mystack.peek() << endl;
mystack.pop();
}
cout << copyStack.pop() << " ";
infile.close();
outfile.close();
system("PAUSE");
return 0;
}
//====================================================================
void ConvertNumber (int decimalNum, stackType<int> stack)
{
while (decimalNum > 0)
{
ConvertNumber(decimalNum/2, 1);
stack.push(decimalNum%2);
}
}
|
For example if I have
ConvertNumber(decimalNum/2, mystack);
the prog says that 93 main.cpp [Error] 'mystack' was not declared in this scope
Line 1,23,24,25,60: Where is stackType declared?
Line 3: Why is number global?
Line 6: Missing {
Line 60: stack is passed by value. The values pushed onto it are lost when ConvertNumber exits.
the prog says that 93 main.cpp [Error] 'mystack' was not declared in this scope |
There is no lline 93 in the code you posted. What is the corresponding line?
Last edited on
Topic archived. No new replies allowed.