tetron 22 Junior Poster

There are good general prinicples of OO you are adopting. And when your code gets long you want to test things a function at a time.

I think there is a potential issue with the player choose function.
you might find it simpler to have a function to process the input
seperately as there is no error checking

atoi would normally take a c_string rather than a single char
so this might cause problems putting in the address of ones[1] but it might handle it ok but it would be safer to take everything but the first letter

string ones;
int col_id, row_id
cout << "enter a number " << endl;
cin >> ones;
if(ones.size() >= 2)
{
col_id = convert(ones[0]);
//get all but the first letter of ones
ones = ones.substr(1);
row_id = atoi(ones.c_str())
}
else
{
//error
}
//now check col_id & row_id are valid

so you might prefer putting in letter then a number
with two inputs that way if the user only entered one letter
your code will not fail.

also v[two - 1][onez -1] might go out of range if convert finds a non-letter if this happens your code will fail
as v[-1] does not exist
so you should check

this is the issue with your rand %8 is in range 0-7
%8 - 1 in range -1 to 6

a couple of minor things to consider

you have if() …

mitrmkar commented: good atoi catch +5
tetron 22 Junior Poster

It's difficult for me to explain, I'll try to, but perhaps someone else can explain better.

ditto :)

When you say: int x; or alpha my_alpha The computer has to create the space in memory that it needs to
store all of the information about the variable.
So for int x the computer allocates space for an int in memory
and points something to it that you are calling x.

now when you say

int x = 4;

in effect an x is created with an unknown value using x() in it
then this value is set to 4

where as

int x(3);

creates x but this time with 3 set immediately as if there is
x(int val) constructor

When you use the : You can call the constructors of other classes before the computer has finished creating the class
when it reaches the first { all of the memory has to be stored

so in your code it is as if you are saying:

gamma(int a, int b, float c)
:alpha(a * 2), u(a), [B]beta()[/B]
{
//all variables need to be created with defaults by here
gamma(c, c);
//you can then set specific values in here too
}

this implicit beta() constructor does not exist as you did not define it
and beta(float a, float b) cannot be used as the computer has no values for the floats

tetron 22 Junior Poster

Assuming that in all your own .h you have #pragma once

If you rename _objects_ to _abcdefgh_1234
do you get the same error with "_objects_"
replaced with _abcdefgh_1234 ?

if not then you have two places where you have used the same function name and this needs to be fixed it could be var:: is used elsewhere

I always uses classes and don't use the using so as not to confuse
visio intellisense or risk polluting the namespace

I am not familiar with the SDL libraries that you are using
but I assume that you are trying to get some game to work
without using the windows api.

It might be missing something obvious... but there is a lot of
includes and code that are not being shown here and an equivalent
code for me does not give an error

works , but .... even with "#pragma once" on top Im getting

...
In function main
multiple definition of `var::_objects_
first defined here
In function main
multiple definition of var::_img_
first defined here
In function main
multiple definition of var::_font_
first defined here
In function main
multiple definition of var::_sound_
first defined here
In function main
first defined here|
In function main
multiple definition of var::quit
first defined here
In function main
multiple definition of var::pause
first defined here
...

obejcts.h

#pragma once

#ifndef OBJECTS_H_INCLUDED
#define OBJECTS_H_INCLUDED


....

using namespace std;

class game_object
....

class cpong1 : public …
tetron 22 Junior Poster

here i wanted to just put the added value in the suggested variable like above x and y from the main()..
when i tried to do it in the main() , i dunno but where it picked the garbage value..
becoz i think the function >>i checked it using debugging and it worked perfectly....can u just tell me from where it was picking the garbage value???

with i and r being private
does
a.r compile for A()
you need accessors or public varibales

I suspect that your output is if you compile
"0+0i"
ok there are two errors in your code both occurring at the same place when you call the A() function

your set function should be working fine.

1- if you look at what jonsca says it will fix your problem
when you put re and im into the function
the function creates two new variables
as if it says
double re2 = re;
double im2 = im;
and in your function you are changing re2 & im2

to change the value is as jonsca says you need a reference
look at the & he has added! try it

2 - this mistake won't stop you function from working but if you do
c = a + b;
or
a += b;
should really be how you perform an addition
so either update

sleepybug commented: precisely,just what i needed.thanks a lot man! +0
tetron 22 Junior Poster

main should always return an int, not be void.

Why? It compiles to valid code, The extra return 0 is just more code to maintain.

SLEEPYBUG:
There are some general things that help when writing code
and it is best to start early:

give your functions meaningful names
A(); says little about your intention add() would be a better name.
Complex C;
is this intended just as a temporary variable fine
but it is not needed
x = a.r + r;
y = a.i + i;
will work with the references.
My concern is that you were intending to return C;
and have not done it. Did you intend this?

Complex Add(Complex a, double dr, double di);
Complex Add(Complex a, Complex b);

otherwise the reference will be fine but what is it you are wanting
the function to do ?
you might want a display() function for instance

tetron 22 Junior Poster

I am using a different version of compiler so different errors but
have you scoped vector?
the std::

//var.h
#pragma once
#include <vector>
namespace var
{
std::vector<int *> vpi;
std::vector<double *> vdi
}

works for me.

tetron 22 Junior Poster

I wrote a program to compare two text files character by character and its not working probably an easy thing im just not seeing.

for( int i = 0; !ifs2.eof(); i++ ){
    getline( ifs2, s2 );

    if( !ifs1.eof() ){
      getline( ifs1, s1 );

      c1 = s1[i];
      c2 = s2[i];
      if( c1 != c2 )
        cout << endl << "Files differ at line" << (i + 1) << endl;
    }
  }

your line

c1 = s1

i is stepping for lines but you are only cheking the index every
i chars

you do not iterate across line so only are checking a diagonla of cahrs

tetron 22 Junior Poster

Assuming that your code was showing multiple files
including the same file indirectly

by adding the preprocessor line to the top of all of your .h files

#pragma once

you can ensure that no single file includes the same file twice.

tetron 22 Junior Poster

Well I did ask :) , I had to look-up a lot of the scientific terms and even then some areas are still very complicated.

So you have a statistical model of objects rotating ~in a plane towards a centre so that
the system becomes viscous and slows down.

My concern is that you have a particle that has mass, yet the properties that
are being stored are not representative of a particle object but would be an orbit object.
with code like this it is essential to know what represents meaningful elements to your
calculation.

Fully optimised code will not be suitable for another purpose, but minimal overhead is only going
to be a few seconds to allow code to object orientatede and re-usable. There has to be an awareness
that your time in writing, error proofing and maintaining the code can be the most time critical issue.

Now you can iterate over arrays quickly as a[1000] has a pointer 'a' but it can get tricky
to read what exactly you are doing at each step.
It is often beneficial to take advantage of existing solutions and there will be a lot of common tasks
this makes the use of vectors preferable in my opinion.

With vectors they are built for speed so can overflow if you misuse them.
But what you can easily do with vectors is have a test problem with fewer …

tetron 22 Junior Poster

Hi all,

I am just getting into the world of OOP and I'm not totally sure in what situations it's better to use objects and when it's simpler to just revert to arrays.

For scientific code it is normally best to ensure that the code is clearly readable, and reusability is less important than most OOP, but it useful to have both at all times

The first thing you need to do is be sure of what you need for each particle:
mass
position (this might be a coord xyz for a local origin)
speed (coord xyz)
spin
volume

The you might need a time frame handled seperately
update each coordinate check for collisions then decide whether
a particle breaks-up sticks to another particle bounces.

It is important to get the system working before optimisation

Arrays have a short shelf life and can introduce magic numbers if
you have an array of X values and Y values what happens if you miss one on input?

what you can use is a std::vector<particles>
this is #include <vector>

then if you can iterate over the vector in a for loop
getting the particle speeds have
if you put this in a class with methods

add_particle(particle &p);
update_particles();
change_particle(particle & p, int new_state);
advance_time();
you can control the methods:

but if you can be clearer over the design the problem …

tetron 22 Junior Poster

I cannot test any script at the moment as I am in the middle of a long run and looking for thngs to do before it finishes.

The ascii charcter charts show that 130 should be
é so the char(130) is working
the -126

a char is 8 bits and like ints it is possible to treat them as signed numbers so as 256 - 126 = 130
the int to char conversion is not treating the char as a character but as a signed number
char c = 'é';
unsigned int(c);
should be 130
not sure which compiler option has caused this.

but it helps if you output the char next to the number to check that
everything is what you think it is in your code
"/n"
should be replaced by endl; this can be important for later examples.

cout << ">" << i << "< >" <<temp <<endl;
as for the -23 this doesn't look right where are you getting the é
if you copy and paste from word it may be a different symbol to what you think it is! and so it wraps around the 256 possibilities are only one of two bytes it uses.

When you use char(130) you get the é. When you do int('é') you get -23... and with my program to get value it is -126...

How on earth can I get the 130 value …

tetron 22 Junior Poster

2 approaches to consider:

1st assuming that you have a list of questions that are unique
in something like a std::vector<std::string> questions

there are two approaches I would recommend #2

#1
Shuffle the questions so that they are in a random order
You can use a method suggest by the other posters

have an iterator (something to point to the contents)
point to the [0] string of the shuffled questions
std::string<std::vector>::iterator it(questions.begin());

on get_question you can get current_string
*it (to get the value the iterator is pointing to) in this case a string
++it; //move onto next

need to check (it != questions.end())
as this would mean you have run out of questions

#2
leave the questions fixed in order

have an index of ints

int number_of_questions = questions.size();
std::vector<int> index;
for(int i = 0; i < number_of_questions; ++i)
{
index.push_back(i);
}

Now you can shuffle the index to give a shuffled index
then when you want a question get the number

//when a new asker arrives
index = shuffle(index);
std::vector<int> shuffled_index = index;

int ind = 0;
//now each time when you get a question
//check ind ok
if(ind >=0 && ind < shuffled_index.size())
{
int cur_ind = shuffled_index[ind];
//use current ind to get the question
if(cur_ind >= 0 && cur_ind < questions.size())
{
std::string question = questions[cur_ind];
}
//get the next random id
++ind
}
else
{ …
tetron 22 Junior Poster

I have not directly had to change over from c to c++ for a large number of files but there are couple of things to perhaps clarify in your questions.

I think that what you have is effectively a file called "functions.h"
In a functions.c file you have defined several functions:
fun1()
fun2()
...

and what you are trying to do is change the definitions and inner workings of the function but not what the function is doing.

IF you are using a function in lots of places it is safe to assume that the current version of the function performs the task as you want it to. Therefore, it can be used to test that your new c++ version is still working properly. Most C code should run within c++so the old c code can be used as a starting point.

You can either have a version of each include file in a c++ folder and you can keep the name of the file but you temporarily
#include "c++/function.h"
that can be changed to function when all of the files have been converted and then change the folder properties. This works if you can change all of the class in one go.

method 2
If you place all of c++ code into a namespace then you can have
functions with the same name and when you call the c++ version
it can be scoped name::fun1();