dospy 51 Junior Poster in Training

post your code or provide more details

dospy 51 Junior Poster in Training

your welcome

>> I've been staring at this code for so long that it was starting to not make sense anymore.
don't worry, happened to all of us(i guess), at least to me.
when i got so frustrated about my code failing i just have had a brake for my brain to think logical again; remember it's not good to code when you are angry or under pressure, this way you can't focus

dospy 51 Junior Poster in Training

line 108, again,

while (B[j] < SS)

try replacing with

for(j = 0; j < SS; ++j)

in that while basically you check if B[j](unassigned value) is < 23(pretty slim chances)
so almost none of the array elements get initialized, that's why your program is printing negative values, it just prints garbage

dospy 51 Junior Poster in Training
int a[10];
int* b = a;//b pointing to a[0]
b++;//now b pointing to a[1]
//b[0] == a[1] 
//b[-1] == a[0]
b[-1] = 123;//exact same thing as 'a[0] = 123;'
dospy 51 Junior Poster in Training

are you sure this loop is correct? what is it supposed to do?

for (j = 0; B[j] < SS; ++j)
{
    if (B[j] == B[j+1])
        ++M;
    cout << M << endl;
}

from generatebirthdays function
is this loop supposed to count the matching birthdays?
if yes, this is where your code fails, this loop doesn't look to me like doing something logically

edit: also you needn't declare function Swap, you could have used std::swap(as it does the exactly same thing)

dospy 51 Junior Poster in Training

my guess is that you put the

srand((unsigned int) time (0));

in a while, am i correct?, if yes, just move it first line after main.
remember, srand must be called ONLY ONCE(ofc, excluding the cases when you purposely call it more than one time) for the code to work as expected.
other than that i don't know what the problem could be, if this does not solve your problem, post your whole code so we can have a look at it

dospy 51 Junior Poster in Training

thx

dospy 51 Junior Poster in Training

thx for advice mike; what's the thread library?, i only knew about windows.h threads(the as you say C-style threading)

edit: it is <thread> the library u are talking about? if yes, it seems my compiler does not support it
i am using MSVC 2010 express

dospy 51 Junior Poster in Training

i am planning to start learning how to use threads.
my question is: should i first learn windows.h's functions for threading before boost to get the pattern or should i jump right to the boost threads?

ps: sry if i posted in the wrong section

dospy 51 Junior Poster in Training

i am not sure what's the deal with all this unsigned/signed issues, my guess is that has something to do with the fact that after it reaches it's top/bottom range, a variable starts 'resetting' from the other side(if you understand what i mean)
anyway, if you want a function to check for if a variable is unsigned long(or any other type) you could try this:

template<class T>
inline bool IsUnsigned(T n)
{
    return std::string(typeid(n).name( )) == std::string("unsigned long");
}

note you must

#include <string>
#include <typeinfo> // in my compiler i can skyp this include, so i am not sure it is necessarily

edit: mike just posted a message by the time i was writing mine, you can follow his method since is better(cleaner and less time consuming)

dospy 51 Junior Poster in Training
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main( )
{
ifstream in("abc.txt");
string temp;
vector<string> vect;
while(getline(in,temp))
    vect.push_back(temp);

for(int i = 0; i != vect.size( ); i++)
    CONSOLE_Print(vect[i]);

return 0;
}

this should work, and it's smaller too

edit:
if you want to skip blank lines replace

while(getline(in,temp))
    vect.push_back(temp);

with

while(getline(in,temp))
{
    if(temp.empty( ))    
    vect.push_back(temp);
}
dospy 51 Junior Poster in Training

try this

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main( )
{
char* test;
cin.getline(test, 256, '\n');// note: use cin.getline( ) for getting string inputs
string Stest(test);
ofstream myfile(Stest.c_str( ));//myfile.open( ) does not create new file
myfile << "Test\n";
myfile.flush( );//needed for the file to be created
myfile.close( );

return 0;
}
dospy 51 Junior Poster in Training

thx for the info vijayan121,
i also tried the fourth way and it doesn't work, it says 'for_each' is undefined.

dospy 51 Junior Poster in Training

wow, i didn't know gcc is known as mingw in windows, actually, i worked in mingv at school but i don't like it because it's interface is very poor designed, also i can hardly understand what errors and warnings are about there. so far, MSVC seems better from my point of view; tho thx for the effort, i really appreciate
ps: by "i don't have much experience" i meant that i don't have much experience of how the compiler works (linkage, transforming .cpp into .obj etc); right now i am focusing on soft desing coding practice and c++ knowledge(i've been since i started learning c++), but i don't feel so good when i have to deal with such things like linkage, debugging(although i know to insert breakpoints and such), i usually need to master thing to feel comfortable...

dospy 51 Junior Poster in Training

yes, i have never considered optimizating(because i don't know how, so far, all i know is to code, i don't have much experience with compiler activities) and yes i am using MSVC since it was my very first complier used and i got confortable with. i might consider changing it tho. is GCC free? if yes, can you give me a direct link to it?
thx in advance

dospy 51 Junior Poster in Training

first off, sry for double post.

now, i've done a bit of testing with the 2 methods to see with one is faster, i'll let you see for yourself

//util.h

inline void CONSOLE_Print(const string& message)
{
	cout << message << endl;
}

// time

inline uint32_t GetTime( )
{
	return clock( ) / 1000;
}

inline uint32_t GetTicks( )
{
	return clock( );
}
#include <conio.h>
#include <Windows.h>
#include "util.h"

//#define ARGS

int main(int argc, char* argv[])
{
#ifdef ARGS
	for(uint32_t i = 0; i != argc; i++)
		CONSOLE_Print(argv[i]);
#endif
	uint32_t StartTime;
	MILLISLEEP(1500);
	
	CONSOLE_Print("Started initializing an empty vector of 100.000.000 ints");
	StartTime = GetTicks( );
	vector<int> vect(100000000, 0);
	CONSOLE_Print("Time: " + ToString(GetTicks( ) - StartTime) + " milliseconds");
	StartTime = GetTicks( );

	CONSOLE_Print("Started looping a vector of 100.000.000 ints(using '*it')");
	StartTime = GetTicks( );
	for(auto it = vect.begin( ); it != vect.end( ); it++)
	{
		*it = 1;
	}
	CONSOLE_Print("Time: " + ToString(GetTicks( ) - StartTime) + " milliseconds");
	StartTime = GetTicks( );

	CONSOLE_Print("Started looping a vector of 100.000.000 ints(using 'vect[it]')");
	StartTime = GetTicks( );
	for(int it = 0; it != vect.size( ); it++)
	{
		vect[it] = 1;
	}
	CONSOLE_Print("Time: " + ToString(GetTicks( ) - StartTime) + " milliseconds");
	StartTime = GetTicks( );

	getch( );
	return 0;
}

output:
http://www.picz.ro/show-image.php?id=40a3333d734f08e754162bc11c900ad4

it seems to me like the index method is both cleaner and faster, so what does this safety-nes consist in?

EDIT:

The main advantage is that iterator code works for all stl …

dospy 51 Junior Poster in Training

thx, it seems that the rule 'never add a function body into a header file, just the prototype' can't be always respected. i tried to avoid this but since it's the only way, i'll do it.
also i didn't know you could include a cpp file to a header/another cpp file(never seen it used also), thx for the info
thx for your replies guys

dospy 51 Junior Poster in Training

how exactly is it more safer to use iterators than indexes?
at this point,

vector<int> myvec; // let's assume it's initialized
for(auto it = myvec.begin( ); it != myvec.end( ); ++it)
{
    // code
    // access by '*it'
}

this seems the best one to me..(if iterators are really one step ahead indexes)

ps: thx mike, i didn't know auto could be used this way, i thought that keyword is useless

dospy 51 Junior Poster in Training

there are 2 common ways to loop through a vector

1:

vector<int> myvec; // let's assume it's initialized
for(int i = 0; i != myvec.size( ); i++)
{
    // code
    // access by 'myvec[i]'
}

2:

vector<int> myvec; // let's assume it's initialized
for(vector<int> :: iterator i = myvec.begin( ); i != myvec.end( ); i++)
{
    // code
    // access by '*i'
}

what's the difference between the 2 methods, in what concerns me the first one is far more convenient since is cleaner and easier to use(no overhead with the '*', witch is also confusing sometimes, typically when u store pointers in your vector);
but in most of cases i've seen people using the second method. why?

dospy 51 Junior Poster in Training

basically i have a header full of function prototypes(util.h) and a cpp file with the functions body

// util.h
#include <ctime>

uint32_t GetTime( );
uint32_t GetTicks( );

// util.cpp
#include <util.h>

uint32_t GetTime( )
{
    return GetTicks( ) / 1000;
}

uint32_t GetTicks( )
{
    return clock( );
}

// main.cpp
#include <conio.h>
#include <iostream>
#include "util.h"

int main(int argc, char* argv[])
{
    uint32_t StartTime = GetTicks( );
    // 
    // some code here
    //
    std :: cout << GetTicks( ) - StartTime << " milliseconds passed since the program started" << std :: endl;
    getch( );
    return 0;
}

because i'll use GetTicks( ) and GetTime( ) many times in my code and the function body is really small i'd like to make them inline, but the linker fails if i try to do this

// util.h
#include <ctime>

inline uint32_t GetTime( );
inline uint32_t GetTicks( );

// util.cpp
#include <util.h>

inline uint32_t GetTime( )
{
    return GetTicks( ) / 1000;
}

inline uint32_t GetTicks( )
{
    return clock( );
}

or this:

// util.h
#include <ctime>

inline uint32_t GetTime( );
inline uint32_t GetTicks( );

// util.cpp
#include <util.h>

uint32_t GetTime( )
{
    return GetTicks( ) / 1000;
}

uint32_t GetTicks( )
{
    return clock( );
}

or this:

// util.h
#include <ctime>

uint32_t GetTime( );
uint32_t GetTicks( );

// util.cpp
#include <util.h>

inline uint32_t GetTime( )
{
    return GetTicks( ) / 1000;
}

inline uint32_t GetTicks( )
{
    return clock( );
}

any of these …

dospy 51 Junior Poster in Training

you could use a void pointer(ofc you'll have to add more interface to your class to eliminate error prone as void pointers are very dangerous)

dospy 51 Junior Poster in Training

np, glad to help, happy coding ^^

dospy 51 Junior Poster in Training

thx, for answers, i got it now, no need for other explanations
ps: i am using windows 7 / MSVC 2010

dospy 51 Junior Poster in Training

it seems that you misunderstood what your teacher asked you to do..

dospy 51 Junior Poster in Training

I wouldn't go with the microsoft products because they are too resource heavy, and are too complex for simple things.

the more complex, the more flexible
IMO msvc is a very good compiler since it has a lot of interface facilities, as well as a lot of flexibility

dospy 51 Junior Poster in Training

yea, the problem is i doubt that's what they wanted, but we can't really help much unless they give us really clean explanation of it

EDIT: now i figured out what they'r problem is, they want to find out the address of a structure by knowing the address of one of it's members
for ex we have

struct
{
    int _a; // let's assume that this int's address is 0x02F48C
} Check;

// how do we find out the address of Check without using '&Check'?

i've heard of something like structures have an unusual way to store variables in memory(one right after another, just like an array), but i never really knew it for sure and i haven't even found a tutorial on this(although i'd like to find out about this thing).

sry guys, i can't help you here...
wait for more experienced programmers to answer your questions since it beats me

dospy 51 Junior Poster in Training

that's not the name of the structure, it's an instance of and unnamed structure
basically when you declare a structure without naming it, you are not allowed to create any instances of it, thus the only way to create and instance is by putting names(separated by commas) after the struct declaration AND before the semicolon.
so,

struct{
int a; // note semicolon after declaration, not comma
int b;
char c;
} Check; // note here; it could be Check, Check2, Check3 etc before the semicolon

taking the address is rather simple from now; ex:

#include <iostream>

struct{
int a; // note semicolon after declaration, not comma
int b;
char c;
} Check;

int main( )
{
    std::cout << "Address of the unnamed structure instance named 'Check': " << &Check;
    return 0;
}

so, if you need to pass the address to the function, just use '&Check';

Fbody commented: Someone finally noticed. +13
dospy 51 Junior Poster in Training

i figured it out thx to you Narue, i appreciate your effort, now, one more last thing, is there any other way to specify the command line arguments than by running the program through the cmd followed by "$ ....."?

dospy 51 Junior Poster in Training

And just a precision on dospy's statement. You are allowed to write and do variables[0] = 1; , but the first element (accessed by index 0) must already exist in the vector before you do this.

that's what i meant by saying you cannot initialize a vector this way, did not mention that you can't modify or access them after initialization.
sry for being ambiguous tho :D

dospy 51 Junior Poster in Training

you basically can't 'find out the address' of something that does not exist, you need an instance of the given class/struct to take out it's address
when you say, for example

struct MyStruct
{
    int someVarName;
};

you don't declare anything(you just Define), thus you can't take it's address unless you do what Moschops showed you.

dospy 51 Junior Poster in Training

as a little piece of advice: next time you are dealing with containers/typed/functions etc defined in a header you could try this site, it's very helpful
http://www.cplusplus.com/reference/
this might help you if you're still having some trouble using 'list'
http://www.cplusplus.com/reference/stl/list/

dospy 51 Junior Poster in Training

more irritant thing is that what you're asking has less matter with c++ and more with logic, you should really try harder, it's not so complicated

Ancient Dragon commented: agree +17
dospy 51 Junior Poster in Training

'Thinking in C++' is a perfect book for beginners and it also explains most of OOP aspects well

dospy 51 Junior Poster in Training

i think the problem is your project properties
try this:
File->New Project->Win 32 Console App
now, a window will pop up; DO NOT PRESS FINISH
Click next, make sure that the application type is 'Console application' and uncheck the 'Precompiled heather' box and check 'empty project' box

everything should be working now

dospy 51 Junior Poster in Training

push_back() is a function that pushes an element into the vector

std::vector<int> variables; // this creates an empty vector of int variables
variables.push_back(1); // is the same thing as variables[0] = 1, except you are not allowed to set values using "variables[0] = 1"; if you do, your program will crash
variables.push_back(1000); // this will set the second element in the array to 1000( variables[1] = 1000 )
// and so on...
dospy 51 Junior Poster in Training

i understand how to use command line arguments, i only wanted to know how to specify them;
in that tutorial it says somewhere "Now run the program, adding command line parameters:"
how exactlty you do that?

dospy 51 Junior Poster in Training

thx for posting all
could someone tell me how to change the command line arguments? or post a link/tutorial?
thx in advance

dospy 51 Junior Poster in Training

i've never knew how to use command line arguments since the first source code i used to edit(and from i learned basics of c++) was using a config file to 'read' user input data on program startup and not command line arguments.

now my question, what's the difference between using command line arguments and creating a special designed class to read a config file (config.cfg) looking like this:

path = C:\ProgramFiles:\MyProg
logfile = log.txt
savegamepath = C:\ProgramFiles:\MyProg\save
// etc..

i know that on the first thought, the cfg option is far more complicated and timeconsuming, but almost every program needs a config file to read the default values for the data it needs

i ask this cuz i'm not sure if it's worth learning to use command line arguments or not

dospy 51 Junior Poster in Training

>>you want to rename main.h to myclass.h so that the name of the file tells you what's in the file
i always do this, but this was just an example and no other name came though my mind :D

thx again to all of you for a clear explanation
oh, and one more question, how do i mark a post as Solved?

dospy 51 Junior Poster in Training

what's the difference between:

// main.h
#ifndef X_H
#define X_H

#include "point.h"    // supposing that point.h contains the declaration of Point class

class MyClass
{
private:
Point m_Point;

//...
};
#endif

// main.cpp

#include "main.h"
//...

and

// main.h
#ifndef X_H
#define X_H

class Point;

class MyClass
{
private:
Point m_Point;

//...
};
#endif

// main.cpp

#include "main.h"
#include "point.h"
//...

i mean difference in performance, manners of writing and so on.., i already know that the first case is used to avoid circular referencing when used with pointers/references

dospy 51 Junior Poster in Training

i understand, thank you very much both of you

dospy 51 Junior Poster in Training

thx for reply, but to be sure, is that an answer to the question "why to create functions to access data members?" or to "why would i declare a data member private/protected if i need to access it?"

dospy 51 Junior Poster in Training

i've seen in some sources that some classes have Getters and Setters even for public data members

class Example
{
private: 
int m_ID;

public:
string m_Name;
int m_Age;

Example( );
~Example( );

void SetName( string nName )    { m_Name = nName; }
void SetId( int nID )           { m_ID = nID; }
void SetAge( int nAge )         { m_Age = nAge; }

int GetID( )                    { return m_ID; }
int GetAge( )                   { return m_Age; }
string GetName( )               { return m_Name; }
};

well, i see relevance in creating functions to access private data( m_ID in this case ), but i don't know why would someone loose time for declaring fuctions for public data members

and also, isn't faster to access direclty the member rather than through a function?

Example ex;
ex.m_Age = 10;

vs

Example ex;
ex.SetAge( 10 );

i suppose that the first example is faster since it doesn't call a function?..

and last question, why would i declare a data member private/protected if i need to access it?, making me have to write extra code for getters and setters; the only benefit i can see is that you cannot declare a pointer or a reference to the specific data member

dospy 51 Junior Poster in Training

i think your problem is that your header files does not have header guards: see this

dospy 51 Junior Poster in Training

that was what i needed to know.
also the additional information that you both gave me is useful
thx you very much

dospy 51 Junior Poster in Training

Hey, it's cool you hung around for so long and remembered us when you did need help. Nice one. Welcome again.

heh, actually i wanted to create a new account on a C++ forum because i am willful to learn as many new things as i can, but the name 'dospy' was already taken. so, i asked myself "who would use the 'dospy' name?"(because this name is not popular at all) and then i just thought that i might have created an account a few years ago (i am lucky that i have the same password to all my IDs/Accounts :D)

but thx anyway, i feel very good here and i hope that i can help you guys too

dospy 51 Junior Poster in Training

i know that define redirectives only replace the defined object with the text after

------------------------------------------------------------------------------------
what i want to ask is
when we check for some flags like in this line: if( flags & FLAG_BIG ),
the compiler transforms that code into this: if( flags & 1 << 2 )
heh, would be this calculeted as 'if( (flags & 1) << 2 )', or as 'if( flags & (1 << 2) )'?

i've noticed that the compiler treats it as if( flags & (1 << 2) ) and i don't understand why. does the '<<' and '>>' operators have a bigger precedence than '&' operator?
---------------------------------------------------------------------------------
^
that is the part that i don't understand -|

dospy 51 Junior Poster in Training

recently i understood at last how can i store more values in an int with bytwise operators,
also, i have read that #define directives are evil for a number of reasons.
one of them:

#define SEVEN 2 + 5

int result = SEVEN * 5;
cout << result;    // you would expect that the console prints 35, but it doesn't
/*
let's take a look why this happens:
the folowing line: int result = SEVEN * 5;
it is converted by the compiler to this:
int result = 2 + 5 * 5;    // this will printout 27, not 35 as expected
so, if you'd want a correct answer, you'd have to put 2 + 5 into brackets at the define redirective like this: #define SEVEN (2 + 5)
*/

usually, bytwise operators are used to store different values into one single variable
here is an example where we use an int to denote some flags:

// random flags, the meanning does not matter
#define FLAG_FAST         1 << 0
#define FLAG_VISIBILE     1 << 1
#define FLAG_BIG          1 << 2

// ... etc

// function to set some flags to an integer
void SetFlags( int &flags )
{
    if( ... )
        flags |= FLAG_FAST;
    if( ... )
        flags |= FLAG_BIG;
    if( ... )
        flags |= FLAG_VISIBLE;
}

int flags = 0;
SetFlags( flags );

// now we want to use our flags
if( flags & FLAG_BIG )
{
// do stuff
}
// ... etc

what …

dospy 51 Junior Poster in Training

The more typical way to handle these situations is to have a "finalize" function in the SecClass that the MainClass can call before "really" destroying itself. This way, the responsibility is on the MainClass to make sure that "finalize" is called when the MainClass is still fully-functional. Then, the destructor of SecClass no longer has to do this trickery (and, this also works whether the SecClass object is held by pointer or by value in the MainClass).

thx, i'll take into account your advice

dospy 51 Junior Poster in Training

The general rule is that the lifetime of an object ends as soon as the body of the destructor is entered. This would normally mean your code is "illegal". However, it technically isn't entirely. You should still be able to call member functions or access POD data members fairly safely (it is against good coding practices, but it shouldn't crash).

The only reason that I see why it would crash is if: you call a virtual function of the MainClass. Then, it should crash. If you call a virtual function, you should know that the virtual table of the object is no longer valid when you enter the body of the destructor and you will basically be dereferencing a null-pointer.

there is no virtual fuction in the MainClass, but thx for that notification, i didn't know that virtual function/object are no longer valid after calling the destructor.

I think you need to include what your 4 standard methods do for each of your classes in your post. For example I believe that SecClass should use flat-versions, i.e. do not any deletes for the mainClass whereas MainClass should delete, and deep-copy the SecClass members, but we need to see what you are doing there to help you.

no, the SecClass is accessing just a normal fuction of the MainClass, it does not delete any of the object of it either.

the source code is not private, i am just editting an open source program(because i use that program and …