Thread: need help with data structure

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    23

    need help with data structure

    My assignment is to create a first in last out data structure but it must be able to take floats and strings. function overloading is supposed to be used for the push() method. I tried figure out how to make it take strings and floats but i failed miserably so i came here hoping to get steared on the right track. heres my code so far.......

    Code:
    #include <iostream>
    #define SIZE 4
    
    class stack
    {
       private:
          int top_of_stack;
          double data[SIZE];
          
       public:
          int init();
          stack();
          int push(int put_on_stack);
          float push(float put_on_stack);
          char push( char put_on_stack);
          int pop();
    };
    
    
    int main()
    {
       using std::cout;
       using std::cin;
       using std::endl;
       stack s1;
       s1.push(58);
       s1.push(23);  
       s1.push(12);
       s1.push(23);
       s1.push(54);
       
       s1.pop();
       s1.pop();
       s1.pop();
       s1.pop();
       s1.pop();
       s1.pop();
       cin.ignore();
    }
    
    stack::stack()
    {
       top_of_stack = 0;
    }
    
    int stack::init()
    {
       top_of_stack = 0;
    }
    
    int stack::push(int put_on_stack)
    {
       if (top_of_stack >= SIZE)
       {
          std::cout<<"Stack is Full!!"<<std::endl;
       }
       data[top_of_stack] = put_on_stack;
       ++top_of_stack;
    
    }
    
    float stack::push(float put_on_stack)
    {
       if (top_of_stack >= SIZE)
       {
          std::cout<<"Stack is Full!!"<<std::endl;
       }
       data[top_of_stack] = put_on_stack;
       ++top_of_stack;   
    }
    
    int stack::pop()
    {
       if (top_of_stack == 0)
       {
          std::cout<<"Nothing on Stack!!";
          return 0;
       }
       top_of_stack--;
       std::cout<< data[top_of_stack]<<std::endl;
    }

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    your push() functions all specify a return type, but none of them actually return a value at the end. You either need to return a value, or, if you don't want to do that, change their return types to void. Similar complaint about your pop() function (the compiler generates a warning because the path to the end of the function doesn't return a value)

    On to pushing a string onto a stack of doubles - I assume that the string is in fact going to be convertable to a double, eg "12.3456" - the best way to convert strings to other types in C++ is with a stringstream, eg
    Code:
    double strToDouble(std::string in)
    {
       std::stringstream ss(in);
       double d;
       ss >> d;
       return d;
    }
    Last edited by Bench82; 05-01-2006 at 07:31 PM. Reason: fixed mistake

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    23
    Thanks! but minor problems. I get an error that says... variable std::stringstream ss` has initializer but incomplete type. Also does this definition allow for words to be added on to the stack? for example...s1.push("five").

    Also a couple of questions on the definition. is "in" a command or just a parameter name? and is "ss" part of a command? and what does .......ss >> d.. do? And im assuming stringstream just converts a number to a string correct?

    and after i graduate high school in a couple of weeks would you recommend taking C++ data structures?




    Sorry im a question asker. thats how i learn most things.
    thanks.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    1. std::stringstream is declared inside <sstream>. #include it.

    2. If you want, you can declare a seperate class called CombinedClass which has an int and a string, then encapsulate all the string/int/double/etc assignments and conversions inside. The stack class can pass all types of data into your CombinedClass without fuss.

    3. You declare a function as "<return_type> <fn_name> ( <arg_type> <arg_name>, ... )". So std::string is the <arg_type>, and "in" is the <arg_name>. Basic!

    4. std::stringstream can do anything std::istream and std::ostream can do. "ss >> d" will make ss behave like cin, and stream it's data into d.
    If you overload the I/O stream operators for your own defined classes, stringstream will inherit the defined functionality.

    5. What do you mean by "C++ data structures"? Data structures are a universal programming concept. They can be implemented in C++, BASIC, Python... heck, even a Turing machine. (If you didn't know what that last one meant, better go look it up)

    If you meant the STL And Friends™, you won't need to wait; you can pratically breeze through them in an afternoon.
    Last edited by jafet; 05-02-2006 at 02:50 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by Rumproast23
    does this definition allow for words to be added on to the stack? for example...s1.push("five").
    No - that's more difficult to do (Well, ok, maybe not that difficult - but you will need to create a lookup table and convert all the words manually). The built in conversions through stringstreams convert the character representations of numbers (usually ASCII), into numeric types such as int or double, eg, "12.345" as a string, into 12.345 as a double.

    Also a couple of questions on the definition. is "in" a command or just a parameter name?
    Yes, that's just a parameter name - when you call the strToDouble() function, you would pass it a string.

    and is "ss" part of a command? and what does .......ss >> d.. do?
    ss is a stringstream object, a stream - which means it works in a very similar way to cin. if you used cin >> d - then your program would prompt you to type in a number as text - and that number would be passed, as a double, to d (because the stream knows how to convert between strings and built in types).
    The difference here is, ss gets initialised by the contents of the string in. cin is a bit of a special-case stream object, in that it is able to read from your OS's keyboard input buffer.

    And im assuming stringstream just converts a number to a string correct?
    stringstreams can do much more than that, but in this case, yes, that's what it's doing. (actually, it's the other way around: converting a string to a number)

    Sorry im a question asker. thats how i learn most things.
    thanks.
    Asking questions is fine - Much better that than wandering around aimlessly without a clue what's going on!
    Last edited by Bench82; 05-02-2006 at 01:26 PM. Reason: small correction

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    int stack::push(int put_on_stack)
    {
       if (top_of_stack >= SIZE)
       {
          std::cout<<"Stack is Full!!"<<std::endl;
       }
       data[top_of_stack] = put_on_stack;
       ++top_of_stack;
    
    }
    it should be
    Code:
      if (top_of_stack >= SIZE-1)
    And even if that test fails you put the data into the array.
    Kurt

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    23
    Hey thanks everyone!

    to the person that replied to me about taking C++:data structures....I think its more of an error handling course and techniques. Its the third course after taking C++ I and C++ II.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Include GTK+ just for a data structure?
    By Jesdisciple in forum C Programming
    Replies: 0
    Last Post: 04-12-2009, 07:19 PM
  2. pthread question how would I init this data structure?
    By mr_coffee in forum C Programming
    Replies: 2
    Last Post: 02-23-2009, 12:42 PM
  3. Data structure implementation
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 07-31-2003, 07:44 AM
  4. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 06:03 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM