JoBe 36 Posting Pro in Training

Hello ladies and gents,

Have a few questions about the following example that is given:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	char ch;
	int n=0;
	ifstream ff("aa");

	if (!ff) 
	{
		cout << "Can't open file aa for input.\n";

		return 1;
	}

	ofstream gg("bb");

	if (!gg) 
	{
		cout << "Can't open file bb for output.\n";

		return 1;
	}

	// Copying starts here:
	while (ff.get(ch)) 
	{
		gg.put(ch); 
		n++;
	}

	cout << n << " characters copied.\n";

	return 0;
}

When talking about "can't open file aa for input". Could this be for example a Word document? Is that what 'a file' could point to???

Also,

ifstream ff("aa");

Is it the intention to put an actual link towards an existing file like for example C:/MyPrograms/Microsoft Visual/ ... within this part (" ") If this type of code would be used in a real example :?:

JoBe 36 Posting Pro in Training

Hello ladies and gents,

In my book, I'm currently reading about the use of creating your own operators << and >> for in- &output.

There's been given a small example that goes like this:

#include <iostream>

using namespace std;

class vec 
{ 
public:
   vec(float x1=0, float y1=0) {x = x1; y = y1;}

   vec operator+(const vec &b) const
   {  
	   return vec(x + b.x, y + b.y);
   }

   float x, y;
};

ostream &operator<<(ostream &os, const vec &v)
{  
	return os << v.x << "   " << v.y << endl;
}

istream &operator>>(istream &is, vec &v)
{  
	float x, y;

	is >> x >> y;
	v = vec(x, y);

	return is;
}

int main()
{  
	vec u, v, s;
	cout << "Typ twee getallenparen:\n";
	cin >> u >> v;
	s = u + v;    // Vectoroptelling 
	cout << "De som bij vectoroptelling is:\n"
		 << s;

   return 0;
}

But, what is actually the benefit of writing it this way, when you could write it like this aswell?

#include <iostream>

using namespace std;

class vec 
{ 
public:
	vec(float x1=0, float y1=0) {x = x1; y = y1;}

	void printvec() const
	{
		cout << x << "  " << y << endl;
	}

	vec operator+(const vec &b) const
	{
		return vec(x + b.x, y + b.y);
	}

private:
   float x, y;
};

int main()
{  
	vec u(3, 1), v(1, 2), s;

	s = u + v;    // Vectoroptelling!

	s.printvec(); // Uitvoer: 4  3

   return 0;
}

Could someone explain …

JoBe 36 Posting Pro in Training

I think I got the solution to define, initialize and delete the two dimensional array with pointers in C++ Narue.

#include <iostream>

using namespace std;

int main ()
{
	int **p;
	int i, j;

	p = new int*[2];

	for ( i = 0; i < 2; i++ ) 
	{
		p[i] = new int[3];
		for ( j = 0; j < 3; j++ )
			*( *( p + i ) + j ) = i * j;
	}

	for ( i = 0; i < 2; i++ )
	{
		for ( j = 0; j < 3; j++ )
		             cout<< p[i][j];
	cout<<endl;
	}

	for (i = 0; i < 2; i++)
		delete [] p[i];

	delete [] p;

	return 0;
}

Correct :?:

Also, to free the memory in the double array example written in C, is this correct then:

free (base);
free (p);
JoBe 36 Posting Pro in Training

Hey Narue,

Your tutorial on 'Pointers and dynamic memory'.

I executed your example in wich you use this piece of code

char *p = malloc ( sizeof "This is a test" );

Since I wanted to see how I had to write this in C++, I tried it out this way

char *p = new char[sizeof "This is a test"];

Though it worked, I wanted to ask wether, this is the correct way?

Additional questions:
C code in your example:

base = malloc ( 2 * 3 * sizeof *base );
  p = malloc ( 2 * sizeof *p );

Altering this into C++ code, is this correct?

base = new int[2];
  p = new int*[3];

:?:

Also, how do I delete this double array?

Like this:

delete []base;

  for (i = 0; i < 3; i++)
	  delete [i] p;
JoBe 36 Posting Pro in Training

Hey Narue,

Your tutorial on 'Pointers and dynamic memory'.

I executed your example in wich you use this piece of code

char *p = malloc ( sizeof "This is a test" );

Since I wanted to see how I had to write this in C++, I tried it out this way

char *p = new char[sizeof "This is a test"];

Though it worked, I wanted to ask wether, this is the correct way?

JoBe 36 Posting Pro in Training

... i am doing the program and asking help from fellow C++ geniuses.

:lol: From fellow C++ geniuses hey, well, there certainly are several hanging around here, but seeing as you even use void main(), I'm certain your not one of them :lol:

Don't know about you, but saying to someone with the knowledge and willingness to help anyone out who shows effort around here to zip it, now, I think that is really stupid don't you think ;)

JoBe 36 Posting Pro in Training

Thanks Narue :!:

JoBe 36 Posting Pro in Training

Hi Narue,

Ive tried to alter your example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *reverse ( const char *s )
{
  int i = 0, j = strlen ( s );
  char *save = malloc ( j + 1 );

  if ( save == NULL )
    return NULL;

  while ( j > 0 )
    save[i++] = s[--j];
  save[i] = '\0';
  
  return save;
}

int main ( void )
{
  char *p;

  p = reverse ( "J. Random Guy" );
  puts ( p );
  free ( p );

  return 0;
}

into this:

#include <iostream>

using namespace std;

char *reverse ( const char *s )
{
  int i = 0, j = strlen ( s );
  char *save = new (char) ( j + 1 );

  if ( save == NULL )
    return NULL;

  while ( j > 0 )
    save[i++] = s[--j];
  save[i] = '\0';
  
  return save;
}

int main ( void )
{
  char *p;

  p = reverse ( "J. Random Guy" );

  cout<< p <<endl;

  delete p;

  return 0;
}

I'm getting an error though wich seems to be related towards deleting p :?:

What am I doing wrong there?

Could you tell me also how I can cast the C example

char *save = malloc ( j + 1 );

so I can execute your example.

Thanks.

JoBe 36 Posting Pro in Training

Ok, thanks Narue :!:

Ive got a few more if you don't mind ;)

Ive read the part about incrementing and subtracting pointers, but I don't see the difference in the examples you show for those two, they're actually both the same :confused:
Also, I don't understand what the idea of these are, meaning, when would you use them?

The example:

#include <stdio.h>

int main ( void )
{
  int a[] = {1,2,3,4,5};
  int *p = a, *q = a + 5;

  printf ( "%td\n", p - q );
  printf ( "%td\n", q - p );

  return 0;
}

Also, when I execute the two examples for incrementing and subtracting, they both give me 'td' as result :confused:

But, when executing the one in wich you cast the result to (long) (atleast I think it's a cast :?: ) I get a result of -5 and 5. The same result is shown when I use cout?

Is this again related towards being C code?

The results of -5 and 5, are those the amount of pointers that have been used, or what are they?

Thanks for the help and explanation!

JoBe 36 Posting Pro in Training

Hi Narue,

Ive started your tutorial on pointers and had a few questions about it if you don't mind.

1) You talked about the fact that a function has an adres itself and that you can call a function threw it's adres. This I understand, as I tried it out wether I could print the adres of a function a did this as follow:

void function(int, int)
{
...
}

int main()
{
...
cout<< function <<endl;
...
}

This worked as it should, thing is, I tried this out aswell with &function and *function and those two gave me the exact adres.

Question is, are all three correct possibilities to obtain the functions adres?

2) I tried out the example you showed about pointers to functions and had a problem that I got two errors, they stated the following:

C:\Program Files\Microsoft Visual Studio\MyProjects\VoorbeeldenInternet\PointersTutorialByNarue\Testing.cpp(39) : error C2440: 'initializing' : cannot convert from 'const void *' to 'const int *'
        Conversion from 'void*' to pointer to non-'void' requires an explicit cast

wich is related to this piece of code:

int compare ( const void *a, const void *b )
{
  const int *ia = a;
  const int *ib = b;
  ...
}

Now, I understand what the error message is saying and I changed the code so that the parameter const void *a, get's cast like this:

const int *ia = static_cast <const int*> (a);

This way it works, but, I kept wondering …

JoBe 36 Posting Pro in Training

Maybe better would be to take a look at the list of books Dave Sinkula has made:

With regard to C++ books, I'll just echo the advice here.


The following books are recommended; read them in mostly the order listed.
"Accelerated C++" Andrew Koenig & Barbara Moo
"The C++ Standard Library" Nicolai Josuttis --- a "must have"
"Effective C++", "More Effective C++", "Effective STL" Scott Meyers
"Exceptional C++", "More Exceptional C++" Herb Sutter
"The C++ Programming Language" 3rd edition or later Bjarne Stroustrup
"Modern C++ Design" Andrei Alexandrescu
"C++ Templates" Vandevoorde & Josuttis
"Standard C++ IOStreams and Locales" Langer & Kreft

Link towards Dave's thread with direct links to Amazon can be found here: Dave's List (Click Here)

JoBe 36 Posting Pro in Training

Hi cpp noob,

You know, no offence ment, but maybe you'd better learn how to program correctly before you start tackling game programming.

Though I'm no programmer and I'm still learning it myself, I can see, you have little or no experience.

You didn't use switches to simplify the several selections you made :confused: You didn't use any functions in wich several pieces of code could have been written, thus simplifying your code straight away.

Don't take this the wrong way, but, you'd be doing yourself a big favor when you would start and learn the syntax and simple exercises from books, when you master those and have a good understanding of that. Then you could try to write a game ;)

Hope you don't take this the wrong way ok ;)

JoBe 36 Posting Pro in Training

@ Dave, thanks for the tip on that, I think it's important not only to get your code working, but improve it where possible :!:

@ Rashakil Fol, now I know why you didn't need to use the -1, you used

--i

and I used

i--

So, you subtracted 1 from i before you entered the loop.

JoBe 36 Posting Pro in Training

There's no '\0' character in the string. i starts after the end of the string because --i returns the decremented value of i, meaning that the first comparison uses the last character and first character of the respective strings. (Whereas i-- would return the original value of i.)

Unless there's a bug. I have not compiled and run.

Euh, yes I ment '\0'

Hmm strange :confused: But, If I don't use it, it doesn't show the correct result :!:

JoBe 36 Posting Pro in Training

Is this right?

No, it's a typo :)

if (name.size() != name2.size()) 
        {
	return false;
        }

Can you tell me why you prefer to use .size instead of .length?

while (i)
         {
	if (name[--i] != name2[j++])
            ...

Man, this is a very very good tip, never thought about using a while loop and didn't even think that during the selection you could compare name with name2 by subtracting i and adding towards j during the while loop.

Also, string::size_type is an integer type that is guaranteed to be larger than any stored string.

Don't completly understand why you prefer to use this, but, I'll google to see what string::size_type just is :?:

string::size_type i = name.size()-1;

Has to be -1, otherwise your comparing '\n' ;)

Thanks for the very usefull tips and improvements on my code Rashakil Fol :!:

JoBe 36 Posting Pro in Training

Hello ladies and gents,

I had to do an exercise in wich I was required to compair two standaardtype strings opposits, meaning: name = "Johan", name2 = "nahoJ"

The idea was to return a bool value True or False. I did this with the following code I wrote:

#include <iostream>
#include <string>

using namespace std;

bool compair(const string &name, const string &name2)
{
	int i, j;

	if (name.length() != name2.length())return false;

	for(i = name.length() - 1, j = 0;i >= 0 ;i--, j++)
		{if (name[i] != 0 name[j]) return false;}

	return true;
}

int main()
{
	string name = "Johan", name2 = "nahoJ";

	int c = compair(name, name2);

	if(c == 1)
		cout<<"True!"<<endl;
		else
		cout<<"False!"<<endl;

	cout<<"Press any key to continue!\n";cin.get();

	return 0;
}

Alltough I managed to get 'a' solution, I was wondering if any of you could tell me how I could improve/shorten or make the code more clearly to follow :?:

The idea is solely to try and code a program as best as possible.

Thanks for any assistance ;)

JoBe 36 Posting Pro in Training

Ok, thanks for the explanation Narue.

JoBe 36 Posting Pro in Training

... Since you haven't created an array of characters for hulp to point to, when you copy to whatever hulp is pointing to, you're writing to write to random memory, causing the error.

Bingo, :D

char *hulp;
hulp = new char [100];

Am I correct that it is best to write

delete[] hulp;

when I don't need that pointer to an array of characters anymore :?:

Also, could you give me a clue as to wether I'm correct in saying this:

...the way "char* &p" is written, that this is because the parameters of sort2 are array's of pointers

If so, could they have been written differently, for instance like this:
"char *p[]"

Thanks for the help Rashakil Fol :!:

JoBe 36 Posting Pro in Training

Hello ladies and gents,

Came across a program example in wich you are able to sort program-parameters in alphabetical order. The code is this:

#include <iostream>
#include <string>

using namespace std;

void sort2(char* &p, char* &q)
{
	if(strcmp(p, q)> 0)
	{
		char *h;
		h = p; p = q; q = h;
	}
}

int main(int argc, char *argv[])
{
	int i;

	cout<< "argc   = "<< argc <<endl;

	if(argc == 4)
	{
		sort2(argv[1], argv[2]);
		sort2(argv[2], argv[3]);
		sort2(argv[1], argv[3]);

		for (i = 1; i < argc; i++)
		cout<<"argv["<< i <<"] = "<< argv[i] <<endl;
	}
	else
		cout<<"Give three program-parameters!\n";

	cout<<"Press any key to continue!\n";cin.get();

	return 0;
}

My question about this code is this part:

void sort2(char* &p, char* &q)

Am I correct that the way "char* &p" is written, that this is because the parameters of sort2 are array's of pointers:?:

If so, could they have been written differently, for instance like this:
"char *p[]" :?:

Also, I changed the sorting from this:

char *h;
		h = p; p = q; q = h;

into this:

char *hulp;

		strcpy(hulp, p);
		strcpy(p, q);
		strcpy(q, hulp);

I just wanted to see wether with the use of strcpy I could make this work aswell, apparantly not :confused:

Though not getting error messages when debugging, I do have one warning in that local variable "hulp" isn't initialized.

When executing the program, I get a message saying that there was an error during the execution …

JoBe 36 Posting Pro in Training

Hi,

You have made a declaration of your pointer *b, but where is the definition of it :?:

A pointer is an adress and must be assigned towards one, meaning, you'll need to use 'new' to assign an adress towards b.

Hope this helps.

JoBe 36 Posting Pro in Training

I understand, neither is trying to explain something what's wrong without being able to show it.

But like I said, with what you told me then showing me how you did it with the picture, I just deleted the previous one and started again.

I didn't compile the headerfile vkv.h and after creating the other source files and compiling them, the headerfile automatically was added towards the program :D

Thing with these items is, that once you have seen how it is supposed to be done, it isn't difficult at all, but, problem is, you have to be able to let it succeed ONE TIME :lol:

JoBe 36 Posting Pro in Training

Yep, got it Dave:

[IMG]http://img252.imageshack.us/img252/9535/compilingtwo5da.th.jpg[/IMG]

With your explanation and example I managed to do it :)

Thanks very much for the help, really appreciated it :!:

Thanks to ImageShack for Free Image Hosting

JoBe 36 Posting Pro in Training

>> The popup seems to indicate that you were attempting to compile a header. You don't click on 'compile' for a header.

Then when I create that Header file vkv.h, I just write the code and save it as a Header File?

Also, you have a map called 'External Dependencies', instead of putting the Header file in there, can I just put it in the map 'Header files'?

I notice that in your Source Files for Sample PP, there are two .cpp files, main.cpp and vkv.cpp, did you add these to the vkv.h header file?

Did you add any files towards the main.cpp or/and vkv.cpp?

I have deleted the different programs and will make a new attempt tommorow, I printed you're example so I have an idea what it should look like ;)

Thanks for your patience Dave, very much appreciated :!:

JoBe 36 Posting Pro in Training
JoBe 36 Posting Pro in Training

It is explained in this book that you take the name of the class, in this case vkv and make this into part two and three a headerfile by writing it as following:

#include "vkv.h"

All the names in the map are as follows:

LATestVoorbeeldenBoek.cpp type: C++ Source file
LATestVoorbeeldenBoek.dsp type: Project file
LATestVoorbeeldenBoek.dsw type: Project Workspace
LATestVoorbeeldenBoek.opt type: OPT-file
LAT....

I added to this map the following file:

vkv.h wich is of type: C Header file

Hope this gives you a clue as to what I'm doing wrong?

JoBe 36 Posting Pro in Training

>>Is what you posted earlier currently the exact contents of the filename shown in the error message?

Yes.

>>And you don't have a full path in the #include, do you?

I just typed below the #include <cmath> in part two and #include <iostream> in part three the #include "vkv.h" line.

These are the programs now:

// LATestVoorbeeldenBoek.cpp:

class vkv
{
public:

	void coeff (double aa, double bb, double cc);

	bool losOp();

	double wortel1()const {return x1;}
	double wortel2()const {return x2;}

private:

	double a, b, c, x1, x2;
};
// LATestVoorbeeldenBoekVervolg.cpp:

#include <cmath>
#include "vkv.h"

void vkv::coeff(double aa, double bb, double cc)
{
	a = aa; b = bb; c = cc;
}

bool vkv::losOp()
{
	double D = b * b - 4 * a * c;

	if (a == 0 || D < 0)
		return false;

	double wD = sqrt(D);

	x1 = (-b + wD)/(2 * a);
	x2 = (-b - wD)/(2 * a);

	return true;
}
// LATestVoorbeeldenBoekVervolg2.cpp:

#include <iostream>
#include "vkv.h"

using namespace std;

int main()
{
	double a, b, c;

	cout<<"Typ a, b en c: ";
	cin>> a >> b >> c;

	vkv v;

	v.coeff(a, b, c);

	if (v.losOp())
		cout<<"Wortels: "<< v.wortel1() <<" "<< v.wortel2() <<endl;

	else
		cout<< "Geen reële wortels.\n";

	return 0;
}

I have added the two path's that I told you and they show up in the Workspace's Header Files Map as vkv.h

In the map, they are shown as vkv.h as Type: C Header file

JoBe 36 Posting Pro in Training

Yep, forgot that :rolleyes:

Also changed the mistake in the first part in wich I had written two times private!

Didn't solve the problem though, now I'm getting those error messages at part two AND part three.

c:\program files\microsoft visual studio\myprojects\latestvoorbeeldenboekvervolg\vkv.h(1) : error C2143: syntax error : missing ';' before '--'
c:\program files\microsoft visual studio\myprojects\latestvoorbeeldenboekvervolg\vkv.h(1) : error C2143: syntax error : missing ';' before '-'
c:\program files\microsoft visual studio\myprojects\latestvoorbeeldenboekvervolg\vkv.h(1) : error C2501: 'LATestVoorbeeldenBoekVervolg' : missing storage-class or type specifiers
c:\program files\microsoft visual studio\myprojects\latestvoorbeeldenboekvervolg\vkv.h(1) : error C2143: syntax error : missing ';' before '-'
c:\program files\microsoft visual studio\myprojects\latestvoorbeeldenboekvervolg\vkv.h(4) : error C2017: illegal escape sequence
c:\program files\microsoft visual studio\myprojects\latestvoorbeeldenboekvervolg\vkv.h(4) : error C2017: illegal escape sequence
c:\program files\microsoft visual studio\myprojects\latestvoorbeeldenboekvervolg\vkv.h(4) : error C2146: syntax error : missing ';' before identifier 'Files'
c:\program files\microsoft visual studio\myprojects\latestvoorbeeldenboekvervolg\vkv.h(4) : error C2501: 'Program' : missing storage-class or type specifiers
c:\program files\microsoft visual studio\myprojects\latestvoorbeeldenboekvervolg\vkv.h(4) : fatal error C1004: unexpected end of file found
JoBe 36 Posting Pro in Training

Hi Dave,

If added the vkv.h file towards the two last parts of the program using this path:

C:\Program Files\Microsoft Visual Studio\MyProjects\LATestVoorbeeldenBoekVervolg\vkv.h

C:\Program Files\Microsoft Visual Studio\MyProjects\LATestVoorbeeldenBoekVervolg2\vkv.h

But, in the second part, I'm getting next error messages:

ATestVoorbeeldenBoekVervolg.cpp
c:\program files\microsoft visual studio\myprojects\latestvoorbeeldenboekvervolg\vkv.h(1) : error C2143: syntax error : missing ';' before '--'
c:\program files\microsoft visual studio\myprojects\latestvoorbeeldenboekvervolg\vkv.h(1) : error C2143: syntax error : missing ';' before '-'
c:\program files\microsoft visual studio\myprojects\latestvoorbeeldenboekvervolg\vkv.h(1) : error C2501: 'LATestVoorbeeldenBoekVervolg' : missing storage-class or type specifiers
c:\program files\microsoft visual studio\myprojects\latestvoorbeeldenboekvervolg\vkv.h(1) : error C2143: syntax error : missing ';' before '-'
c:\program files\microsoft visual studio\myprojects\latestvoorbeeldenboekvervolg\vkv.h(4) : error C2017: illegal escape sequence
c:\program files\microsoft visual studio\myprojects\latestvoorbeeldenboekvervolg\vkv.h(4) : error C2017: illegal escape sequence
c:\program files\microsoft visual studio\myprojects\latestvoorbeeldenboekvervolg\vkv.h(4) : error C2146: syntax error : missing ';' before identifier 'Files'
c:\program files\microsoft visual studio\myprojects\latestvoorbeeldenboekvervolg\vkv.h(4) : error C2501: 'Program' : missing storage-class or type specifiers
c:\program files\microsoft visual studio\myprojects\latestvoorbeeldenboekvervolg\vkv.h(4) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

LATestVoorbeeldenBoekVervolg.obj - 9 error(s), 0 warning(s)

And in the third part just one error:

ATestVoorbeeldenBoekVervolg2.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\LATestVoorbeeldenBoekVervolg2\LATestVoorbeeldenBoekVervolg2.cpp(9) : error C2239: unexpected token '{' following declaration of 'main'
Error executing cl.exe.
JoBe 36 Posting Pro in Training

Hello ladies and gents,

Ive been reading about how when a class becomes to big due to to many memberfunctions, it is best to split the program up into several sections wich could be something like this:

Interface section
Implemenation section
Application section

I was given an example wich is this one, but in trying this out, I got an error message in the Implementation and Application section wich was this one:

C:\Program Files\Microsoft Visual Studio\MyProjects\LATestVoorbeeldenBoekVervolg2\LATestVoorbeeldenBoekVervolg2.cpp(4) : fatal error C1083: Cannot open include file: 'vkv.h': No such file or directory
Error executing cl.exe.

I know it has to do with the fact that I have to implement the "vkv.h" file into the program, problem is, I don't know how or where I have to put it :-|

The program exists out of these three parts:

class vkv
{
private:

	void coeff (double aa, double bb, double cc);

	bool losOp();

	double wortel1()const {return x1;}
	double wortel2()const {return x2;}

private:

	double a, b, c, x1, x2;
};
#include <cmath>
#include "vkv.h"

void vkv::coeff(double aa, double bb, double cc)
{
	a = aa; b = bb; c = cc;
}

bool vkv::losOp()
{
	double D = b * b - 4 * a * c;

	if (a == 0 || D < 0)
		return false;

	double wD = sqrt(D);

	x1 = (-b + wD)/(2 * a);
	x2 = (-b - wD)/(2 * a);

	return true;
}
#include <iostream>
#include "vkv.h"

using namespace std; …
JoBe 36 Posting Pro in Training

Ok, I think I get the picture, thanks Dave ;)

JoBe 36 Posting Pro in Training

>I might say source files (.cpp), which are compiled into object files (.obj) and then linked together into the execute file (.exe) .

Yep, got it :!:

> I don't quite understand either.

Well, then it'll stay a mistery for the time being ;)

> ...: the neat little graphical stuff that keeps you from the land of makefile hell.

Thanks for the link :!: Thing is, I previously added that link of Wikipedia (wich you gave then aswell) to my favorites, don't know why I didn't use it to look it up myself :rolleyes:

>I'd say think of the standard library. There may be thousands of functions in there, but if you're only using two, only those two need be put in your executable.

So, if I understand you correctly, I could detract those functions from the standard library wich are used in my program, am I correct that these then should be written as #include "..... .h" :?:

JoBe 36 Posting Pro in Training

Thanks for the explanation Dave :!:

>The object files within the project are then linked together into an executable.

So, explained in my own words, the 'project' is actually just all the object files(.cpp) wich are then linked together into the execute file (.exe) :?:

Still am clueless in why they explained that 'project' bit then :confused:

>IDEs generally hide the fact that a makefile is used to handle the building of the project's target(s).

Euh, what are IDE's :?:

> Also, object files may be packaged into a library instead of an executable. Then the linker can pick from the library any routines used in source modules.

Is this what also happens when a class becomes to big or there are to many classes, it get's divided like this:
Interface

class vkv
{
     ....
};

Implementation

# include "vkv.h"  // h equals headerfile
# include ...

Application

# include <iostream>
# include "vkv.h"

using namespace std;

int main()
{
       ...
}
JoBe 36 Posting Pro in Training

Hello ladies and gents,

Ive been reading about when you have a very large program, that it is advisable to divide this into several modules. Ive tried this out with an example of two separate modules that where given as examples and managed to get two .cpp modules into the Source files of the workspace of one of those.

Examples:

#include <iostream>

using namespace std;

extern int n;
void f(int i), g();

int main()
{
	cout<<"Previous n = "<< n <<" (module 1)\n";

	f(8);
	g();

	cout<<"Press any key to continue!\n";

	return 0;
}
#include <iostream>

using namespace std;

int n = 100;
static int m = 7;

void f(int i)
{
	n += i + m;
}

void g()
{
	cout<<"After raising it with 8 + 7 (in module 2):\n";
	cout<<"n = "<< n <<endl;
}

I'm using VC++6.0 as compiler on XP Pro if you need to know.

I did it following this path: Project --> Add Project --> Files --> Added the necessary . cpp file!

Now, the question is, is this what is called linking?

The reason I ask is because in the book, after they talk about the 'linker', they talk about the following:

It's also possible in this compiler (the author also used VC++6.00 compiler) to make use of a 'Project' in wich the two named files would be incorporated (C1 module1.cpp module2.cpp). These executable files wich are created by executing this command 'Project' would have the name …

JoBe 36 Posting Pro in Training

>What are you talking about?
My mistake ;)

... that black thingy with white text that you type and it does stuff.
OH, NOW I understand :D

JoBe 36 Posting Pro in Training

>Something like that, yea. But wouldn't it be faster to try it out for yourself? ;)

How very true, but STL is chapter 10 :mrgreen: I'm at chapter 6 ;)

The author of the book showed a few examples in using STL and standard code! Had to do a few exercises with STL in chapter 5, but will be extended much more in chapter 10 :!:

Bye the way, I wouldn't dare to say that I could do it faster then what is written in that book you know :mrgreen:

Infact, can you tell me in wich type of programs you use STL regularly, I mean, is it used in games, embedded systems, .... ?

>Either will work from the shell. The only issue with back slashes is in C++ where they are the escape for special characters in string literals.

Understood, only, what do you mean with the expression "from the shell"?

JoBe 36 Posting Pro in Training

@ BruceWilson512,

Thanks for the tip, you mean something like this:

int main()
{
	int n, ni, i ,j;

	typedef vector<int> rij;

	cout<<"Geef het aantal regels dat volgt: ";
	cin>> n;

	vector<rij> v(n);	// vector van n rijen

	for (i = 0; i < n; i++)
	{
		cout<<"Geef ni, de lengte van de eerstvolgende regel: ";
		cin>> ni;

		cout<<"Tik " << ni <<" getallen in:\n";

		for (j = 0; j < ni; j++)
		{
			int x;
			cin>> x;
			v[i].push_back(x);
		}
	}

	cout<<"De volgende rijen zijn ingelezen:\n"<< fixed <<endl;

	for (i = 0; i < n; i++)
	{
		ni = v[i].size();

		for (j = 0; j < ni; j++)
		{
			cout<< setw(6) << v[i][j] <<" ";
		}
		cout<<endl;
	}
	
	cout<<"Press any key to quit!" <<endl;cin.get();

	return 0; 
}

Above code was given as an example and wasn't written by myself!

Now, I understand the above code and have a fairly good idea what typedef is doing, as it let's 'rij' be a vector on it's own correct?

But, what would I have to change to let this code work without the use of typedef?

I asume I have to make another vector from 'rij' then right?

@ Dogtree,

Would it be something like this then?

C:\> Documents and Settings/Johan Berntzen/Mijn documenten/Mijn ontvangen bestanden/VCPrograms> Test1
C:\> Test1

Also, do I need to use forward or backward slashes?

JoBe 36 Posting Pro in Training

Sorry Dave, I found it, I created this map VCPrograms, wich is the map that has to contain all the files with output. But, ofcourse, you have to give each file it's own name wich I didn't at first :o

std::ofstream out("C:/Documents and Settings/Johan Berntzen/Mijn documenten/Mijn ontvangen bestanden/VCPrograms/Test1");

Haven't figured out how this one works though

C:\> prog > file
C:\> type file

:?:

Do I have to enter the same path wich it has to follow as in the other ones?

Thanks for the help ;)

JoBe 36 Posting Pro in Training

Well, changed it towards forward slashes and not getting any error messages anymore, problem is, the path where it is pointed at, doesn't get any file loaded into it :confused:

I thought when I would write it like this:

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
	int i;

	std::ofstream out("C:/Documents and Settings/Johan Berntzen/Mijn documenten/Mijn ontvangen bestanden/VCPrograms");
	std::streambuf *saved_buff = std::cout.rdbuf();

	std::cout.rdbuf(out.rdbuf());


	for (i = 0 ; i < 10; i++)
	{
		std::cout<< setw(2) << i <<endl;
	}

	std::cout.rdbuf(saved_buff);

	cout<<""<<endl;cin.get();

	return 0;
}

I would find a file in VCPrograms with the output: 0 1 2 3 4 5 6 7 8 9

JoBe 36 Posting Pro in Training

Thanks for the explanation Dogtree, Ive been trying to get the output to work and if I'm correct, I assume that in the part of

std::ofstream out("file");

I have to put the place where the output should be saved to correct?

Well, Ive tried this with the following path

std::ofstream out("C:\Documents and Settings\Johan Berntzen\Mijn documenten\Mijn ontvangen bestanden\VCPrograms");

The problem is that I'm getting the following warnings :confused:

C:\Program Files\Microsoft Visual Studio\MyProjects\Testing\Testing.cpp(11) : warning C4129: 'D' : unrecognized character escape sequence
C:\Program Files\Microsoft Visual Studio\MyProjects\Testing\Testing.cpp(11) : warning C4129: 'J' : unrecognized character escape sequence
C:\Program Files\Microsoft Visual Studio\MyProjects\Testing\Testing.cpp(11) : warning C4129: 'M' : unrecognized character escape sequence
C:\Program Files\Microsoft Visual Studio\MyProjects\Testing\Testing.cpp(11) : warning C4129: 'M' : unrecognized character escape sequence
C:\Program Files\Microsoft Visual Studio\MyProjects\Testing\Testing.cpp(11) : warning C4129: 'V' : unrecognized character escape sequence

I'm undoubtedly doing something wrong, Ive changed the letters to 'small size'( <-- don't know the English word for this) but, it gives the same warnings so, I'm sure it's not related towards using big or small size letters.

The program code I used is similar to what you wrote:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	std::ofstream out("C:\Documents and Settings\Johan Berntzen\Mijn documenten\Mijn ontvangen bestanden\VCPrograms");
	std::streambuf *saved_buff = std::cout.rdbuf();

	std::cout.rdbuf(out.rdbuf());

	std::cout<< "output\n";

	std::cout.rdbuf(saved_buff);

	cout<<"Kopieer dit eens!"<<endl;
	
	cout<<"Press any key to continue!\n";cin.get();

	return 0; 
}
JoBe 36 Posting Pro in Training

1) Yep, that made it perfectly clear :!:

2) In other words, it means that *pp has those properties wich are mentioned in the structure right :?:

And can only a pointer have that ability to be written in that place :?:

What is the difference then when writing a structure, like this

struct row
{
      int *col, size;
};

int main()
{
      row pp;
...
}

and this:

struct row
{
      int *col, size;
}*pp;

int main()
{
      pp;
...
}

Because, the first example also gives the properties of the structure to the object pp right?

3) Well, Ive tried cutting and pasting, but, it doesn't work when the exercise is executed? Could you tell me how I can make it redirect towards the output of a file?
System is Windows XP Pro and compiler is VC++ 6.0

4) Well, I guess it depends on what you call simple right :o But, I do think I get the picture with you're explanation and Dave's code example, I think ;)

JoBe 36 Posting Pro in Training

Wow, thanks for that example.

Few questions Dave if you don't mind.

1) Am I correct that you wrote the structure inside main so you could get to its members directly?

2) One thing I don't understand, because I actually haven't seen it yet is this

struct row
   {
      int *col, size;
   } *pp;

Why is written outside the structure but yet inside the ; ?

3) You allways show your output in the manner as above, how do you do this, is this something you can let your compiler do?

4) When would you prefere your solution towards mine? For more complicated programs, so that, it actually becomes less complicated?

JoBe 36 Posting Pro in Training

Got it :D

Thanks Dave :!: Now, that's what I call, good TIPS ;)

for (i = 0; i < n; i++)
	{		
		cout<<"Geef ni, de lengte van de eerstvolgende regel: ";

		cin>>ni;
		pp[i] = new int[ni];
		teller[i] = ni;

		cout<<"Tik "<< ni <<" getallen in:\n";

		for(j = 0; j < ni; j++)
			cin>> pp[i][j];
	}

	for(i = 0; i < n; i++)
	{
		for(j = 0; j < teller[i] ;j++)
		{
			cout<< pp[i][j] <<" ";
		}
		cout<<endl;
	}

Is there another way I could have solved this, besides using STL <vector> ?

JoBe 36 Posting Pro in Training

Hi Dave,

Well, I tried to use this piece of code

pp[i] = new int[ni];

detemines how many there would be entered in each row of the array, I suspect(ed) that it was this what I needed.

But when I write

ni = sizeof pp[i];

my output four numbers in each row, if I entered more then four, they are not in the row of the array they supposed to be in.

If I enter less, it's filled up with random number (rubbish) :?:

I'm baffled, because, I don't know what else I should use other then that piece of code :confused:

JoBe 36 Posting Pro in Training

Hello ladies and gents,

I'm trying to output a 2D array, but can't seem to find the solution, can anyone help me out here.

#include <stdafx.h>
#include <iostream>

using namespace std;

int main()
{
	int **pp;
	int i, j, n, ni;

	cout<<"Geef het aantal regels als volgt: ";

	cin>> n;
	pp = new int *[n];

	for (i = 0; i < n; i++)
	{
		cout<<"Geef ni, de lengte van de eerstvolgende regel: ";
		
		cin>> ni;
		pp[i] = new int[ni];

		cout<<"Tik "<< ni <<" getallen in:\n";

		for(j = 0; j < ni; j++)
		{
			cin>> pp[i][j];
		}
	}

	for(i = 0; i < n; i++)
	{
		ni = sizeof ?????
		for(j = 0; j < ni; j++)
			cout<< pp[i][j] <<" ";
		cout<<endl;
	}

	for(i = 0; i < n; i++)
		delete[] pp[i];

	delete[] pp;

	cout<<"Press any key to quit!\n";cin.get();

	return 0; 
}

I know I have to use the size of each row of the array, but fail to see wich piece of code I have to use to write next to sizeof

for(i = 0; i < n; i++)
	{
		ni = sizeof ?????
		for(j = 0; j < ni; j++)
			cout<< pp[i][j] <<" ";
		cout<<endl;
	}

Any help would be greatly appreciated :)

JoBe 36 Posting Pro in Training

>I think the exercise is trying to be simple, you have have way overcomplicated it.

Well DAve, I think, no, I know you just hit the nail on the head ;)

That's my problem, I allways think that the solution is much more complicated then it really is :!:

Sometimes I think that it can't be the intention of having a simple solution :confused:

JoBe 36 Posting Pro in Training

Hi DAve,

Problem is, the exercise mentions the combination of operator< and the sort algorithm. I don't see how I have to combine those two to get them into one list.

Also, the numbers are read from the keyboard, not const integers.

JoBe 36 Posting Pro in Training

I'm reading it like this: use the STL sort -- this requires that you only write an operator< [saving you from needing to write some sort algorithm of your own that will be far too much fun to debug -- all that for the price of one easy function, not bad].

But, do you think it's the intention of the exercise to add the values of q(10,16) to the values of p and sort them again :?:

So that you would get something like this:

Values entered for p:
10 15
35 20
25 5

Sorted:
5
10
15
20
25
35

And then: q(10, 16) added to p would give sorted.

5
10
10
15
16
20
25
35

How do I do that :?:

I'm almost certain that you can do this by combining the operator< and the sort algorithm, but I can't see how :confused:

JoBe 36 Posting Pro in Training

Must be, probably because of different timezones aswell :!:

JoBe 36 Posting Pro in Training

Is there actually one of you ladies/gents using Irc :D

Ive been online a few times now, never seen anyone there, it sure is quiet around #Daniweb :!:

JoBe 36 Posting Pro in Training

Actually it would be

cout<< !(p<q) <<endl;

Why has there got to be a '!' infront of it, I tried it out without it and it works aswell, or is this reversing the 0 and 1 if I don't write the '!' infront of it?