William Hemsworth 1,339 Posting Virtuoso

if ( color && 1 ) puts("violet");
if ( color && 2 ) puts("indigo");
if ( color && 4 ) puts("blue");
if ( color && 8 ) puts("green");
if ( color && 16 ) puts("yellow");
if ( color && 32 ) puts("orange");
if ( color && 64 ) puts("red");

Shouldn't it be a & and not a &&? Otherwise the && # is pointless.

jephthah commented: yes, your're right. and this is embarrassing. :P +10
William Hemsworth 1,339 Posting Virtuoso

>William, regarding your post (#11): I think that int main( void ) is the preferred way in C
I think we're looking into things far too much here. It works now, so let's just leave it at that, OK? :icon_wink:

I'll start using a void parameter from now on.

tux4life commented: There's a difference :P +7
William Hemsworth 1,339 Posting Virtuoso

It is always better to put the statements in if conditions in brackets. And start using else if's.

Who says using brackets is better? IMO your code was harder to read than the original code. If it were me, I would have written it like this:

#include <stdio.h>

int main() {
  int color;
  
  printf( "Enter any number:\n> " );
  scanf( "%d", &color );

  switch ( color ) {
    case 1:  printf( "\nviolet" );  break;
    case 2:  printf( "\nindigo" );  break;
    case 4:  printf( "\nblue" );    break;
    case 8:  printf( "\ngreen" );   break;
    case 16: printf( "\nyellow" );  break;
    case 32: printf( "\norange" );  break;
    case 64: printf( "\nred" );     break;
  }

  getchar();
  getchar();
}
iamthwee commented: Sorry but I disagree with your brackets statement! -4
Aia commented: If you disagree make a relevant post about it. Instead of grading posts like if you were somebody. +17
William Hemsworth 1,339 Posting Virtuoso

>>...but #include "stdafx.h" is missing
>Please note that it's not needed for non-Microsoft compilers
It's not needed for Microsoft compilers either. :icon_rolleyes: I hate how they use that as a default include for generated code. That's one of the reasons I always create an empty project with Visual Studio instead of using the templates.

I do the same thing, I only ever use stdafx.h when including large header files to minimize build time, like when I had a generated header file with about 60000 constant c-strings in it (I was making a scrabble solver and didn't want any loose files :P).

William Hemsworth 1,339 Posting Virtuoso

You shouldn't use a textfile, the user could just simply delete the text file or something. You could create the file in a hidden folder somewhere on the D: drive so the user could not find it, keeping your source code secure.

Nah, a text file is fine, so long as you ensure than any modifying of the file will not be accepted, and an encryption is used. Saving it somewhere else will just make it slightly harder to find, but once it has been found... then what?

William Hemsworth 1,339 Posting Virtuoso

If you are working out the biggest number, you have to start off with the smallest possible value and keep looking for a bigger value.

To find the smallest, you have to do the opposite. Start off with the biggest possible value, and keep looking for a smaller one.

Also it is more efficient to put the for loop inside the if block instead of the other way round, here is the corrected code:

#include <stdio.h>
#include <limits.h>

#define Biggest   1
#define Smallest  0

int FindBiggest(int *arr, int length, int choice)
{
  int i;
  int num = 0;

  if ( choice == Biggest ) {
    for (i = 0; i < length; i++) {
      if ( arr[i] > num ) { // Look for a bigger value
        num = arr[i];
      }
    }
  }

  else if ( choice == Smallest ) {
    num = INT_MAX; // Make it start off with the biggest possible value
    for (i = 0; i < length; i++) {
      if ( arr[i] < num ) { // Look for a smaller value
        num = arr[i];
      }
    }
  }

  return num;
}

int main(void)
{
  int arr[] = {2, 1, 3};
  int length = sizeof(arr) / sizeof(arr[0]); // Works out the length of arr
  int result;

  result = FindBiggest(arr, length, Smallest);
  printf("%d\n", result);

  getchar();
  return 0;
}
William Hemsworth 1,339 Posting Virtuoso

>what the diff between array and just normal i?
It's two completely different things, i is just an integer, array is the element which is located at the index of i.

William Hemsworth 1,339 Posting Virtuoso
#include <stdio.h>

int FindBiggest(int *arr, [B]int length[/B])
{
  int i;
  int num = 0;

  for (i = 0; [B]i < length[/B]; i++) {
    if ( arr[i] > num )
      num = arr[i];
  }

  return num;
}    

int main(void)
{
  int arr[] = {0, 5, 3};
  [B]int length = sizeof(arr) / sizeof(arr[0]);[/B]
  int result;

  result = FindBiggest( arr, [B]length[/B] );
  printf( "%d\n", result );

  getchar();
  return 0;
}

I highlighted the changes, also you were looping through too many elements in the array by using <=.

William Hemsworth 1,339 Posting Virtuoso

Is there any code you aren't posting? what is FIND_NUM and BIGGEST? You can't use sizeof to work out the length of an array passed to a pointer, instead you have to allow the caller to pass the length of the array as a parameter.

William Hemsworth 1,339 Posting Virtuoso

Read the comments on this snippet. [link]

William Hemsworth 1,339 Posting Virtuoso
while(!inFile.eof())

Don't use eof() inside a loop like that, simply do what ArkM suggested and do this:

while ( inFile >> ascii ) {
  cout << (char)ascii;
}

Hope this helps.

tux4life commented: Yes! +7
FaMu commented: thank you +1
William Hemsworth 1,339 Posting Virtuoso

I remember having problems with the wheel scroll, I think the return value is WHEEL_DELTA if the scroll is up, and -WHEEL_DELTA if the scroll is down.

Hope this helps.

William Hemsworth 1,339 Posting Virtuoso

Then a zero value is returned, or am I wrong?

You're wrong. If you pass a NULL pointer to atoi, your program will most lightly crash. If you pass a string such as "12Q34" then it will return 12, as it stops as soon as it reaches the first non-digit character.

William Hemsworth 1,339 Posting Virtuoso

An array is simply a list of objects (in this case, PictureBox is the object).

The code:

// Create an array with 100 picture boxes
array<PictureBox^> ^pBoxes = gcnew array<PictureBox^>( 100 );

makes an array which you can assign 100 PictureBoxs too.


So you could then do...

pBoxes[0] = pBox1;
pBoxes[1] = pBox2;
pBoxes[2] = pBox3;
pBoxes[3] = pBox4;
// ...

If you don't understand that, try researching arrays.

William Hemsworth 1,339 Posting Virtuoso

Try using this code instead:

PictureBox ^pBox = gcnew PictureBox();

pBox->Parent = this; // 'this' is pointing to our form (the parent)
pBox->Location = Point( 240, 210 );
pBox->Size::set( Drawing::Size(30, 30) );
pBox->BackColor::set( Color::Aquamarine );

And as for making an array of PictureBoxs, this code will make an array that can hold 100 of them.

// Create an array with 100 picture boxes
array<PictureBox^> ^pBoxes = gcnew array<PictureBox^>( 100 );

Then you simply have to assign each element, for example:

pBoxes[0] = pBox;

Hope this helps.

arshad115 commented: thanks a lot +1
William Hemsworth 1,339 Posting Virtuoso

If the interest is there, people will find the time. Even working 12 hours a day, every day of the week, and taking a full 8 hours for sleep, that still only adds up to 140 hours. What are your Turkish people doing for the other 28 hours? How much do you want to bet that it's entertainment?

True, it's easy to fit in a bit of extra time. For example, when learning Kanji it's easiest to keep coming back to them and familiarising yourself with them at regular intervals. Even if it's during a short break while watching tv, or before you go to bed, it's definitely possible.

William Hemsworth 1,339 Posting Virtuoso

but do you think you will benefit from learning japanese? it is not a common language, it will help you understand only japanese people. learning spanish could be a better time investmen but most of the spanish speakers also understand english. i plan to learn russian. it is a prevelant language. and learning russian may make you feel special as people around you wont understand what you are speaking :)

Looks awesome on a résumé, people assume Japanese is a hard language (like you did) therefore they think you must be smart. I also watch anime and read manga, not to mention I enjoy it learning the language itself.

William Hemsworth 1,339 Posting Virtuoso

I'm learning Japanese at a slow pace, mostly self-taught. Now than I'm leaving to go to college, I can't have my one japanese lesson per week at high school anymore. I'll probably get private lessons or keep myself motivated and continue learning by myself.

I speak a fair amount of Spanish and in my opinion, Japanese is actually easier to learn (except for the couple thousand of kanji symbols). It's a much more logical language with alot less irregular verbs. Japanese pronunciation is similar to Spanish and isn't as hard to learn as most people think. As for learning how to write Japanese, that just takes pure practise, but I find it fun :)

William Hemsworth 1,339 Posting Virtuoso

zero -- I don't listen to music on the internet.

This doesn't really have anything to do with the internet :)

My winamp says: "4538 items [11 days+22:01:52][24.26gb]"
But I like most of what I have, and never seem to get bored of my music.

William Hemsworth 1,339 Posting Virtuoso

From this post.

i promise here, with my own words, from now on, Narue is just another poster for me, i am not going to mention her, i am not going to PM her saying that Dilettante chocolates reminds me of her because i tent to associate every quality thing with her.. ok you will see that i am a person who keeps his promisses. i promise here, i apologize from you all.

i was really surprised to see a c++ geek who is blonde and cute( i am not allowed to give her name, you know her anyways).

You failed, but... as long as it gave you a nice fuzzy feeling :icon_rolleyes:

William Hemsworth 1,339 Posting Virtuoso

Also William is working on another interesting activity of compression. Could you too tell us more ?

Basically just doing it for fun. I'm making it in C++, and though creating a good compression algorithm is difficult, trying to maintain the folder structure for decompression is also a challenge. So to do this, i've made a class called File, with all the necessary functions to write itself to the main output file (compressed), and a main Compression class which has a tree of all the subfiles to be compressed.

And serkan sendur, I would say this is a pretty decent thread you've started :P

William Hemsworth 1,339 Posting Virtuoso

My own compression algorithm, which works for folders and all subfiles. It's a challenge, but it seems to be working okay at the moment.

William Hemsworth 1,339 Posting Virtuoso

A small game in flash AS2 which allows the user to create their own stages using a grid of different objects. Ill probably upload it to deviantart.com or something when i'm finished. Though I haven't done much recently as I have my exams now. A spanish speaking exam tomorrow which i'm terrified to go into, and about another 10+ big exams within the next week... wish me luck :icon_rolleyes:

tux4life commented: Have luck with it :) +4
William Hemsworth 1,339 Posting Virtuoso

yeah i also think that it is richness to have various languages but if your mother tongue had not been english, you would have suffered like i do. each morning when i wake up, my cache memory appears to be loaded with turkish words, then until i go to office i think in turkish, then somebody calls my name with their own pronounciation, that is the time for me to load cache with english.
that is tiring. i use my left brain lob when i start to speak english, it takes a while to switch to right brain lob.

Well I originally used to speak in Portuguese as I only moved to the UK when I was aged about 5, but I guess that was plenty of time to adapt. In fact, now I can't even remember much Portuguese (but some parts of it come back to me while learning Spanish). Given enough time, you should get used to it.

William Hemsworth 1,339 Posting Virtuoso

>i wish all the people all around the world spoke the same language.
Well, that would just be plain boring. I find different languages interesting and enjoy learning them. Different languages seem to have their advantages, english seems to be a nice phonetic language which IMO has the nicest sounding music. But others sound nice too (like Dragostea Din Tei which sounds nice in romanian but awful in english).

Most languages represent their culture or history and wouldn't be the same without... take chinese or japanese for example, which can be an art in itself. [link]

William Hemsworth 1,339 Posting Virtuoso

You can use the default copy constructor, like this:

struct myObject {
  int data[10];
};

int main() {
  myObject a;

  /* Do stuff to 'a' */

  myObject b( a );

  /* 'b' is identical to 'a' now */

  return 0;
}
tux4life commented: Absolutely right :P +3
William Hemsworth 1,339 Posting Virtuoso

>Just a question though... I don't want to terminate only the function (so I don't want to use return),
>but rather the whole program from within the function. How could I do that without exit()?
Check for errors in the main function, and if one has occurred, close it from there.

bool my_function() {
  /* Code Here */
  
  if ( /* Some Error */ ) {
    return 1;
  }

  return 0;
}

int main() {
  if ( my_function() == 1 ) {
    return 0;
  }

  /* Other Code Here Never Executed */

  return 0;
}
William Hemsworth 1,339 Posting Virtuoso

For some reason the (code=language) tags aren't working today, at least initially. Regular (code) tags are fine though.

Just noticed that in this thread.

William Hemsworth 1,339 Posting Virtuoso

Okay ive had a bash at C++, but i have some errors (c++ pointers confuse me...)

...

Its supposed to be a linked list btw. The addNode and printList methods are what is broken.

Many of those errors are caused by things like illegal indirections and trying to assign values to members when there's no data allocated.

Remember that when you're trying to access members through a pointer, you have to use the -> operator, but that pointer has to have space allocated. For example, in this function:

void LinkedList::addNode(string data)
{
  ListNode newNode(data);
  head.setNext(*newNode); // if head is a pointer, then use the  ->  operator here
}

newNode is not a pointer, so you don't need the * there unless newNode is a valid pointer to a ListNode object.

It would take a while to go through the next 9 errors, so I recommend you revise pointers :icon_cheesygrin:

Hope that helps.

William Hemsworth 1,339 Posting Virtuoso

> Edit:: C++ Beginner's Guide is also a very nice one (you can get a free legal ebook copy here)

I bought that book before the age of 13, and was my first step to becoming the totally awesome programmer you see today :P

William Hemsworth 1,339 Posting Virtuoso

WH: What are your system specs and compiler?

I use VS2005 professional. 2gb ram, running 32-bit Windows Vista.

William Hemsworth 1,339 Posting Virtuoso

WH: You're saying that the code below does not crash on your system?

#include <iostream>
int main() {
    int matrix[512][512][512];
    matrix[0][0][0] = 1;
    std::cout << matrix[0][0][0] << '\n';
}

Assuming 32-bit ints, that's half a gigabyte! Removing the 3rd dimension works for me, but that's only a megabyte.

Works flawlessly :)

Salem commented: Beware the optimiser reducing the simple program to std::cout << 1 << '\n'; +29
William Hemsworth 1,339 Posting Virtuoso

Both compiled fine for me, in fact, even int matrix[512][512][512]; compiled and ran fine for me.

William Hemsworth 1,339 Posting Virtuoso

>I have got to say this .NET stuff is very frustrating.
.NET is very simple compared to the amount of work needed to accomplish the same task in MFC or plain Win32 API.

Post some code and the errors you are getting.

William Hemsworth 1,339 Posting Virtuoso

It's not surprising your program crashes, it's the same as making an array with a size of: 512 * 512 = 262144 elements, or 1048576 bytes. As Ancient Dragon said, you don't have enough stack space.

>Do you think I would have the same problem if I use a vector instead?
A vector allocates dynamic memory, so it will only work if you have enough free dynamic memory - which it should have.

>Do you know how to overcome this problem?
If dynamic memory doesn't work, which I think it will, there's probably another way to approach the problem without having to allocate so much space.

William Hemsworth 1,339 Posting Virtuoso

Yep, you did :-(

William Hemsworth 1,339 Posting Virtuoso

>how do you manage to make all your threads so creepy
So true :icon_rolleyes:

Serkan, seek help to get over your Narue obsession. :icon_cheesygrin: or just keep it to yourself.

William Hemsworth 1,339 Posting Virtuoso

>Just wanted to ask how will it know where to output the text?
If you want to output it to your richTextBox1, simply do this:

richTextBox1->Text = IO::File::ReadAllText( openFile->FileName );

Hope this helps.

William Hemsworth 1,339 Posting Virtuoso
openFileDialog1->FileName

This isn't the file contents, it's the file path. Try something like this.

IO::File::ReadAllText( openFile->FileName )

Hope this helps.

William Hemsworth 1,339 Posting Virtuoso

I have no idea what's causing that error, the button event compiled and ran fine for me.
The only thing I can suggest is, try removing line 84 and see if it works.

William Hemsworth 1,339 Posting Virtuoso

Attach or post the whole code please? It will be faster than figuring this out with small snippets of your code.

William Hemsworth 1,339 Posting Virtuoso

> Could you please explain this part of the code as it is giving me errors:

Don't just copy and paste what I wrote, use your head :) Change the name list to what ever you called your listbox. But I just re-read your question, and realized that's not what you want. But adding the entire contents of a file to one line in a list box doesn't seem logical, but if that's what you need, learn how to open a file and read the data.

Hope this helps.

tux4life commented: Well said :) +2
William Hemsworth 1,339 Posting Virtuoso

I haven't done Windows Forms for one or two years, but with the help of google... I managed to make this small example for you.

System::Void button_Click(System::Object^  sender, System::EventArgs^  e) {
  OpenFileDialog ^openFile = gcnew OpenFileDialog();

  if ( openFile->ShowDialog() == Windows::Forms::DialogResult::OK ) {
    list->Items->Add( openFile->FileName );
  }

}

Search better next time.

rEhSi_123 commented: Very Helpful +1
William Hemsworth 1,339 Posting Virtuoso

hey guys, emm can someone explain fast for me how to get to the programming state where u get a window with some options on the side and can then switch to code i cant remeber how to get there

Huh? o.O if you want a good reply, ask a good question (give details, IDE/Compiler... ect) and explain what you want clearly.

William Hemsworth 1,339 Posting Virtuoso

I'm not entirely sure what it is you're trying to do, posting some code would help (or even a small example). Some other ways to simulate these events is to use mouse_event and keybd_event, as directly sending a message into a windows procedure can sometimes give you a different result to what your expecting. I'm not entirely sure what you mean by "The values for LPARAM and wParam for Keyboard is also easy, but I've no idea how to get the lParam for mouse events."

William Hemsworth 1,339 Posting Virtuoso

You're weird ;)

William Hemsworth 1,339 Posting Virtuoso

>julienne is a nice name by the way, i will name my daughter like that if i have one oneday
Leave Narue alone! :<

William Hemsworth 1,339 Posting Virtuoso

Actually i dont understand why people dont want to show their faces in the forum?
we can take Dani as an example, she is the most preminent one and she does not need to hide her face.

What are you talking about? :) jbennet and I both show our faces. Check our profiles, it's just some people prefer not to have the whole of daniweb know their face.

William Hemsworth 1,339 Posting Virtuoso

Maybe because half DaniWeb members ARE children. How many members posting here are under 21 years old?

Heh, I think i've been pretty mature in this thread :)

William Hemsworth 1,339 Posting Virtuoso

Not bad ;)