c++ - How can I stop my integers from displaying as HEX? -


i practicing code implementing different data structures. example trying implement simple stack data structure. far works intended, keep getting hex characters when trying display stack. me figuring out why case?

also trying better @ structuring code properly, can involved in industry please give me constructive criticism have coded far. thanks.

#include <iostream>  using namespace std;  // stack_max == maximum height of stack const int stack_max = 10;  class stack{     public:         stack(){         //constructor initializes top of stack             top = -1;         }          bool isfull(int top){         //isfull() check make sure stack not full         //will return true if stack full, , false if          //stack not full             if(top == stack_max - 1)                 return true;             else                 return false;         }          bool isempty(int top){         //isempty() check make sure stack not empty         //will return true if stack empty, , false if         //stack not empty             if(top == -1)                 return true;             else                 return false;         }          void push(int x){         //push() push new element on top of stack             if(isfull(top)){                 cout << "sorry, stack full!" << endl;                 exit(1);             }             else{                 top++;                 x = stk[top];             }         }          void pop(){         //pop() pop top element stack             if(isempty(top)){                 cout << "sorry, stack empty!" << endl;                 exit(1);             }             else{                 cout << stk[top] << " being popped stack!" << endl;                 top--;             }         }          void display_stack(){         //diplay_stack() show elements in stack             int temp;   //will temporarily hold position of stack             temp = top;             while(!isempty(temp)){                 cout << stk[temp] << endl;                 temp--;             }         }     private:         int top;         int stk[stack_max]; };  int menu(){      int choice;      cout << "welcome stack!" << endl;     cout << "what do? (select corresponding #)" << endl << endl;      cout << "1. push" << endl;     cout << "2. pop" << endl;     cout << "3. display" << endl;     cout << "4. quit" << endl;      cin >> choice;      return choice; }  int main() {     int selection, x;      stack mystack;      selection = menu();      while(selection != 4)     {         switch(selection){             case 1:                 cout << "please enter number pushed: ";                 cin >> x;                 mystack.push(x);                 selection = menu();                 break;             case 2:                 mystack.pop();                 selection = menu();                 break;             case 3:                 mystack.display_stack();                 selection = menu();                 break;             default:                 cout << "oops that's not selection, try again" << endl;                 selection = menu();                 break;         }     }      cout << "thank stopping , using stack!" << endl;     system("pause");     return 0; } 

as pointed out prehistoric penguin, push() function incorrect:

x = stk[top]; 

should changed to:

stk[top] = x; 

i wanted comment anyway offer general comments requested:

  • if statements can replaced single line of code:

        if(top == stack_max - 1)         return true;     else         return false; 

becomes:

return (stack_max - 1 == top); 
  • put constant expressions on left-hand side of comparison expression. example:

    (top == stack_max - 1)

becomes:

(stack_max - 1 == top) 

the reason 1 day accidentally type like:

(top = stack_max - 1) 

and or else waste lot of time debugging :)

  • your isfull() , isempty() functions shouldn't take parameter. should use private member variable top. how call these functions without access top, you've correctly made private member?

  • in general, avoid using. in opinion defeats whole purpose of namespaces. using namespace std commonly used exception, then, hard type std::cout?

  • always put curly braces around clauses of if statement, if 1 line. it's easy forget add braces if need add more statements clause later on, can quite confusing.

  • your code formatting pretty good, pick bracket style , consistent. either put opening curly brace on same line function header / control statements etc, or put on line afterwards.

hope helps.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -