Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 26 27 [28] 29 30 ... 91

Author Topic: Programming Help Thread (For Dummies)  (Read 100751 times)

Andyfuji

  • Bay Watcher
  • Chocolate Trotskies
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #405 on: January 24, 2012, 07:29:25 pm »

How would one go about writing and extracting data from text files in C++?  I haven't had much experience with file input/output operations.

I tried it myself, but my code just keeps returning gibberish.

With the function:
Spoiler (click to show/hide)
it returns:
Spoiler (click to show/hide)
The only thing in the opened file is a single line with "ABC" written on it.
Logged

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #406 on: January 24, 2012, 09:45:00 pm »

you need to include the fstream header,
#include fstream
thats used for file input / output
then you have to declare your file steam variables
ifStream inData; ofStream outData;
then you have to open the file you want to read from / write to
syntax filesteamvariable.open(source name);
so if you declared the same variables I did earlier it would look like this
inData.open("data.txt");
outData.open("data.txt");

and now for reading and writing, instead of using cin and cout, you're going to use inData and outData
or whatever you delcared them as, you're still going to use >> and << the same as if useing cin and cout.

example inData >> payrate; would import the first data in the file data.txt and store it in the variable payrate.

when you're done you need to close the files,
inData.close();
and
outData.close();

if the file doesn't exist the open statement fails and the input stream enters the fail state.  if the output file doesn't exist it is created.  by default old contents are erased when written to,

to add data to the end of the file, use the option ios:app
example
outData.open("data.txt", ios:app)
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #407 on: January 24, 2012, 09:57:31 pm »

One thing I noticed is that you return an array of chars, but the function is typed as returning a string. These are two different types.
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #408 on: January 25, 2012, 02:14:49 am »

The return line should be:
Code: [Select]
   return g_Out[];
Logged

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #409 on: January 25, 2012, 02:21:16 am »

The return line should be:
Code: [Select]
   return g_Out[];
That isn't valid C++.
Logged

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #410 on: January 25, 2012, 02:34:17 am »

I thought C was compatible with C++.
Logged

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #411 on: January 25, 2012, 02:38:38 am »

I'm pretty sure having an array name followed by [] isn't legal C either.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #412 on: January 25, 2012, 02:42:40 am »

Yea that doesn't look right. Assuming you can implicitly cast a char[] to a string, the correct line would be
Code: [Select]
   return g_Out;

ILikePie

  • Bay Watcher
  • Call me Ron
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #413 on: January 25, 2012, 02:46:52 am »

I'm pretty sure having an array name followed by [] isn't legal C either.
IIRC it's just like writing "*g_Out", though it's been a while since I used the array notation in C. I'm more of a pointer person.
Logged

kaijyuu

  • Bay Watcher
  • Hrm...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #414 on: January 25, 2012, 03:35:37 am »

*g_Out would give the first value in the array. Arrays are just pointers in C/C++. "variable[]" is invalid anywhere outside delete, iirc.


You'd either have to return a char*, or convert your char array to a string before returning.
Logged
Quote from: Chesterton
For, in order that men should resist injustice, something more is necessary than that they should think injustice unpleasant. They must think injustice absurd; above all, they must think it startling. They must retain the violence of a virgin astonishment. When the pessimist looks at any infamy, it is to him, after all, only a repetition of the infamy of existence. But the optimist sees injustice as something discordant and unexpected, and it stings him into action.

Antsan

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #415 on: January 25, 2012, 12:34:23 pm »

@Valid_Dark:
Quote
when I do the Gaussian elimination including my vertex that I want it gives me imaginary numbers
I know, you solved your problem, but I am really not sure how you managed to get imaginary numbers with Gaussian elimination.
I'm not sure what the "vertex" thing is, but well, I don't even need it for Gaussian elimination. I won't do it here, just build up the homogenous matrix for your problem. From there on it's simple Gaussian elimination, which only consists of addition and multiplication, so there is no way you would be getting imaginary numbers.

The basic formula for a parabola is
f(x)=a*x²+b*x+c
So for every coordinate given you have one formula with the three unknowns a, b and c. f(x) and x are given, x² is trivially calculated.

So, your three points are:
(10;353), (260;3) and (530;353)

This gives the formulas
353=a*100+b*10+c
3=a*260²+b*260+c
353=a*530²+b*530+c

Now to the matrix on which you will perform your Gaussian elimination:
Code: [Select]
   a   b c
100   10 1 | 353
260² 260 1 |   3
530² 530 1 | 353
Reduce to reduced echelon normal form and you will have the results for a b and c, which you can insert into the formula for a parabola as given above.
Yes, the numbers are huge, but you will never end up with imaginary numbers from here.
Logged
Taste my Paci-Fist

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #416 on: January 25, 2012, 02:22:20 pm »

it wasn't giving me imaginary numbers for a b or c, but instead for x intercepts.

Gaussian elimination gives the values

A  =  0.0052
B  =  -2.8000
C  =  380.4815

now when plugged into y=ax^2+bx+c

you get the x intercepts
269.2307692 + 26.15936027i   and  269.2307692 - 26.15936027i
which are imaginary numbers, this is due to the vertex being off by 5, the vertex is the midpoint of the parabola, and is found by
(max x value - min x value)/2 , whatever y value you want for height.
in my case the midpoint was 5 units to the right of where it was supposed to be.
and you do need a vertex for Gaussian elimination, or else you just have the starting position and ending position of where you want your item to travel, and a variety of a,b,and c values would work, (only 1 of which you actually want)
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Antsan

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #417 on: January 25, 2012, 04:29:12 pm »

I guess (as English is not my mother tongue) an intercept is the x-values for f(x)=0.
When you get imaginary values for those it means that the parabola does not cut the x-axis and there are no real-value intercepts. I have no idea why you think that there *must* be some of those.
And with the values given, where 260 (which is fairly close to the center between 10 and 530) is above the y-axis it isn't even a surprise!

Quote
and you do need a vertex for Gaussian elimination, or else you just have the starting position and ending position of where you want your item to travel, and a variety of a,b,and c values would work, (only 1 of which you actually want)
First: What item? Why travel? This is only a function and it doesn't describe the parabola of a thrown object as you would throw the object down for it to fall up again when using the standard interpretation for positive y-values meaning "up".
Second: When having 3 unknown values and 3 different non-redundant formulas you don't need additional information but you get exactly 1 possible solution for all three unknowns. Any parabola is exhaustively and uniquely described through three different points in the 2-dimensional plane the parabola goes through. There is no ambiguity and no multiple solutions to the system of linear equations.

Quote
(max x value - min x value)/2
This is definitely *not* right in this case, as this is not correctly defined because
a) max(x)=inf
b) min(x)=-inf
c) you need to add those values
Why that is, see above.
More correctly you can use:
xvertex=(x1+x2)/2 where f(x1)=f(x2)
With the already given points we can deduce that
xvertex=270
I don't want to check as I want to go to bed soon: Does that match with the values for a,b and c the Gaussian elimination gave you?

I hope I am helping you instead of hindering your progress or cluttering up your head.
Logged
Taste my Paci-Fist

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #418 on: January 25, 2012, 05:32:35 pm »


Quote
(max x value - min x value)/2
This is definitely *not* right in this case, as this is not correctly defined because
a) max(x)=inf
b) min(x)=-inf
c) you need to add those values
Why that is, see above.
More correctly you can use:
xvertex=(x1+x2)/2 where f(x1)=f(x2)
With the already given points we can deduce that
xvertex=270
I don't want to check as I want to go to bed soon: Does that match with the values for a,b and c the Gaussian elimination gave you?

I hope I am helping you instead of hindering your progress or cluttering up your head.

i'm using layman's terms, of course by max x and min x I didn't mean maximum possible or inf, but the starting point and ending point,
also I zero'd out the origin, meaning the starting point will always be 0,0 no matter what it really is (add back the x and y value to it later,)

Quote
Second: When having 3 unknown values and 3 different non-redundant formulas you don't need additional information but you get exactly 1 possible solution for all three unknowns. Any parabola is exhaustively and uniquely described through three different points in the 2-dimensional plane the parabola goes through. There is no ambiguity and no multiple solutions to the system of linear equations.

but without a middle point you only have 2 points, not 3

i've since developed an algorithm for finding the a and b values for the formula of the trajectory when entering distance you want it to go and height you want it to go.

    a = ((height*2) / (((distance/2)*(distance/2))*2));
    b = ((height * -2)/ (distance/2));

   
this will output the a and b value for the parabola fitting  whatever height and distance you want.
(opens upwards though because for the SDL graphical library up is down in regards to graphs, example y=200 would be lower than y=20)
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Chaos Armor

  • Bay Watcher
  • Grand Master Lurker
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #419 on: January 25, 2012, 10:19:12 pm »

Does anyone possess the knowledge of how to start other programs and import files in Small BASIC? I need the command and cannot find it.
Logged
Lurking since 2009

Leerooooooooy walk forward twice
Pages: 1 ... 26 27 [28] 29 30 ... 91