Solved

# Make a stack with float and int values

Posted on 2006-11-30
550 Views
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
Question by:MissProgrammer
• 2
• 2

LVL 2

Accepted Solution

score_under earned 250 total points
ID: 18048222
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

LVL 2

Expert Comment

ID: 18048270
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 Comment

ID: 18048375
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

LVL 4

Assisted Solution

chhokra_expert earned 250 total points
ID: 18055231
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

LVL 4

Expert Comment

ID: 18055333
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

## Featured Post

### Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …