JoBe 36 Posting Pro in Training

Thanks for the help Dave, but, can you explain this to me without using those two functions, it's an exercise in wich functions an references aren't seen yet and to be honest, I'd rather do this exercise without them, it's to confusing for me at this moment ;)

So, how could I do that without using those functions?

Thanks

JoBe 36 Posting Pro in Training

Hi Narue,

Ive been playing around with the code you gave me and alltough I'm pretty sure I understand what is happening, I can't really figure out how this is going to help me in getting four different numbers smaller then 16 into one variable x :o

I understand that when having four bits 1111, I get a decimal maximum of 15.

I understand that by using those binary operators << or >> you can shift the binary to the left or right adding a ZERO to the four bits and changing the decimal number.

I understand that using the code x = bit(i) | 1; am putting the bit (i) to 1 and changing the decimal number again.

Have a few question about it also

#define bit(x) (1UL << (x))

int main()
{
  unsigned int x=0;

  x |= bit(0) | 1; // 00000001 = 1
  cout<< x <<endl;
  x |= bit(1) | 1; // 00000011 = 3
  cout<< x <<endl;
  cout<< (x >> 1 << 1) <<endl; // 00000010 = 2 
  x |= bit(2) | 1; // 00000111 = 7
  cout<< x <<endl;
  x |= bit(6) | 1; // 01000111 = 71
  cout<< x <<endl;

  // Get back a sliver
  cout<< (x >> 1) <<endl; // 00100011 = 35
  cout<< (x >> 2 << 2) <<endl; // 01000100 = 68
  cout<< (x >> 1) <<endl; // 00100011 = 35
  cout<< (x >> 1 << 1) <<endl; // 01000110 = 70
  cout<< (x >> …
JoBe 36 Posting Pro in Training

I will ;)

Thanks Narue!

JoBe 36 Posting Pro in Training

Hello ladies and gents,

I'm starting on the next exercise, but I'm puzzled a bit and was wondering if any of you might help me with this.

I have to enter four integer numbers, non negative and smaller then 16 into ONE variable int x, now, the thing I was wondering is, do I have to use an array for this int x, so that I get int x[3] put the four numbers smaller then 16 into the array or is it possible to solve this solely by using a certain bit operator?

The idea is, when those four numbers are entered, I then have to select a number from 0 to 3 and depending on the selection it has to give me the previous entered number smaller then 16!

Hope you understood what I'm trying to accomplish.

JoBe 36 Posting Pro in Training

Hi jeanatkin,

Thanks for the offered help, but, I think I got it working now :D

#define AMOUNT 30			 
#define HOWMANY 10			 
#define WIDTHARR 9			 

int main()					
{
int s[AMOUNT], i=0, x=0;

                while (i<AMOUNT)
                {
                           cin>> s[i];
                           if (cin.fail())		 
                           {
                                            cin.clear();
                                            break;
                           }
                i++;
                }

for (int u=0;u<i;++u)
{
                for (int j=0;j<u;j++)
                if (s[u]==s[j]) 
                {
                                  x++;
                                  break;
                }
}
	
i-=x;
		
if (i>=10)
cout<<"The amount of different numbers is bigger then TEN ! "<<endl;
                else
                cout<<"The amount of different numbers is: "<< i <<endl;

int j=i;	 

for (i=0;i<j;i++)	 		
{
                cout<<setw(4)<<s[i];	 		
                if (i%HOWMANY==WIDTHARR)cout<<end
}

cout<<endl;

return 0;

}

If you think I could change something wich would improve the code, then by all means, tell me :D

JoBe 36 Posting Pro in Training

DOH :o

Now I understand it :D I tought you where referring to this part in the loop:

string line;
      cout << "? ";
      if ( getline(cin, line) )            // get response as text
      {
         if ( line == "STOP" )
         {
            break;
         }
JoBe 36 Posting Pro in Training

But the solution has to be made without you having to convert a string into an integer.

Converting is something that is not shown before this exercise, so, actually, it doesn't exist yet :-|

JoBe 36 Posting Pro in Training

Hi guys,

Sorry to bother you again with this stupid exercise, but I can't get it to work properly :o

#define AMOUNT 5

int main()
{
	int s[AMOUNT], x=0;

		for (int i=0;i<sizeof s / sizeof *s;i++)
			cin>>s[i];cin.get();

		for (i=0;i<sizeof s / sizeof *s;i++)
		{
			for (int j=0;j<i;j++)
				if (s[i]==s[j]) 
				{
					x++;
					break;
				}
		}

		i-=x;

		cout<<"The amount of different numbers is: "<< i <<endl;

		for (i=0;i<sizeof s /sizeof *s;i++)
		{
			cout<<setw(4)<<s[i];
			if (i%AMOUNT==9)cout<<endl;
		}

	cout<<endl;

	return 0;
}

The problem is that when I enter a word like STOP, the program does stop but it does show the amount of numbers that have to go into the array.

I'll try to explain this abit better, when the array is equal to five as in the example, and I enter three numbers and then enter 'STOP', the other two places in the array get filled up aswell, how can I avoid that?

Thanks for the help guys and sorry to keep bringing this up, but I'm not going to start another exercise before I get this one to work properly :!:

JoBe 36 Posting Pro in Training

Found the cause of the problem, seems everytime I enter more then twice the same number it counts this of course as also the same number and increments x by one!

If I put in twenty random numbers from wich there are never more then two the same, there's no problem, but then again, that's not the idea of this exercise :cheesy:

JoBe 36 Posting Pro in Training

Hi guys,

Ive been working on this abit more and decided to try out to find the amount of different numbers first and then, when this works, add the stopping part!

What I got untill now is this:

#define AMOUNT 20

int main()
{
	int s[AMOUNT],n, x=0;

		for (int i=0;i<AMOUNT;i++)
		{
			cin>>n;cin.get();
			s[i]=n;
		}

		for (i=0;i<AMOUNT;i++)
		{
			for (int j=0;j<i;j++)
				if (s[j]==s[i])
				x++;
		}

		i-=x;

		cout<<"The amount of different numbers is: "<< i <<endl;

		for (i=0; i<AMOUNT;i++)
		{
			cout<<setw(4)<<s[i];
			if (i%AMOUNT==9)cout<<endl;
		}

	return 0;
}

Problem is, the amount of different numbers isn't correct :-|

Can anyone please explain what I'm doing wrong, thank you ;)

Also, is it better or wiser to enter the random numbers directly into the array cin>>s; or is it better to use another variable :?:

JoBe 36 Posting Pro in Training

@ Dave, thanks for the tip.

@ Vegaseat, yes, I'm a very big NOOB still :mrgreen:

I have been following lessons in evening school and have seen Structured Programming and Object Oriented programming.

Problem is, this course was given in a modular system within a time period of FOUR months wich was just to fast for me, that's why I decided to do this course again starting in September but in the mean while I wanted to learn as much as possible on my own.

Therefore, coming to your question, it's a Dutch book from a professor called Leen Ammeraal, he's a Mathematical Engineer and teaches or teached (don't know exactly) at the High School of Utrecht.

That's why you'll see text in my code that's written in Dutch ;)

JoBe 36 Posting Pro in Training

Hi guys,

Thanks for the help and examples you have given, tough, there's one thing that is important, that is, this exercise is stated just when you learned about iterations, selections, array's.

I'm sure as you have shown that there are several solutions to this problem, but I would like to stick with the book I'm reading and with the chapter just read, using pointers, and stuff that I haven't even seen or read about yet, it would be much to confusing :o

Vegaseat, thanks for the example, but as said before, you are using code that I haven't even learned or know about ;)

There has to be an easier way, right :?:

JoBe 36 Posting Pro in Training

That still doesn't change the fact that when you enter twice the same number, it still stops the count and that's not the idea ;)

Like I said earlier, I'm not certain wether I need to do this with an array, the idea is solely to enter numbers and aslong as the amount of different numbers doesn't equal ten, I could enter one thousand numbers by manner of speaking and it would still run.

It ONLY stops for two reasons,:
1) when after a certain amount of numbers I type in a word or letter.
2) the amount of DIFFERENT numbers is equal to TEN!

int main()
{
	const int amount=10;
	int s[amount],n=0, x=0,y=0;

		for (;;)
		{
			if (x==10) cout<< "The amount of different numbers has reached it's limit!"<<endl;
				else
					cin>>n;
					++y;
						for (int i=0;i<y;i++)
						{
							if (n==s[i])break;
								if(s[i]>0 && n!=s[i])x++;
						}
						s[i]=n;
		}
		cout<<"The amount of different numbers is: "<< x <<endl;

	return 0;
}

The problem with my code is that it allways increases x with one even if it allready has done this the previous time for the number.

I'll try and explain a bit better, if I have following numbers entered, 1, 45, 43, 54, 34, 63.

My code will compare them with eachother and increase x everytime it runs trough the array. So, it's like it compare's 1 with the others and this puts x equal to 5 and then it'll compare 45 with the others, meaning 43, …

JoBe 36 Posting Pro in Training

Thanks for the info, tried out your version but it doesn't work as It should,
from the moment you enter a number that allready exists, the program stops and says how many different numbers there are, that's not the idea, it should keep on going aslong as the amount of different numbers isn't more then ten.

If you enter a word after a certain amount of numbers it should stop aswell, your program does but adds the word to the amount of different numbers and gives a ZERO in your array and gives a random negative numberafter that aswell in your array.

JoBe 36 Posting Pro in Training

Thanks for the help guys,

@ Chainsaw,
I'm using the variable y as an indicator of how many numbers there are in the array. Replacing this by x isn't possible in my opinion because x is the indicator how many different numbers there are!

The stop is effectively used to end the entering of numbers, because I'm using an integer, when entered a char it will stop.

@ Dave,

Could you explain why you use "i < sizeof array / sizeof *array " as an selection in your loop?

I allways heard that using goto is to be avoided at ALL COSTS :!:

JoBe 36 Posting Pro in Training

Hi all,

Ive changed it abit, but still not working as it should :-|

int main()
{
	const int amount=10;
	int s[amount],n=0, x=0,y=0, z=0;

		for (;;)
		{
			if (x==10)cout<< "The amount of different numbers has reached it's limit!"<<endl;
				else
				cin>>n;
				++y;
				for (int i=0;i<y;i++)
				{
						if (n==s[i])break;
						
				}
				x++;
				s[i]=n;
				
		}
		cout<<"The amount of different numbers is: "<< x <<endl;

	return 0;
 }
JoBe 36 Posting Pro in Training

Hello ladies and gents,

I'm trying to write this little program in wich I have to enter by keyboard a certain amount of numbers (int) not floating points, and the program must count how many different numbers I have entered, when the amount is equal to ten, it must say that the amount of different numbers has reached it's limit!

If the limit is not reached I must enter a word or letter like STOP or stop or s or whatever and the program should show me how many different numbers there actually are, wich of course is lower then 10.

I have written this piece of code, but it's not doing what it should do :)

int main()
{
	int x, y=0, z=0, MAX= 10;

	for (int i=0;y<MAX;i++)
	{
		if (y==MAX)
			cout<< "The amount of different numbers has reached its limit!"<<endl;
		else
		{
			cin>>x;
			for (int i=0; i<MAX; i++)
			if (x!=z)
			{
				y++;
				z=x;
			}
		}
	}
	cout<< "The amount of different numbers is: " << y <<endl;

I also tried it out with an array because I was not and still am not certain wether this could be done without an array!
I think the control should be done by going threw the array each time a number is entered and that this should determine wether the number allready exists or not, if it exists, the amount should be added by 1, if not, I should be able to continue adding numbers until …

JoBe 36 Posting Pro in Training

Thanks Narue,

Changed the last loop and deleted those braces, like you said, there not needed !

Tought I had some questions, but after reading your code more carefully, I understand what your doing!

Hope you don't mind me trying out your code to see what happens ;)

JoBe 36 Posting Pro in Training

Hello ladies and gents,

I was making this small exercise in wich I have to enter a certain date meaning, day, month and year.
If entered it should count the amount of days from the beginning of that year and print it.

I found a solution for this, but was wondering wether there is an alternative or maybe somethings you might change in this code. I know that there is a certain library wich makes this automatically, but then again, it wouldn't be much of an exercise now would it ;)

The code is:

int main()
{
	int dag, maand;
	int month= 1, year=0, amount=0;

	cin>> dag;cin.get();
	cin>> maand;cin.get();
	cin>> year;cin.get();

	int tabel[12]={31,28,31,30,31,30,31,31,30,31,30,31};
	tabel[1]=28+(year%4==0 && year%100 !=0 || year %400==0);

	 
	for (int i=0;i<maand;i++)
	{
			if(month!=maand)
		{
			amount+=tabel[i];
			month++;
		}
	}
		for (int day=0; day<dag; day++)
		{
			{
			           amount++;
			}
		}

	cout<<amount<<endl;

	return 0;
}
JoBe 36 Posting Pro in Training

Hi Acidburn, change the " to ' Ive tried it out and got the exact error message you got:

C:\Program Files\Microsoft Visual Studio\MyProjects\Testing\Testing.cpp(14) : error C2440: 'initializing' : cannot convert from 'char [4]' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\Testing\Testing.cpp(14) : error C2440: 'initializing' : cannot convert from 'char [4]' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.

Testing.obj - 2 error(s), 0 warning(s)
JoBe 36 Posting Pro in Training

hello, ok

here it goes...

I've got a char array but it doesnt want to let me add data to it!! I cant figure out why

//skip the program heres the line...

int chararry[12][10] = {Jan, Feb....}

but I keep getting an error saying its not been initialized... I've tried google but nothing :eek:

What about the ; behind {}

And 'Jan', 'Feb'

JoBe 36 Posting Pro in Training

ROFLMAO, oh those employees are so touchy these days :mrgreen:

Before I forget, thanks for the links Dani :)

JoBe 36 Posting Pro in Training

Thank you very much ladies :)

Narue, ever tought of becoming a teacher, I'm certain you'd be great at it ;)

JoBe 36 Posting Pro in Training

Hello ladies and gents,

I'm trying to reverse a string when one is entered with cin.getline, so when typing for example: testing

My output should become: gnitset

Ive written this piece of code and it works but, because the string has a certain length and not the whole length of the string is neccesarly being used the part wich is not used is also being shown in my output?

How do I get rid of this?

int main()
{
	char tekst[20];

	cout<< "Put in a line of text! ";

	cin.getline(tekst, 20);

	cout<< "Reverse the string now!" <<endl;

	for (int i=0; i<sizeof tekst; i++)
		cout<< tekst [sizeof tekst-i-1];
 
	cout<<endl;
	return 0;
}

Allready tried a search on this topic, came up with four possibilities and none really was sufficient for my problem, so thanks for the help in advance ;)

JoBe 36 Posting Pro in Training

@ 1o0oBhP

I'm from the Flemish part of Belgium, but we do speak the same language as the Dutch ;)

So, Dutch and Flemish writing are one and the same :!:

JoBe 36 Posting Pro in Training

Thanks for the help, seems you we're thinking about modulo as some of my friends we're :D

int main()
{
	int a=0, b=0, c=0, x, getal=0;
	
	cout<< "Lees een rij van tien positieve getallen in en bepaal hoeveel van deze getallen"
	" door 2, hoeveel door 3 en hoeveel door 5 deelbaar zijn! "<<endl;

	for (int i=0; i<9; i++)
	{
		cin>> x;cin.get();

		if (x%2 == getal)
			a++;
			if (x%3 == getal)
				b++;
				if (x%5== getal)
					c++;
	}

		cout<< "Het aantal gedeeld door 2 is gelijk aan: "<< a <<endl;
		cout<< "Het aantal gedeeld door 3 is gelijk aan: "<< b <<endl;
		cout<< "Het aantal gedeeld door 5 is gelijk aan: "<< c <<endl;

	cin.get();

	return 0;
}

Thanks again for the help ;)

JoBe 36 Posting Pro in Training

Ok, here's the code Ive written so far:

int main()
{
	int a=0, b=0, c=0, x, number;
	
	cout<< "Give ten numbers and see how many are dividable by 2, how many by 3 and how many by 5! "<<endl;

	for (int i=0; i<4; i++)
	{
		cin>> x;cin.get();

		float result= float(x)/2;
		if (result1== number)
			a++;
			float result2=float(x)/3;
			if (result2== number)
				b++;
				float result3=float (x)/5;
				if (result3!= number)
					c++;
	}

		cout<< "The amount dividable by 2 are: "<< a <<endl;
		cout<< "The amount dividable by 3 are: "<< b <<endl;
		cout<< "The amount dividable by 5 are: "<< c <<endl;

	cin.get();

	return 0;
}

if (result1== number)// this is the problem here, if result is equal to anything but .0, a shouldn't be added by one, like if result equals 1.8 or 5.3 a should remain as before, only if it would be 1.0 or 5.0, a should be added by one!

Hope you understand what I mean.

Thanks for the help ;)

JoBe 36 Posting Pro in Training

Hello ladies and gents,

Ive written a program wich I'm almost certain it'll work, Ive only got one problem, I need to write an if like this:

int a = 0;


if ( u!=x)
a++;

Now, u is a floating variable wich is divided before this selection by 2, 3, 5.

It's got to state that when u equals any floating point like 1.1 untoo 1.9 it shouldn't ad one to a, if u is 2.0 or 3.0 or whatever aslong as it's not .1 untill 0.9, it should ad a++

It's probably something simple wich I'm not seeing, but I can't figure out what :)

Any help would be appreciated.

JoBe 36 Posting Pro in Training

@ Chainsaw & Dave Sinkula,

Thanks for the help guys, very much appreciated!

Chainsaw, am I correct when saying that when following your example, the pointer directs to the first location(adress) of each dogs name and that therefore each dog has it's own pointer with the adress of the array where it's being started???

Therefore calling it an array of pointers wich have strings inside them?

Are *name and &name both referring too a certain adress in the memory?
Are *name[] or name[][] both referring to the actual string of names in the arrays?


And Dave, Ive tried out those two examples you gave me earlier and those are a big help, I'll try and work it out with the last example you gave me!

In this you are working with a declaration of a pointer char*a and one time referring to it with a two dimensional array correct?

Because char*a[] is the same as char a [][], in the first you are referring towards adresses wich contains arrays, second one is the same but with a two dimensional declaration of an array correct?

Both are referring to the letters inside the array, when wanting the adresses, you need to type in name or &name?

Anyway, I'll certainly give it a go ;)

JoBe 36 Posting Pro in Training

@ Dave Sinkula, do I understand the difference, not entirely no :-|

But thanks for the two different examples, I'm going to try and see how they work and see wether by this I can grasp the difference between the two and more importantly, try and understand why it has to be written like that. Just a pitty I can't ask those twenty questions, seems your the person who could bring clarity intoo this subject ;)

@ big146, thanks for trying to help, but your example makes me confused even more, to me it seems your using items wich are related towards structures, tough we have seen them aswell, I'm trying to understand pointers and mixing them with structures isn't really helpfull tough I appreciate your effort. Thing is, I wish we got more time to learn the different subjects of C++, but fact is, we're learning structured programming and Object Oriented programming within a time period of FOUR (4) months :!:

Not only that, we also have to make assignements in a certain period wich count for are final exams.

My personnal feeling about it is that alltough I understand the overall syntax, there are manny different subjects wich are not 100% clear to me from wich pointers is the one that gives me the biggest problems.

JoBe 36 Posting Pro in Training

1) Post code. I really hate playing 20 questions.
Declaration: char *naam[10];

2) Google for "bubble sort".
Ok, will do
[To me, I hear an echo of "kuckoo for Cocoa Puffs". Wierd.]
Nope, you've got me, don't understand this, tough, I do get the feeling it's written with a twitch of sarcasm right :mrgreen:

3) I'm not that hip on sorting, but it looked like the bubble sort to me.
Guess you're telling me to check out bubble sort, though, not sure about it since I'm not native English speaking, thanks anyway.

You could say, subject closed :)

JoBe 36 Posting Pro in Training

How do you declare the array that you are passing to the function?

Well, that's an array, but one of pointers correct?

I'm not that hip on sorting, but it looked like the bubble sort to me.

Link isn't working.

Your assumption of -1, 0, 1 as return values is not entierly accurate for strcmp.

Ok, I'll read this one, if it's not to much trouble, could you explain the things Ive asked in my previous post, thanks in advance.

JoBe 36 Posting Pro in Training

Hi, the idea is just to put ten names, dave, johan, eric, kathy, ... ten names in alphabetical order, to do this I wanted to write this function in wich instead of arrays being used, it would be done by pointers, so selecting and putting them in alphabetical order would be done by pointers :!:

That's what I wanted to do, thanks for the example and yes, it is the second subject that I wanted to do :)

But, Ive got one more thing to ask, could you please explain the following, what and why you are doing this.

for ( i = 0; i < size - 1; ++i )
for ( j = i; j < size; ++j ) why do you need two different variables i and j ?


if ( strcmp(name, name[j]) > 0 ) You've got me here, why two different letters, I assume they are connected towards int i and int j, but why, how...

And why do you make it so that they both have to be bigger then 0 ???????

Thanks again for the help!

JoBe 36 Posting Pro in Training

This is what Ive come up with:

for(int i=0;i<9;i++)
	{
		int resultaat= stricmp(naam[i], naam[i+1]);

		if (resultaat==1)
		{
			strcpy(temp, *naam);
			strcpy(*naam, *(naam+i));
			strcpy(*(naam+i), temp);
		}
	}

My theorie on it:

I didn't need the second loop because the first as Ive said before is selecting the names one by one.

stricmp will give a value to resultaat of -1, 0 or 1, if the value is one, the selection will step in and threw use of strcpy the two names will get switched!

That's my theorie :!:

Problem is, it doesn't work, so I would really really really appreciate it, if someone could explain to me why it's not working.

JoBe 36 Posting Pro in Training

- Ive allready changed the char temp into char temp[20] so it can contain not only one letter but indeed the full name!

- If I'm feeding it with char's, then how do I feed him strings, that is the question I'm asking ;)

- I was using the inner to get from the first to the second name, tough, Ive come to realize that:

- don't need it, because I have one loop from 0-9.

- and stricmp is selecting number and [i+1].

JoBe 36 Posting Pro in Training

Ok guys,

I'll try to explain what my idea is/was with this function, I want to be able to sort ten names wich are randomly picked and put them in alphabetical order.

char temp= a temporary place to put the first of the names into so the actual first place of the name is free!

stricmp(*naam, *naam[i+1]); = this in my opinion will make a comparison of the first and second name to see wether the first name is should be in front of the second, if not it will switch the names by:

strcpy(temp, *naam); = puts the name into a temporary place.

strcpy(*naam, *naam[i+1]); = puts the second name into the first place.

strcpy(*naam[i+1], temp); = puts the first name wich was transported to temp into the second place.


naam equals to me a firstname from a person.
temp equals to me as a temporary place to put a certain name wich isn't in it correct place.

Hope you guys understand what I mean by this explanation?

JoBe 36 Posting Pro in Training

Hello ladies and gents, I have tried to write my sorting function but keep on getting error messages.

Ive been reading the tutorials on this forum wich explaines alot but I'm still having lots of trouble in comprehending how pointers work and more important, how that I can correctly write the code that I need for them

Hope someone can help me out and explain with this piece of code what I'm doing wrong and how I have to change it and most important why it has to be changed!

void sorteer(char *naam[])
{
	for (short i=0; i<9;i++) 
	{
		for (short i=0; naam[i]!= '\0';i++)
		{
				char temp;

				stricmp(*naam[i], *naam[i+1]);
				strcpy(temp, *naam[i]);
				strcpy(*naam[i], *naam[i+1]);
				strcpy(*naam[i+1], temp);
		}
	}

}
JoBe 36 Posting Pro in Training

Hello ladies and gents, I have tried to write my sorting function but keep on getting error messages.

Ive been reading the tutorials on this forum wich explaines alot but I'm still having lots of trouble in comprehending how pointers work and more important, how that I can correctly write the code that I need for them :-|

Hope someone can help me out and explain with this piece of code what I'm doing wrong and how I have to change it and most important why it has to be changed!

void sorteer(char *naam[])
{
	for (short i=0; i<9;i++) 
	{
		for (short i=0; naam[i]!= '\0';i++)
		{
				char temp;

				stricmp(*naam[i], *naam[i+1]);
				strcpy(temp, *naam[i]);
				strcpy(*naam[i], *naam[i+1]);
				strcpy(*naam[i+1], temp);
		}
	}

}
JoBe 36 Posting Pro in Training

New Member

Hi everyone. I am brand spanking new. Like, just a few minutes born to the new kid in the DaniWeb block. Today I will just get the feel and browse. However, I will host a lot of questions that my experienced curiosity has brought to mind and many are not answered yet.

Thank you all for accepting me as your new member!!

Truly, Louisinho

Hi Louisinho,

Allways great to see new people who want to share there knowledge and help us noobs in C++ with advice ;)

JoBe 36 Posting Pro in Training

I know you told me I couldn't use delete (naam), I wasn't referring to that, I refered towards delete naam

I understand what the difference is now between delete naam and delete[] naam[].

Thanks for the help, much appreciated!

JoBe 36 Posting Pro in Training

Thanks again for the explanation, two other questions about it:

1) Why can't I write, delete naam then, since it is linked towards the array of pointers?

2) When placing the delete [] naam into my program, am I correct in assuming it should be in the function, alltough, I'm not sure because by doing so I would automatically delete the array of pointers without it even getting to main correct?

So, does it have to be stated as last part in main?

JoBe 36 Posting Pro in Training

Thanks Asif,

It's working!

Got another question tough, when using new, it is best to give the used memory back and doing so is by using delete(naam)!

Since the creation of the dynamic memory ( new ) is in the function, I assumed that I need to put ( delete ) into the function aswell, correct?

@jwenting, euh, I'll let you guys see how I did it as soon as I'm tackling that part ok :lol:

wenting, hum, by anychance Dutch?

JoBe 36 Posting Pro in Training

Hello ladies and gents, could someone explain what I'm doing wrong here, the ideas of this excercise is:

- Declare an array with 10 firstnames using pointers!

- Show the array on screen using a function!

- Sort out the names alphabetically and show the array again sorted!

I want to try and write the program first just getting the ten names into the array using pointers, but there seems to be something wrong, message I keep getting is:
error C2664: 'inhoud' : cannot convert parameter 1 from 'char [10]' to 'char *[]'

I tried to use the library MSDN, but I don't fully understand the example that is being given in it?

This is my code:

void inhoud(char *naam[]);

int main()
{
	char naam[10];	

	cout<< "Voer tien voornamen in: " <<endl;
	
	inhoud(naam);	

	return 0;
}

void inhoud(char *naam[])
{
	for (short i=0; i<10;i++)
	{
		naam [i]= new char [20];
		cin.getline(naam[i], sizeof naam);
	}
}

Could someone please explain this please, I don't want someone to give the solution, but if you could give me an example how it should be written and why you wrote it that way, I would appreciate it alot. ;)

JoBe 36 Posting Pro in Training

LOL, not really, that is really perfectly clear, I hope you won't mind me saying, but I'll use my solution to turn in tough, this way it was made 'mostly ;) ' by me.

Sure wish I could make it seem so easy like you did :cheesy:

I'm allready thrilled when I can write something like this:

#include <stdafx.h>
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{	

	short s=0,y=0;
	short begin=0, eind=0;

	cout<< "Geef alle mogelijke combinatie's van een geheel getal 
                       met twee of meerdere opeenvolgende getallen" << endl;

	cout<< "Als het geheel getal gelijk is aan: ";
	cin>>s;cin.get();

	cout<< "Dan zijn er de volgende oplossingen: "<<endl;

	do
	{
		short z=0,w=0;
		y=++y;
		w=y;

			while(z<s)
			{
			z=z+w;			
			w++;
			}
	
		if(z==s && z!=y)
		{
			begin=y;
			eind=w;
			for (begin;begin<eind;begin++)
			{
				cout<<setw(3);
				cout<<begin;
			}
			cout<<endl;
		}
	}
	while (y<s);

	return 0;
}

And yeah, I'm showing of :lol:

Anyway, thanks for your help and time Narue :!:

JoBe 36 Posting Pro in Training

I understand what you mean Narue, actually, it's my fault, the idea was and is in the program that we have to use a function or functions.

Also, there has to be an implementation of a string (cin.getline) and an array
(aantal[26])since these where the subjects that we saw before we got our assignment.
I understand that there are probably many other solutions for this, but we have to use those subjects so he can see wether we understood what we we're thaught.

So, next time I'll give a better explanation of what the exercise is ment to do :!:

JoBe 36 Posting Pro in Training

Lattest version with function?

short zoek(const char str[], char letter);

int main ()
{
    static short aantal[26];
    char str[80], letter='a';

    cout<< "Geef een zin in: " <<endl;

    cin.getline(str,sizeof str);

    for (short i=0;i<26; i++)
    {
    aantal[letter -'a']= zoek(str, letter);
    cout<< char(i+'a') << " = " << aantal[letter-'a'];
    cout<< setw(5);
    if(i%7==6)cout<<endl;
    letter++;
    }

    cin.get();

    return 0;
}

short zoek(const char str[], char letter)
{
    static short aantal[26];

    for (short i=0; str[i] !='\0'; i++)
    {
        if(str[i]==letter)
        aantal[letter -'a']++;
    }
    return aantal[letter -'a'];
}
JoBe 36 Posting Pro in Training

Well Narue,

It's not only doubling the performance, it's also much more comprehensible :!:

JoBe 36 Posting Pro in Training

Solution :?:

int main ()
{
    static int aantal[26];
    char str[80];

    cout<< "Geef een zin in: " <<endl;

    cin.getline(str,sizeof str);

    for (int i=0; str[i] !='\0'; i++)
    {
        for (char letters='a'; letters <='z';letters++)
        {
            if(str[i]==letters)
            aantal[letters -'a']++;
        }
    }

    for (i=0;i<26; i++)
    {
    cout<< char(i+'a') << " = " << aantal[i];
    cout<< setw(5);
    if(i%7==6)cout<<endl;
    }

    cin.get();

    return 0;
}
JoBe 36 Posting Pro in Training

>little bit old don't you think?
IMO, the only better reference than The C++ Programming Language, is the C++ standard itself. Rest assured that everything in it is up to date enough for your needs.

Ok, I'll keep it in mind :!:

JoBe 36 Posting Pro in Training

>That's why I showed you an example that couldn't possibly be accepted as homework. It's just a way for me to show off. ;)

<<Oh, showboat LOL

>Before C++ was standardized, everyone basically agreed to use headers such as iostream.h as defined by The Annotated C++ Reference Manual written by Bjarne Stroustrup. When the language was standardized in 1998, these headers were edited heavily and the names were changed to remove the .h extension. You're being taught an old dialect of C++ that is quickly dying out.

<<Understood, I'll certainl mention this to him ;)

>...returning a value from main results in undefined behavior.

<<Understood.

>You have to do what you have to do. Just remember that by assuming ASCII your code isn't as portable as it could be.

<<Well, for this excercise I'll use it, but will keep in mind of the portability problem with it!

>Neither is better than the other simply because the former is more efficient for your purposes, but the latter is efficient if you need a more general frequency table for the character set.

<<Thanks for the examples, In recoding my own, I'll surely will try and see how and more important why you did it in that certain way :!:

>Yes and no. I make use of standard objects, but the term generic programming (through templates) would be more appropriate than object oriented programming in this case.

<<Well, I'll try that type …