sfuo 111

You can use System.IO.Directory.GetFiles(DIRECTORY_NAME); which will return an array of strings that contain all the files (with full path) in that folder.

PulsarScript commented: Thank you +2
sfuo 111

Line 18 of what you posted makes no sense. It will always select maximum = maximum in the trinary operator and you cannot declare a variable equal to an undeclared variable (even if you could it would be holding a garbage value).

Line 19 you are now saying that the low guess is equal to some mystery value and your high guess is the maximum tolerated error? and then your maxError is now set to 0 and never used within your helper function.

In your helper function the only two lines that make sense are the first two where you calculate the average and the resulting fifth power of that value. Other than that I do not fully understand what you are trying to do because fifthPower - x will not give anything useful the way you are using it (maybe if you used maxError in here somewhere then it would make sense, but then again you are setting that to 0.0).

Here is what I think you are attempting to do

#include <iostream>
#include <cmath>

using namespace std;

double fifthRootHelper(double x, double maxError, double low, double high)
{
    if (x == 1.0) //special case for 1.0
        return 1.0;

    double avg = (low + high) / 2.0;  //use 2.0 not 2 just for the future case of running into integer division without knowing
    double fifthPower = avg*avg*avg*avg*avg;

    if (fabs(fifthPower - x) < maxError) //if our new guess is within our error, return that as the answer
        return avg;

    if (fifthPower ...
sfuo 111

I would suggest that you do not try to store all your information in labels, but rather have seperate variables that control the amount of each stat that your character has and then display those values in the labels.

I tossed together a quick example of how you can use buttons to increase and decrease the stats for your character. I am sure there are better ways of coding this but this should get you on track and going.

UI is here - http://i.imgur.com/zS0hM22.png

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DaniRPG
{
    public partial class Form1 : Form
    {
        Player me;
        Int32 remainingPoints;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            me = new Player();
            remainingPoints = 20;

            lblAgiPts.Text = me.Agility.ToString();
            lblIntPts.Text = me.Intelligence.ToString();
            lblStrPts.Text = me.Strength.ToString();
            lblStamPts.Text = me.Stamina.ToString();
            lblRemainingPts.Text = remainingPoints.ToString();
        }

        private void btnStrInc_Click(object sender, EventArgs e)
        {
            if (remainingPoints > 0)
            {
                me.Strength++;
                remainingPoints--;
                lblStrPts.Text = me.Strength.ToString();
                lblRemainingPts.Text = remainingPoints.ToString();
            }
        }

        private void btnStrDec_Click(object sender, EventArgs e)
        {
            if (me.Strength > 0)
            {
                me.Strength--;
                remainingPoints++;
                lblStrPts.Text = me.Strength.ToString();
                lblRemainingPts.Text = remainingPoints.ToString();
            }
        }

        private void btnAgiInc_Click(object sender, EventArgs e)
        {
            if (remainingPoints > 0)
            {
                me.Agility++;
                remainingPoints--;
                lblAgiPts.Text = me.Agility.ToString();
                lblRemainingPts.Text = remainingPoints.ToString();
            }
        }

        private void btnAgiDec_Click(object sender, EventArgs e)
        {
            if (me.Agility > 0)
            {
                me.Agility--;
                remainingPoints++;
                lblAgiPts.Text = me.Agility.ToString();
                lblRemainingPts.Text = remainingPoints.ToString();
            }
        }

        private void btnIntInc_Click(object sender, EventArgs e)
        {
            if (remainingPoints > 0) ...
sfuo 111

Not sure why you are switching values in the b array or why your swap function uses loops.

#include <iostream>
using namespace std;

int main()
{
    const int N = 3;
    double *a = new double[N*N];
    double *b = new double[N];
    double *x = new double[N];

    a[0] = 9;
    a[1] = 3;
    a[2] = 1;
    a[3] = 0;
    a[4] = 4;
    a[5] = 5;
    a[6] = 8;
    a[7] = 1;
    a[8] = 4;

    b[0] = 7;
    b[1] = 8;
    b[2] = 9;

    cout << "Before" << endl;
    for (int r = 0; r < N; r++)
    {
        for (int c = 0; c < N; c++)
            cout << a[r*N + c] << " ";
        cout << b[r] << endl;
    }

    cout << endl;

    for (int r = 0; r < N; r++)
    {
        //"Assume" maxIndex is the first element of the row
        int maxIndex = r*N;

        //Check to see if the rest of the elements are greater than current max index
        for (int c = 1; c < N; c++)
        {
            //If it is greater then set the max index to the current index
            if (fabs(a[r*N + c]) > fabs(a[maxIndex]))
                maxIndex = r*N + c;
        }

        cout << "Max = " << fabs(a[maxIndex]) << " idx = " << maxIndex - r*N << endl;

        //If the max index is not a diag then lets switch it
        if (maxIndex - r*N != r) //col != row aka diag
        {
            double tmp = a[maxIndex];
            a[maxIndex] = a[r*N + r];
            a[r*N + r] ...
sfuo 111

Line 17 should be display->array = (char **) malloc(sizeof(char*) * height);
Note that it is sizeof(char*) not sizeof(char) because you are trying to allocate char pointers which have a size of 4, where a char has a size of 1.

Also your display function will not work as intended because you did not null-terminate your char arrays ('\0'). You can either null-terminate them or print the arrays out char by char, opposed to trying to print each line using the c-string format option in printf.

sfuo 111

Since you have just recently started learning C++ and I don't know what programming background you have. I would suggest that you learn the foundations of the language before moving on to graphics.

While you are learning the lanuage you should think of really basic "games" that you could complete in a console window (i.e. tic-tac-toe, blackjack, and other easy card games). These games will help you by making you use a many parts of the C++ language all together.

After you feel more comfortable with the language then you should really think about what IDE and graphics library you want to use. Alternativly you can use a game engine that will handle graphics, physics, sound, GUIs, and much more, but this depends on if you actually want to learn how to program or just make games that are bound by their engine. Personally I like to try to make my own "libraries" with the exception of a graphics library because that would be just crazy.

I use OpenGL and VS2012, however if you are just starting out I would suggest using SDL or another "simplified" graphics library. This way you are not spending lots of time learning the graphics library and instead you can focus on learning how to use the language and understand how a game works.

Here is the C++ tutorial website that I used to learn the language and I still use this website as a reference for the standard libraries.

Here is a link to ...

sfuo 111

If you don't want to use the table you can just use a program to print out what the char for those hex values would be.

#include <iostream>
using namespace std;

int main()
{
    char message[] = { 0x4B, 0x50, 0x7B, 0xF1, 0xF4, 0xF5, 0x5E, 0x50, 0x7B, 0xF1, 0xF4, 0xF5, 0x5E, 0x4B, 0x4B, 0x4B };

    for( int i = 0; i < 16; i++ )
        cout << message[i];
    cout << endl;

    cin.get();
    return 0;
}

The message is junk but maybe you can make something out of it. I'll let you run it and see what it is.

sfuo 111

Or instead of using an int array you could use a map (or unordered_map if you have a compiler that supports C++11).

Accessing the values would be exactly the same but instead of having an array with a size of 97 + 26 (because 'a' is 97 (as said above) and then you need room up until 'z') you would have a map with 26 entries. Obviously maps have overhead that a normal array does not, but I think it would be good to play around with some of the STL containers so that you know how to use them and what each of them can and cannot do.

#include <iostream>
#include <map>

using namespace std;

int main()
{
    map<char, int> myMap;

    for( int i = 0; i < 26; i++ )
        myMap['a'+i] = i;

    cout << myMap['c'] << endl;

    cin.get();
}

By the way (I'm sure you just wrote it out and didn't actually test it) you defined an int array of size 10 but assigned/accessed out of bounds (in your second example).

DeanMSands3 commented: While NathanOliver and AncientDragon give "correct" answers, this is probably the most useful. +5
sfuo 111

When I used SDL for graphics I don't ever remember using SDL_GetKeyState(); however, it could have been just a preference that the writer turtorials I followed had.

My suggestion (it might not be the best option) is to create your own bool array to manage the key states. You can do this by calling SDL_PollEvent() and when SDL_KEYDOWN/UP is called set the key states to true/false respectivly. The major issue that I see with this is that num lock, caps lock and scroll lock all are toggled rather than having momentary states like all the other keys. So you can treat this as a special case or just ignore it if you do not plan on using those keys for anything.

The other things that I added was a fps timer since using SDL_Delay() actually freezes the program up and I changed the starting speed for the player because you cannot move with a starting value of 0.

I didn't change the whole increasing speed method but I think having to press the space bar 5 times before you notice any change is a bit odd.

"main.cpp"

#include <cstdlib>
#include <ctime> //for the timer

using namespace std;
#include <SDL.h>

#include "playertank.h"

int main ( int argc, char** argv )
{
    SDL_Init(SDL_INIT_VIDEO);

    unsigned int curTime = clock(); //used for fps timer rather than using delay

    bool keys[SDLK_LAST]; //array of keystates

    //you might want to set the initial states to that of what SDL_GetKeyState finds
    //since for some reason it could find ...
sfuo 111

Actually the code post is exactly what you wanted for the whole conversion from one base to another. This is just set to hex, if you want to it be general then replace all the 16s with a variable and expand the alphabet to Z.

You can write a check minimum base function and pass A, B and Z/R into it then run a loop checking to see if A + B = Z and A x B = R with the current base, if not then check the next. If you hit the maximum allowable base then return 0. If you find a base that works then run it to the end to see if it is unique or not.

If you cannot figure this out now then you are hopeless!

sfuo 111

By the way 120 can be of any base from 3 to inf.

Thats exactly what hes getting at. If you want to convert A2C3 you have to say what base it is in because this could be anything from base 13 to infinity the way you see it.

If you are assuming that A2C3 is in hex then you can loop through the string, converting each hex char to decimal and then add it to an integer variable.

#include <iostream>
#include <cmath>
using namespace std;

int hex2dec(string hexstr)
{
    int decval = 0;

    for( unsigned int i = 0; i < hexstr.size(); i++ )
        if( hexstr[i] >= '1' && hexstr[i] <= '9' )
            decval += (hexstr[i] - '0')*pow(16, hexstr.size()-1-i);
        else if( toupper(hexstr[i]) >= 'A' && toupper(hexstr[i]) <= 'F' )
            decval += (toupper(hexstr[i]) - 'A' + 10)*pow(16, hexstr.size()-1-i);

    return decval;
}

int main()
{
    int num = 0xA2C3;
    cout << num << endl;
    cout << hex2dec("A2C3") << endl;

    cin.get();
    return 0;
}
sfuo 111

From reading the wiki page for Greatest common divisor (GCF) it show to use Euclid's algorithm to find the GCF. The link that shows some pseudocode for using the algorithm is here.

And here is a recursive implementation.

int GCF(int a, int b)
{
    return ( (b == 0) ? a : GCF(b, a%b) );
}
sfuo 111

Yeah if you wanted to have it work for 2D and 3D points then you could either overload the function to do both 2D and 3D "hardcoded" or you can use a loop to sum all the squares then take a single squareroot at the end.

Example

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

double Distance(vector<double> pt1, vector<double> pt2)
{
    if( pt1.size() != pt2.size() )
        return 0;

    double sum = 0;
    for( unsigned int i = 0; i < pt1.size(); i++ )
        sum += (pt2[i]-pt1[i])*(pt2[i]-pt1[i]);

    return sqrt(sum);
}

int main(int argc, char *argv[])
{
    vector<double> pt1, pt2;
    pt1.push_back(150);
    pt1.push_back(50);
    pt2.push_back(15);
    pt2.push_back(20);

    cout << Distance(pt1, pt2) << endl;
}
sfuo 111

I kind of see what you are trying to do with the nested for() loops but it is wrong.

For distance I would just make a function that takes in the two coordinates, or this 4 element array, and then just plug it into the formula.

Also in C++ you want to use the cmath library for math and not the math.h C version.

#include <iostream>
#include <cmath>

using namespace std;

double Distance(int x1, int y1, int x2, int y2)
{
    return sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
}

double Distance( int coords[4] )
{
    return sqrt((coords[2]-coords[0])*(coords[2]-coords[0]) + (coords[3]-coords[1])*(coords[3]-coords[1]));
}

int main(int argc, char *argv[]) {

    int coord[2*2] = {150, 50,
                      15, 20};

    cout << Distance(coord[0], coord[1], coord[2], coord[3]) << endl;
    cout << Distance(coord) << endl;
}
phorce commented: Thank you! :)!!! +4
sfuo 111

There are a few methods for finding the det of a matrix.

Here is an example for using the minors method.

sfuo 111

The code that you are using for checking who wins is pretty bulky. If you just treat the hands like 0, 1 and 2 and that higher numbers beat lower numbers with the exception that 0 beats 2 then you can just use the mod operator to wrap around and see who wins.

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
    srand(time(NULL));
    int player, cpu;
    char option;
    int wins = 0, losses = 0, ties = 0;

    char hands[3] = {'r', 'p', 's'};

    while(1)
    {
        cout << "Enter (R)ock, (P)aper, (S)cissors, or (Q)uit:" << endl;
        cin >> option;

        if( tolower(option) == 'r' )
            player = 0;
        else if( tolower(option) == 'p' )
            player = 1;
        else if( tolower(option) == 's' )
            player = 2;
        else if( tolower(option) == 'q' )
            break;
        else
            continue;

        cpu = rand()%3;

        cout << "Player | CPU" << endl;
        cout << hands[player] << "    " << hands[cpu] << endl << endl;

        if( player == cpu )
        {
            cout << "Tie!" << endl;
            ties++;
        }
        else if( player == (cpu+1)%3 )
        {
            cout << "Player wins!" << endl;
            wins++;
        }
        else
        {
            cout << "CPU wins!" << endl;
            losses++;
        }

        cout << "Score:" << endl;
        cout << "w: " << wins << " | l: " << losses << " | t: " << ties << endl << endl;
    }

    return 0;
}

I added in a score keeping feature too.

sfuo 111

Yeah sorry my wording may have been a bit misleading. The variable i does have a value of 4; however, acc[i] contains a junk value. So what I guess I should have said is that your array "acc" has a junk value at index i and anything past that (since it is not being set anywhere).

Now what I do not know is why it is crashing for trying to access the index of the array with that junk value. I tried accessing the array at a large index and it just spat out a junk number but when I typed in a value of ~100000 or so it crashed like before. I'm not sure if there is a "magic" number around there but I'm sure there is a hard limit or just undefined behaviour going on. Either way you don't every play out of bounds like that anyways because you could be overwriting other allocated blocks of memory or could just simply crash like this.

sfuo 111

Your second for() loop is causing the crash because the first for() loop leaves i at a an index with a junk value since it increments then checks to see if ip_num is 0.

There are two quick ways to fix this:

1) You can assume that ip_num is never zero when entering the loop on the first run and move the break condition to the bottom of the loop.
2) You can start the second for() loop with i = i-1 (or i--).

Either way works and I'm sure there are many other ways to go about solving this problem but I didn't want to modifiy what you had too much.

sfuo 111

As far as functionality goes it works; however, you shouldn't be using labels/gotos because they are a bad coding habbit and make reading the flow of your code harder. Anywhere you use a goto statement you can replace it with a loop (which is what I did in this case).

Another thing that you should think about is making variable names that actually mean something. When I saw p, f, d, b I had no idea what those were until I looked over the program. So instead of using p as a name for your array of workers you can just call it workers or something descriptive.

You should also know that most programming languages are 0 base indexed so if you want an array that has 10 entries, it starts at 0 and ends at 9.

If you wanted to make this a bit more C++ rather than C you could use strings for the name of the worker and job, also you could use a vector, list or any other STL container that can dynamically resize to however many workers you want to create (or read in from the file if you want to add that feature).

#include <iostream>
#include <fstream>
#include <conio.h>

using namespace std;

struct work
{
    char name[100];
    int id;
    char job[10];
}workers[10];

int main()
{
    int numWorkers = 0;
    int option = 0;
    ofstream file;

    while(1)
    {
        cout << "\nThe db++ Database application\n1-To enter the data\n2-To read the data\n3-To save the data\n";
        cin >> ...
piyush3dxyz commented: hey sfuo....i am the beginner in the c++ programming...Its only 2 month of my c++ learning.....but thax for correcting me.... +0
sfuo 111

I have attached a rewrite of your program because if you ever want future help your code has to be structured.

I would recommend having main.cpp with pretty much just main() in it so you can read the flow of the program and if you want to know what happens in a function then you go open up that header/source file.

Obviously this is just the way I like to structure small games like this and others would disagree/change aspects of it; however, I would hope everyone would agree that having 99% of your code in main.cpp just makes finding errors extremely difficult.

There were many areas where you used the comparison operator (==) rather than the assignment operator (=) which doesn't do what you want and if you have warnings turned off it doesn't tell you that it does nothing.

As far as fixing the problem with the box goes, I changed it so that the dropping of the box is handled by the player class rather than having a seperate handle for the box. The main issue with the box in the first place was that you were making it visible by setting drop to true then right after you released the key you set it to false (which makes the box disappear).

If you have any questions feel free to ask.

ChrisMackle commented: THANK YOU!!!! +0
sfuo 111

On line 11 your function prototype says that approximateSqrt() takes no parameters so that is where that error is coming from. To fix it change that line to double approximateSqrt(double);

sfuo 111

You don't have a display function or an idle function. Not 100% sure if this is the problem since I haven't used glut in a while but from what I remember you need them.

sfuo 111

Like VernonDozier said, you are declaring 'char c' out of scope but there are also a few other errors within your program.

You are calling the function srand() at the start of the for() loop on each iteration which results in all the characters being the same. The solution to this is to call srand() once either at the start of this function or at the beginning of main() (I would suggest placing it at the start of main()).

The second problem is the range for your rand() function. You had [ICODE]rand() % 26;[/ICODE] which will generate a number from 0 to 25 not 0 to 26. So the solution is to just set your maximum to the maximum value in your range + 1.

I did a bit of tweaking and shortened your append section but for the most part it is the same.

Also notice that srand() is not being called here because it should be at the start of main().

[CODE]string genstring()
{
string generated_string;
string stringsize = inputsentance();

for( unsigned int k = 0; k < stringsize.size(); k++ )
{
    char c = rand() % 27 + 'A'; //random from "A-["

    if( c == '[' )
        c = ' ';

    generated_string.append(1, c);
}
return generated_string;

}[/CODE]

sfuo 111

Without actually knowing how it is implemented and by only playing around with these two classes it seems when there is a name conflict (in this case x) the default scope to be used is that of the class that is calling it. When inheriting another class the class that is being inherited is added to the scope of the "inheriter" if there is no conflict.

For example
[CODE]class B
{
public:
int x;
};

class D: public B
{
public:
int x;
};[/CODE]

would result in 'D' looking like
[CODE]class D: public B
{
public:
int D::x; //in the default scope so 'D d.x' would look at this
int B::x; //name conflict so would need to use 'D d.B::x' to access
};[/CODE]

As for implementing a structure like this in C I would say it is impossible to put two variables with the same name into the same structure since there are no namespaces or 'built-in' inheritance in C. I came across [URL="

sfuo 111

The issue lies within your function gasket().

On line 34 you are missing an equals sign between point and T, so it should look like
[CODE]GLPoint point = T[index];[/CODE]

Then when you are adding point.x and t[index] then dividing by 2 you really want to add each component then divide by two. So line 42 and 43 should look like
[CODE]point.x = (point.x + T[index].x) / 2;
point.y = (point.y + T[index].y) / 2;[/CODE]

Also I'm not sure about you but when I try to compile it gives me a bunch of undefined reference errors about all the OpenGL functions, so at the top I had to put
[CODE]#include <GL/gl.h>

include <GL/glu.h>

include <GL/glut.h>

[/CODE]

sfuo 111

It looks like all your errors/problems are in "cdrom.cpp".

In the function CDROM::setName() you have [ICODE]getline(cin, name)[/ICODE] being printed out in your cout statement. The function getline() returns *this which is a pointer to the istream object being used (I think, either way this is not what you want). Also you have cin.ignore() being used which is pointless because getline() reads the '\n'. cin.ignore() should be used after reading in a number (int, float, double) because when reading in a number you only read in digits, decimals and signs (+/-), which results in characters typed in after to be left in the input buffer (ie "22char\n" results in "char\n" being left in the input buffer).

The next function, CDROM::setType(), has the exact same problem as CDROM::setName() so get rid of the cin.ignore() and do not output getline().

In CDROM::setCost() you want to clear the input buffer after you input the number because it leaves characters in the buffer, but you do this after you get the input not before. In order to use the line [ICODE]cin.ignore(numeric_limits<streamsize>::max(), '\n');[/ICODE] you need to include <limits> at the top because numeric_limits<streamsize>::max() is defined within it. If you do not clear the input buffer then when this loops for the next CDROM input you will not see text pop up until you type in something.

Here is what I did to correct the errors.

[CODE]void CDROM::setName()
{
cout << "Please enter name : ";
getline(cin, name);
}

void CDROM::setType()
// Sets the CDROM Type
{ ...

sfuo 111

Since it says there are no duplicate inputs I'm guessing (and there is no way to check) there will be no need for checking if a duplicate number was entered. You can throw on a check to restrain input from [0,n).

This is what I came up with "1 day ago" according to this thread.

[CODE]#include <iostream>
using namespace std;

int main()
{
int n = 10, inpt;

for( int i = 0; i < n; i++ )
    cin >> inpt;

for( int i = 0; i < n; i++ )
    cout << i << endl;

return 0;

}
[/CODE]

sfuo 111

I wrote this linked list a while ago. After reading yours for a bit I went back and modified mine to use templates and used your names.

You can take a look at this and use what you want.

It has four functions:
Add() which appends a node to the list
AddAt() which inserts a node at an index
AddAfter() which inserts a node after the named node
Print() displays the contents of the list
[CODE]#include <iostream>
using namespace std;

template <typename T>
struct Node
{
Node(){};
Node( T n )
{
name = n;
next = NULL;
}
T name;
Node *next;
};

template <typename T>
class List
{
Node<T> *first;

public:

List()
{
    first = NULL;
}

void Add(T name) //adds a new entry to the list
{
    Node<T> *newNode = new Node<T>(name);
    Node<T> *curNode = first;

    if( curNode == NULL )
    {
        first = newNode;
        return;
    }

    while(curNode->next != NULL)
    {
        curNode = curNode->next;
    }
    curNode->next = newNode;
}

void AddAt(T name, int index)
{
    Node<T> *newNode = new Node<T>(name);
    Node<T> *curNode = first;
    Node<T> *nexNode = 0;
    Node<T> *prvNode = 0;

    if( curNode == NULL )
    {
        first = newNode;
        return;
    }

    int i = 1;
    while(curNode->next != NULL && i != index)
    {
        prvNode = curNode;
        curNode = curNode->next;
        i++;
    }

    if( i != index )
    {
        curNode->next = newNode;
        return;
    }

    if( prvNode == NULL )
    {
        nexNode = first;
        first = newNode;
        newNode->next = nexNode;
        return;
    }

    nexNode = prvNode->next;
    prvNode->next = newNode; ...
Braindead90 commented: Gave a detailed explanation of suggestion even with comments inside code... Very Helpful +0
sfuo 111

I would use two for() loops. You can use a while() loop but I tend to avoid them when you want to count through a range of values.
[CODE]#include <iostream>
using namespace std;

int main()
{
int num1, num2;

cout << "Enter a positive integer: " << endl;
cin >> num1;
cout << "Enter another positive integer: " << endl;
cin >> num2;

if( num1 > num2 )
{
    int tmp = num1;
    num1 = num2;
    num2 = tmp;
}

//low to high
for( int i = num1; i <= num2; i++ )
    cout << i << " ";
cout << endl;

//high to low
for( int i = num2; i >= num1; i-- )
    cout << i << " ";
cout << endl;

return 0;

}
[/CODE]
I didn't throw in any checking for the two inputs.

sfuo 111

One huge problem that you had was that you were not allocating memory for your stack. The reason why you did not get run-time errors for this is because you were never actually using your stack, which is why (along with a few other things) you were getting garbage values for the initial test.

Within the oper() function you have a check that attempts to see if there are at least two numbers in the stack and in your case that is never true so it just returns. I would suggest you remove that all together and take advantage of the fact that your pop() functions return a bool.

Out of all the functions you have the only one that is correct is the pop() function. Your push() function is changing the memory address of mPtop when what you really want is to change the value at mPtop's memory address, and you need a range restriction on that function so you are not pushing too many values onto a stack of a limited size (otherwise you go out of bounds of the memory allocated and this results in a run-time error).

For the oper() function your division function is wrong and you need to look up how to divide complex numbers (I just typed in dividing complex numbers into google) and attempt to come up with the same result that I put below (I did it all with variables on pen and paper and then transferred it into code directly). ...