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

I am not a Unix/Linux user so I am not 100% sure but after reading on GetOpt here and a GetOpt example here I would say the short answer is you must write myprogram.c -t file1 file2 file3 because it parses for options first.

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

Just a question.
Could you tell me how you thought this:

a[maxIndex] = a[r*N + r];

I mean the index r*N + r.
As I can see it gives the diagonal elements.
I didn't know about that!

The diagonal elements occur when the row index is equal to the column index. We use "r" in this case because we are iterating through the rows with the outer most for loop. If you write the matrix out on paper and try to come up with general solutions using only constants (N in this case) and variables that you are controling (r and c), you can come up with equations that you can input directly into your code.

Also, do you have a solution using the max value instead of the max index?

Because ,I can't figure why I couldn't get the proper max value.

You could use the max value but you will have to keep track of the column index of the max value (unless you want to scan through the columns after finding the max value to find it again and do the swap, which I think is a bad idea).

for (int r = 0; r < N; r++)
{
    //"Assume" maxValue is the first element of the row
    double maxValue = fabs(a[r*N]);
    int col = 0;

    //Check to see if the rest of the elements are greater than current max value
    for (int c = 1; ...
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

If you want to make a console application (the black cmd window) you select the Win32 Console Application project type. If you do not want the console to show then you create a Win32 Project, if you are using SDL or GLUT then you can call some of their library functions to create a window and then you just pass in functions into a callback that they specify (if you follow a tutorial you will figure out what I'm talking about). If you do not use SDL or GLUT (or similar library bundles) then you will have to look into WinAPI and it is not fun because it is written in C and has all of Window's custom data types and functions.

I'm going to assume that you are starting with SDL. If you read Lazy Foo's first tutorial and select Window > Visual Studio 2010, he gives you a step by step of what you need to do to get started.

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 are new to game programming then you should start with a 2D game. There is a massive gap between 2D and 3D if you plan on writing any of your own code (physics or other engine elements).

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

All three errors are from line 23 in your original post. If you read my post then you should have been able to correct that line and you would not have any more errors.

sfuo 111

There are a few things wrong with your code and you can make a few improvements too.

You are getting those errors because your syntax is wrong. If you wanted to input that equation it should look like cout << (storage_f_to_c - 32.0f)*5.0f/9.0f << endl;. So not only is the equation incorrect, but you had the multiplication operator with a closing bracket to the left (you would never see this in written math or input for a computer). Another problem is that you have "<<" at the end of the line and that is looking for a right-hand operand.

Some imporovements that you could make would be to use a variable like "input" to store both enter_f_to_c and enter_c_to_f (and if you were to expand this to Kelvin and Rankine then you would need loads more variables), also should make "result" hold the storage variations of these variables and the variables enter, a, and b can go into a single char variable called "option" and to select which equation to use you can check it with a switch() statement.

#include <iostream>
#include <cmath> //in C++ use cmath not math.h (this goes for all the standard .h headers from C)

using namespace std;

int main()
{
    char option;
    float input;
    float result = 0.0f;

    cout << "If you would like to convert Fahrenheit to Celsius,\n type the lowercase letter -a- (without the hyphens).\n If you would like to convert Celsius to Fahrenheit\n type the lowercase letter -b- (without hyphens).\n You also ...
sfuo 111

I think the biggest problem you are having is that the matrix you think you have is not the one you set up. As you can see I printed out the contents of the matrix and it is correct because I am adding the values of what row and column the for() loops are on.

I also see that you are kind of accessing the matrix with the x and y values being backwards. If you think about it rows are on the y axis and columns are on the x axis. For this example it will not matter since this matrix is <insert special name that I learned in linear algebra here>, which means that it is mirrored about the diagonal.

If you are going to use the new operator then you will have to store the resulting value that you are at in the matrix to a variable that you will return and then delete the memory allocated for the matrix (otherwise you will have a memory leak).

#include <iostream>
using namespace std;

int boundary2D( int xpos, int ypos, int xstep, int ystep )
{
    const int x = 4, y = 4;
    int matrix[y][x];

    for( int r = 0; r < y; r++ )
        for( int c = 0; c < x; c++ )
            matrix[r][c] = r+c;

    for( int r = 0; r < y; r++ )
    {
        for( int c = 0; c < x; c++ )
            cout << matrix[r][c] << " ";
        cout << endl; ...
sfuo 111

Why don't you treat this like two 1D boundary conditions (like the other post that was solved)?

If you create a position structure (just contains two ints or doubles for x and y position) then you can return that rather than creating a 4x4 matrix (which you did not allocate memory for).

I'll give you a chance to attempt this before posting any code to help you out because what you need already exists in the other thread and the rest seems pretty straight forward.

sfuo 111

Modulos gives the remainder of the two numbers if they were used in division.

Places where I use the mod operator are where I see wrapping. So if I had a problem where I wanted to know what angle a line was making from the center of a cricle, I would probably want it to be from [0-360) and not some larger or smaller angle.

The reason why I used static_cast<int> is because I was just trying to show that it will give an int and not pop out a x.5 (and for some reason you are returning double on your function when you are dealing with ints).

Here are two methods that you can use. One is just a cleaned up version of what you posted (an if with the same condition of the do{}while() can be replaced with just a while()) and a method that uses % for both positive and negative steps.

Cleaned up while() method:

int boundaryWHILE( int pos, int step, int size )
{
    if( step == 0 )
        return pos;

    int fpos = pos + step;

    while( fpos < -(size/2) )
        fpos += size;

    while( fpos > (size/2) )
        fpos -= size;

    return fpos;
}

mod method:

int boundaryMOD( int pos, int step, int size )
{
    if( step == 0 )
        return pos;

    step %= size;

    if( step < 0 )
        step += size;

    return (pos + size/2 + step)%size - size/2;
}

The modulus method can probably be cleaned up a bit ...

sfuo 111

I don't know what this "sth" is and I don't know what assumptions you are making as far as the size coming in goes, but here is a method you could use.

int boundary( int pos, int step, int size )
{
    return (pos + static_cast<int>(size/2) + step)%size - static_cast<int>(size/2);
}

The assumptions that are being made here are that size will be odd and that the range of values will always be equal in both directions off 0. And that step is going to be positive.

sfuo 111

You can use strings rather than chars.

If you do not want to read in spaces then you can use cin as your method of input. If you want to include spaces (something like a frist and last name) then you need to use getline(cin, str) for input.

sfuo 111

I would like to say that I agree with WaltP and that you should start off with learning the language before trying to hop into making a game with graphics. The first game I ever made was black jack in a console window (looking back on it, it was a POS) and I learned loads.

However, another option is to use SDL. I learned from it but quickly switched off to OpenGL to attempt some 3D "games".

Here is the tutorial website I used. This shows you how to setup SDL for Code::Blocks and a few other IDEs.

sfuo 111

In <windows.h> ERROR is defined as 0. So unless you were to undefine ERROR before your enum you would be writing

enum ERR_CODE { SUCCESS, 0 };

because the preprocessor just does a "find-replace" for ERROR and swaps it with 0.

A quick solution would be to just define SUCCESS as 1 so then ERROR is 0 and SUCCESS is 1. This is backwards to how you wanted it but unless you undefine ERROR or come up with a different name you cant do anything else.

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

On line 16 you need scanf("%d", &a[n-1]);

It was not working because scanf() takes in a pointer to the variable in which you want to write to.

The reason why you need to write to n-1 is because if you have an array that has a length of 5 the indicies that you should be accessing for that range are 0, 1, 2, 3, 4 (note that you do not access 5).

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

If you use atan2(y,x) it handles divide by zero and what quadrant the angle lies automatically.

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

The variable ans is initialized but not set.

Right before your for() loop you should write [ICODE]ans = num;[/ICODE] otherwise ans will just have a garbage value in it.

[B]EDIT[/B]

Or you can change line 7 from [ICODE]cin >> num;[/ICODE] to [ICODE]cin >> ans;[/ICODE].

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]