Bench 212 Posting Pro

what's the difference between deep copy and shallow copy or not so deep copy :rolleyes:

As far as I understand it, when copying an object containing pointers to dynamically allocated memory, a shallow copy (which occurs by default) only copies the bare pointer members, so that the two objects both refer to the same block(s) of dynamically allocated memory.

On the other hand, take the STL <list> for example - when you create a copy, all the elements pointed by the list's implementation are duplicated, and the new copy won't contain any links to the source from which the copy was made, hence this is a deep copy.

Bench 212 Posting Pro

And besides, doing someone's homework for them isn't actually helping them learn, They're just given the answer with no real understanding gained.

Salem commented: Absolutely! +1
Bench 212 Posting Pro

the game will be easy to create

That simple, huh? I guess you shouldn't need any help then.

Bench 212 Posting Pro

You've only just started C++ and you're trying to implement a doubly linked list? You might want to start out with a singly linked list first... Also, it's hard to see exactly what is supposed to be going on in your code without seeing the definition of Name

#include <iostream.h>
#include <ctype.h>
#include <string.h>

These should be

#include <iostream>
#include <cctype>
#include <string>
main()

to..

int main()
Name * Head, * Tail ;
Name *Head = NULL,
*Tail = NULL;

Error here - you can't redefine names. change to

Name * Head = NULL;
Name * Tail = NULL;
char s1[]="name1";
char s2[]="name2";
char s3[]="name3";

This is OK, but you might be making life difficult for yourself. I recommend you use the C++ std::string instead of C-Style null-terminated char arrays.

std::string s1("James");
std::string s2("Kevin"); //etc

There appears to be a load of other errors too, such as: cout << curr->Name << endl ; but it's impossible to tell what's going on without seeing what Name actually is.

Bench 212 Posting Pro

Actually.. you don't need to pass the array or the size at all - because both data members are stored in your class. You probably want a blank parameter list for the sort function... And since you can work out the size of the array, you shouldn't need to store it as a data member either, you could create a member function to work it out

int Selectionsort::size()
{
   return ( sizeof(number)/sizeof(int) );
}
Bench 212 Posting Pro

Your problem is that you don't have a constructor for the Selectionsort class which accepts the values you're trying to initialise it with. You should try a constructor which accepts a range of values - eg, Selectionsort::Selectionsort(int* begin, int* end) or one which accepts an array, eg, Selectionsort::Selectionsort(int arr[]) Whichever of these you use, I don't think you can use the initialisation syntax as it stands in main() - you would probably need to declare the array seperately first before passing it to the Selectionsort constructor:

int main()
{
   const int foo_size(5);
   int foo[foo_size] = {1,3,5,7,9};
   //Initialisation of Selectionsort object: either
   Selectionsort bar(foo, foo+foo_size);
   // Or...
   Selectionsort bar(foo);
}

A couple of other comments - you don't need to pass the array size to the function, you can work it out using

( sizeof(numbers) / sizeof(int) )

Or you can use a template trick to deduce the size of the array..

template<int N>
sort(int (&numbers)[N])
{  }
Bench 212 Posting Pro

what is the flash input buffer?
if u don't mind 2 explain for me

He said "Flush the input buffer" meaning empty all the data from the input stream.

Bench 212 Posting Pro

How simple? If you look around, there's usually loads of people listing their homework assignments, a few that I can think of from the top of my head:
Vending machine simulator
Student grades database
Calculator
Noughts and crosses game (AKA "tic tac toe" )
Shopping basket/Restaurant menu/other similar "business order" program.

Bench 212 Posting Pro

In main() the program should have a for loop that counts from 0 to 12 (once for each month) and inside the loop ask for the values of rainfall, high_temp and low_temp, then put these values in the array.

I'm sure it was just a typo, but for 12 months, you want to count from 0 to 11 :)

Bench 212 Posting Pro

Yes, I'm sure you'll find many, many people willing to recreate, for free, a game which took a team of professional developers tens of thousands of man hours to produce....

Alternatively, why don't you start out with "Hello, world" and move on from there..

Bench 212 Posting Pro

This seems to me an excersize similar to serialisation. A real linked list would use pointers to connect the "nodes" - if you're using an array to simulate addressable memory, then use an int to contain the array indexes, simulating the pointers. The rest of the program should be fairly similar to a real linked list.

Bench 212 Posting Pro

This looks just like the last question you asked. what don't you understand about it?

Bench 212 Posting Pro
ch2=pow(2,index + 1);

Why did you add 1 to the index? - 32 (2^5) in binary is 00100000 - The code would give your expected result without that.

Bench 212 Posting Pro

I think my main concern would be the ugly mess that you'd get as a result of mixing the two :)

Bench 212 Posting Pro

It sounds like you're trying to do a bit too much at once - start out with getting the program to compile with just one card.. then get it to compile with a deck of cards... then take small steps adding more functionality as yóú go.
Personally i'd change the Card structure. To my mind, the only 2 bits of information a playing card holds are its suit and its rank - a card doesn't hold information about the rest of the deck.

The enums are definitely erroneous, you can't begin an identifier with a number, so you'll need to use ace, two, three, four, five etc, instead.

Bench 212 Posting Pro

What print function are you referring to specifically? do you mean the C function printf()? You shouldn't be using printf with C++ - that's what cout is for.

Bench 212 Posting Pro

What exactly are you going to do with your deck of cards? I presume you'll have some common routines such as "shuffle", "cut deck", etc. but different card games use different rules. A totally generic deck might not be possible, or even desirable. I suggest you decide first which games you want to simulate, then you'll get a much clearer idea of what to do. Not all card games treat the deck as a stack either. some card games treat it more like a queue, others involve a random draw from the middle - keep that in mind when you decide what sort of container to keep the cards in.

Bench 212 Posting Pro

by "Print function" I guess you mean the method to output to the screen or console window? the typical way to output the contents of a 2D array involve a nested loop, eg,

for(int x(0); x!=total_rows; ++x)
{
    for(int y(0); x!=total_columns; ++y)
        std::cout << arrays[x][y];
    std::cout << endl;
}
Bench 212 Posting Pro

C++ has features way beyond most other languages (eg multiple paradigm support in OOP and Parameterised programming), a huge community and wealth of high quality books available, and C++ really has no arbitary limitations in terms of what you can do with it. However, there's no such thing as perfect - every language has its achilles heel IMO.
There are some things where C++ "falls down", to a point. or maybe can be done in other languages much more efficiently. For example, PHP beats most languages hands down for dynamic web content. C++ is also fairly intense compared to other languages (Python for example is regarded as a much friendlier language for beginners).
Also, I believe some of the subtle differences between C and C++ make C a better choice for certain applications involving embedded systems (that's just something i recall reading on comp.lang.c++, so don't quote me on that final point :) )

Bench 212 Posting Pro

There was a thread on another forum a few days ago about Linked Lists, give it a read, because the answers might help you. http://cboard.cprogramming.com/showthread.php?t=78460

Bench 212 Posting Pro

Have a look at this tutorial on functions; http://www.cprogramming.com/tutorial/lesson4.html

Bench 212 Posting Pro

If you're going to carry on using code which someone else has written to base your program on, then make some effort to understand how to use it. Have you even read the errors the compiler gives you for yourself? The errors you've pasted aren't exactly hard to understand.

Bench 212 Posting Pro

I wouldn't call 2 a special case - it fits the general description perfectly; A number evenly divisible only by 1 and itself. :)

Bench 212 Posting Pro

I can see why that is a controversial solution. it would be very easy to mis-read the intention of that code. The least you could do would be to provide brackets around the identifier and post-increment :)
ie

while( *(outstr++) = *(instr++) )
Bench 212 Posting Pro

Something tells me you've found someone else's code, and you're attempting to use it without having a clue what it actually does. I hope this isn't your homework ;)

Studio\MyProjects\delete_v\delete.cpp(670) : error C2660: 'insertArc' : function does not take 1 parameters

The compiler is telling you that you've called the function insertArc() on line 670, but you've passed the wrong number of parameters - you need to pass 1 parameter, or change the function (or overload it) to take the parameters you need.

Studio\MyProjects\delete_v\delete.cpp(681) : error C2664: 'deleteVertex' : cannot convert parameter 1 from 'struct VERTEX' to 'struct ARC'

the compiler is telling you that the first input parameter of the deleteVertex() function must be of the type struct ARC. the object you're passing to it on line 681 is of the type struct VERTEX

C:\Program Files\Microsoft Visual Studio\MyProjects\delete_v\delete.cpp(687) : error C2530: 'graph' : references must be initialized

This one is self explanatory - you declared graph as a reference, but you've left it dangling. The compiler is telling you that references must be initialised - meaning you need to 'point' it to something.

C:\Program Files\Microsoft Visual Studio\MyProjects\delete_v\delete.cpp(708) : error C2065: 'data_v' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\delete_v\delete.cpp(713) : error C2065: 'data_a' : undeclared identifier

You're trying to use the names data_a and data_v without declaring them anywhere... it looks like you had them, and commented them out.

Bench 212 Posting Pro

<offtopic>When I was in school, I was hanging out in the Computer Science Student Lounge when a professor walked in to get a soda out of the vending machine. Then, he exclaimed he had this great idea to give the first year students a project with vending machines and we all got into a discussion with him about it. Thread just brought back memories. Carry on your way.</offtopic>

I'm sure "vending machine" programs are part of the defacto standard set of assignments for School/College/Uni computing students :) One of our 1st year A-level mini projects was very similar in Turbo Pascal

Bench 212 Posting Pro

I get the following error:
error C2451: conditional expression of type 'std::fstream' is illegal

if (outf1)
   {
     char ans;

You can't do that with a std::fstream - however, you can do it with an ofstream or an ifstream. It appears from the variable name, that you're only using outf1 to output data - so change the line

fstream outf1;

to read

ofstream outf1;
Bench 212 Posting Pro

I've read the C FAQ before, admittedly not all the way through, although I can see that I misinterpreted some of the explanation of the subscript operator 'decaying' into a pointer.

Bench 212 Posting Pro
int main()
{
	COseba novi;

	novi.setNumber(5,3);
	novi.Sestej();
	novi.Izpisi();

	COseba();//::COseba();

	novi.Izpisi();

	return 0;
}

a lone call to the constructor won't do anything useful - a constructor call means a new object is created (and in the case above, the object is created, does nothing, then destroyed)

change that line to

novi = COseba();

This calls the constructor to create a new COseba object, and copies the new object to novi

Bench 212 Posting Pro

Ugh! There's no way on earth anyone is going to even try to look at that code - try to format it properly. the very least you can do is not have it all on one continuous line.

Bench 212 Posting Pro

I didn't say they were identical, I said they were equivalents :)

From a data point of view, these are the same

char* x = "hello";
char x[] = "hello";

In the fact that both create an array of char, size 6, and a pointer.

the difference is that the char* x version leaves the pointer variable 'x' non-const (so may be reassigned later), whilst the char x[] version creates 'x' as a const pointer object.

Bench 212 Posting Pro

so how would I set second stream? would i just do another ofstream then the new name
like ifstream fin2?

Yes, that would work.

Bench 212 Posting Pro

ok, i get it, um.. i dont know how to work the whole pointer for array of strings so i just did this

char firstname[SIZE][MAX_LEN], lastname[SIZE][MAX_LEN];

it seems to be working,but how exactly would i use the pointer instead(char* firstname[] etc)? By the way, thanks so much for the help, this program has been driving me nuts:D

The two forms are almost equivalents (With the one caveat that your form explicitly specifies the size of both array 'dimensions') - although the explanation as to why is not quite so trivial ;)

Essentially, a 'C' String is a series of characters linked in contiguous memory (Plus a null terminating character - that's the '\n' thing).. or, as you know it, an array of char.

However, a variable which holds a 'C' String is not an array but a pointer to the first element of that array.

char* myName = "Fred";
char myNickname[] = "Bloggsy";
        // These are equivalent.

With this in mind, a 2D array of characters is not really 2-Dimensional. it is a single array - each element containing a pointer.

So, you have your variable which "contains"(sic) the 2D array (the array of pointers) - but that variable doesn't actually contain an array, it points to the first element.

Hence, the following declarations are equivalent:

char* name[];
char** name;
char name[][];

in every case, the data-type of name is pointer-to-pointer (to char)


Confused? ;)

Bench 212 Posting Pro

Ah, OK - the problem is actually your firstname variable - which is an array of char.

firstname[j] is a single character - the problem is that strcmp is expecting a whole string, ie char* or const char*

did you mean firstname to be an array of strings? or do you intend it to be a single string? if it's a single string, get rid of the [j] at the end

for an array of strings, you need to declare firstname as char* firstname[]

Bench 212 Posting Pro

One of the biggest problems with C++ books older than 1999/2000 is that they do not recognise alot of modern Standard C++ content. A problem commonly found with "revised" books after 1999/2000 (Books which were originally published long before C++ was standardised, but have been updated) is that the Standard C++ content has very much been added as an afterthought, with little or no reflection upon the rest of the book. So many books suffer from this lack of exertion on the part of the author, that these books end up being very unhelpful to someone learning modern C++

C++ really took a whole new direction since the Standard was finalised, and any book which has ignored the progress made by the ISO committee really isn't worth buying for someone who is new to C++.
The comparitively small number of books (sic*) which do follow the direction of C++ are generally found reviewed and rated "Recommended" or "Highly Recommended" at the ACCU website.


*Compared with the vast number of truly awful ones :)

Bench 212 Posting Pro

look up <time.h> - it includes a data type time_t - however, that is only accurate to a second AFAIK. if you need anything more accurate, you'll need to create your own, or find a 3rd party library.

Bench 212 Posting Pro

Not without seeing how emp or firstname are declared. it would appear from the error that emp[a].last is of type int. Can you post more complete code?

Bench 212 Posting Pro

newNumberList is then created with all the elements from numberList(begin) - that is, the first element, up until the position stored by myIter.

Urgh, sorry about that typo - that should have been numberList.begin()

Bench 212 Posting Pro

Can a constructor take more than one argument? If so what's the syntax? Is it something like

Staff(std::string s, double x)

Yes, that's perfectly acceptable. there's no arbitrary limit to the number of arguments you can take - although it becomes somewhat unweildy if you provide too many IMHO. (if you've got 6 or more, your code is probably going to look quite ugly - there's almost certainly a better way)

Also, as with member functions, you may define the body of the constructor outside the class declaration, eg,

class Staff
{
   vector<Employee> members;
public:
   Staff(std::string, double);
   Staff(Employee);
};

Staff::Staff(std::string s, double x)
{
   // do something here
}

Staff::Staff(Employee e)
{
    members.push_back(e);
}

Is there a situation where that would be useful or is it unconventional?

Many situations - you only need to look through the STL to find dozens of examples. The std::vector class has 8 different constructors, some of which take 2, 3 or 4 parameters.

A good example with vectors would be to create a new container based on some elements of an existing container:

#include <vector>
#include <iterator>
#include <algorithm>

int main()
{
    std::vector<int> numberList;
    numberList.push_back(5);
    numberList.push_back(7);
    numberList.push_back(19);

    std::vector<int>::iterator myIter;
    myIter = std::find(numberList.begin(), numberList.end(), 7);

    std::vector<int> newNumberList(numberList.begin(), myIter); 
}

The snippet above creates a vector of int's holding these numbers - 5, 7, and 19
then the find() algorithm searches for the number 7 in the vector.
when it finds 7, it records the position into myIter

newNumberList …

Bench 212 Posting Pro

I'm not trying to be vague. I'm new to programming and don't know all the ins and outs yet. A constructor constructs. That's all I know. As to your original reply, I'm pretty sure I don't want a copy constructor. I think I want to construct a Staff object using an existing vector<Employee>.
Thanks.

When you write a class, C++ already provides you with an invisible default constructor - you don't need to do anything, you don't even need to type it in your code. the constructor will automatically be invoked when you create a staff object, eg,

Staff myStaffList;

Since the only member of your Staff class is a vector<Employee> - a member which doesn't need initialising in order to use it (the vector class itself will have its own constructor which will allow it to be used), you should settle for the default constructor, and concentrate on your member functions for now.

On the other hand, if you later add other values, for example, you might want a string data member to hold the name of your Staff list, you should use a constructor to assign a default value such as "Unnamed Staff List", or a constructor taking a std::string argument to let you specify your own name at the point of construction.
eg,

class Staff
{
    vector<Employee> members;
    std::string listName
public:
    Staff() : listName("Unnamed List");
    Staff(std::string s) : listName(s);
};

This above example uses the initialisation list to assign values (preferred method …

Bench 212 Posting Pro

It's probably to do with the fact that your while loop is reading until EOF is set - in C++ this is the wrong way to do it, because your program will crash reading past the end of openfile (which is actually a stream, not a file). instead, use while (openfile) which will continue as long as there is data in the stream.

Bench 212 Posting Pro

If I was to use && wouldn't that only work if all the information contain in the text file as to be a, b, c, and d for the if statement to run?

No, you're reading the statement wrongly, that "if" statement says
"If input isn't 'a', AND input isn't 'A', AND input isn't 'B', AND input isn't 'b'.... etc"

You could probably cut those conditions down to a small function, if you wanted clearer code

bool isValid(const char& c)
{
    std::string acceptable("abcdABCD");
    if (acceptable.find(c) == acceptable.npos)
        return false;           //npos is the "end of string" token
    else
        return true;
}

Then, later in your code you could say something like

if ( isValid(input_character) && !openfile.fail() )
Bench 212 Posting Pro

OK - you still haven't said anything about what you want your constructor to do, which, I believe is what your original question was about :)

if your vector<Employee> is your only data member, then that doesn't need initialising in order for you to add elements to it. The main purpose of a constructor is to initialise data members and sometimes obtain memory resources for them. It seems from what you've said, that you don't need to do either of those, so you can just make do with the default constructor.

Bench 212 Posting Pro

You can take advantage of the fact that you can rearrange a 3x3 grid so that all horz/vert/diag lines create a sum of 15

2  9  4
  7  5  3
  6  1  8

This gives you a much firmer ground to perform mathematical checks on lines, for both the human player and the AI

Bench 212 Posting Pro

My mind-reading device isn't working today - what are you actually trying to do with the constructor? Construct a Staff object using an existing vector<Employee> ? Construct it using a single Employee object? Make a copy constructor..? Construct it using some other value..?

Bear in mind that the syntax of a constructor is exactly the same as the syntax for any other function.. (Although constructors can have an initialisation list) How would you accomplish what you need with a plain old function?

Bench 212 Posting Pro

Do you mean have 2 different files simultaneously attached to one stream? No, that couldn't possibly work - how would your program know which file to read?

You can, of course close one file, then reuse the same stream to open a different file - but if you need them both at the same time, I'd suggest a second stream object (constantly opening and closing files isn't very efficient)

Bench 212 Posting Pro

The answer is "that depends" - you have to think about how you want to go about making the program - there's no right or wrong answer, because it depends largely on how simple or complicated your program will be. Entities such as Mine or Board are fairly important to a minesweeper game, however, a timer might be an optional addon which you leave til the basic mechanics are working. (obviously you'd leave provision for one to be added).

If you take a look at Windows minesweeper, it's quite a complicated program, with a changing face, different coloured numbers, score board, etc.. but it probably started out as just a board of clickable squares.

It would also be worth noting that OOP is just a tool, so no need to spend ages pouring over class diagrams & relationships - if you find that your program doesn't benefit from OOP, don't use it.

Bench 212 Posting Pro

If you have to use vectors for this (Wouldn't be my first choice, a deque would to allow you to use push_front - eliminating the annoying step of reversing the bit order), rather than vector<int> - you are only holding 1s and 0s, so use vector<bool> - this could simplify your code a fair bit.

You could also reduce alot of repeated code if you used a seperate container for your carrier, rather than a single variable.

this may solve one of the problems with your binAddCal() function, that you are using the carry variable without initialising it. (result - undefined behaviour)


As I mentioned in the other post, there is a much slimmer way to do this using bitwise operators in a recursive function

int binaryAdd(int a, int b)
{
    return (a&b)? binaryAdd( ( (a&b) << 1 ) , a^b ) : a^b;
}

You could emulate this function using STL containers - you'd need to emulate the bitwise operators used: left-shift, XOR and AND.

Bench 212 Posting Pro

an MS-DOS Program runs in DOS, and a Windows program runs in Windows.

which one you create depends on your compiler and/or settings, but from a C++ perspective, there's no difference

Bench 212 Posting Pro

3.

for(int polygon = 0;polygon < 3;polygon++)

This doesn't compile - polygon isn't declared. Try to post what you actually compiled.

was that a slip of the brain? or are you working in C90 mode today? :)