• Status: Solved
• Priority: Medium
• Security: Public
• Views: 570

Make a stack with float and int values

I have this program that I have to do where I must create a message with 2 random  numbers with a int and float data type.
message int =12  message float=12.2
Then I have to put the numbers onto a stack.  How do  I create a float random number for the stack and how do I get them to show at the same time or put them on at the same time when creating my stack?
0
MissProgrammer
• 2
• 2
2 Solutions

Commented:
The stack could be just a struct specially designed: This code will have functions to simeltaneously push and pop from a stack (struct, actually). Since I don't have my C compiler to hand, I can't test this, but you should be able to correct it if there is anything wrong.

(inside struct)
int point=0;
int random_num[256];
float random_dec[256];
(after struct)
void Push_That( YOUR STRUCT TYPE *struct_ptr, int the_int, float the_dec)
{
struct_ptr->random_num[struct_ptr->point] = the_int;
struct_ptr->random_dec[struct_ptr->point] = the_dec;
struct_ptr->point++;
return;
}
void Pop_That( YOUR STRUCT TYPE *struct_ptr, int* the_int, float* the_dec)
{
*the_int=struct_ptr->random_num[struct_ptr->point];
*the_dec=struct_ptr->random_dec[struct_ptr->point];
struct_ptr->point--;
return;
}
0

Commented:
Oops... On Pop_That, put struct_ptr->point--; above the other 2 lines of code, but DO NOT DO IT ON THE PUSH function ok?
0

Author Commented:
When I put this in:

#ifndef TEST_H
#define TEST_H

using namespace std;

struct Test{

int point=0;
int random_num[256];
float random_dec[256];
void Push_That( unsigned *struct_ptr, int the_int, float the_dec);
void Pop_That( unsigned *struct_ptr, int* the_int, float* the_dec);
};
void Test::Push_That( unsigned *struct_ptr, int the_int, float the_dec)
{
struct_ptr->random_num[struct_ptr->point] = the_int;
struct_ptr->random_dec[struct_ptr->point] = the_dec;
struct_ptr->point++;
return;
}
void Test::Pop_That( unsigned *struct_ptr, int* the_int, float* the_dec)
{
*the_int=struct_ptr->random_num[struct_ptr->point];
*the_dec=struct_ptr->random_dec[struct_ptr->point];
struct_ptr->point--;
return;
}
#endif

I get these errors:

\program files\microsoft visual studio\myprojects\tttt\test.h(9) : error C2252: 'point' : pure specifier can only be specified for functions
c:\program files\microsoft visual studio\myprojects\tttt\test.h(17) : error C2227: left of '->random_num' must point to class/struct/union
c:\program files\microsoft visual studio\myprojects\tttt\test.h(17) : error C2227: left of '->point' must point to class/struct/union
c:\program files\microsoft visual studio\myprojects\tttt\test.h(18) : error C2227: left of '->random_dec' must point to class/struct/union
c:\program files\microsoft visual studio\myprojects\tttt\test.h(18) : error C2227: left of '->point' must point to class/struct/union
c:\program files\microsoft visual studio\myprojects\tttt\test.h(19) : error C2227: left of '->point' must point to class/struct/union
c:\program files\microsoft visual studio\myprojects\tttt\test.h(24) : error C2227: left of '->random_num' must point to class/struct/union
c:\program files\microsoft visual studio\myprojects\tttt\test.h(24) : error C2227: left of '->point' must point to class/struct/union
c:\program files\microsoft visual studio\myprojects\tttt\test.h(25) : error C2227: left of '->random_dec' must point to class/struct/union
c:\program files\microsoft visual studio\myprojects\tttt\test.h(25) : error C2227: left of '->point' must point to class/struct/union
c:\program files\microsoft visual studio\myprojects\tttt\test.h(26) : error C2227: left of '->point' must point to class/struct/union
Error executing cl.exe.
0

Commented:
is there a reason you can't use stl stack? also i'm assuming int random is different from trucated version of float

create a struct / class to contain the float and int parts. initialize both member vars and push the struct ptr on to the stack. ps: i haven't compiled this code, but should be relative easy to fix if any errors come up.

#include <iostream>
#include <stack>
#include <cstdlib>
#include <time.h>

using namespace std;

class Message
{
public:
int f;  //these are public for easy access. could use a struct as well if these are teh only things / operations
int n;

Message() : f(rand()*100.0F/RAND_MAX), n(rand())  //initialize members with random values
{
;
}

virtual ~Message() { ; //add dtor code here if needed }
};

int main(int argc, char** argv)
{
srand(time(NULL)); //initialize psuedo rand gen
stack<Message *> messageStack;
Message *pM = new Message();

messageStack.push(pM);  //push newly created message on the stack

//do whatever else you want here with stack

//clean up
while(!messageStack.empty())
{
delete messageStack.top();
messageStack.pop();

}

return 0;
}
0

Commented:
btw, f should be a float not an int.. and you want to either comment out the destructor entirely or split it separate lines
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.