William Hemsworth 1,339 Posting Virtuoso

40 Seconds on first go :P

William Hemsworth 1,339 Posting Virtuoso

That is a well known link for learning the Win32 API. For your original question, there's not much more you could need that's not already on that page.

William Hemsworth 1,339 Posting Virtuoso

It's all here.

William Hemsworth 1,339 Posting Virtuoso

Either change the style of the window, or add this to your windows procedure:

case WM_GETMINMAXINFO:
  {
    MINMAXINFO *mmi = (MINMAXINFO*) lParam;
    mmi->ptMinTrackSize.x = mmi->ptMaxTrackSize.x = 500; // Width
    mmi->ptMinTrackSize.y = mmi->ptMaxTrackSize.y = 500; // Height
  }
  break;

Hope this helps.

William Hemsworth 1,339 Posting Virtuoso

Ahihihihi.. you must stick a container into your chin to catch those saliva..

Ahhh :confused: Please stop with that childish laugh.

William Hemsworth 1,339 Posting Virtuoso
William Hemsworth 1,339 Posting Virtuoso

>what is advantages of main() function.
Without it, there wouldn't be a program.

http://wwwx.cs.unc.edu/~sparkst/howto/cpp_main.php

William Hemsworth 1,339 Posting Virtuoso

hmmp...To understand reality is not the same as to know about outward events. It is to perceive the essential nature of things. The best-informed man is not necessarily the wisest. Indeed there is a danger that precisely in the multiplicity of his knowledge he will lose sight of what is essential. But on the other hand, knowledge of an apparently trivial detail quite often makes it possible to see into the depth of things. And so the wise man will seek to acquire the best possible knowledge about events, but always without becoming dependent upon this knowledge. To recognize the significant in the factual is wisdom.

Link Try thinking up your own instead of going to a wisdom quotes website, or at least quote and give credit ;)

William Hemsworth 1,339 Posting Virtuoso

This question has been asked many times. There's a few ways to do this, an easy way is to simply fill up the array with unique integers, then shuffle it like this:

#include <stdio.h>

int main() {
  int arr[10];
  
  int i;
  for (i = 0; i < 10; ++i)
    arr[i] = i;

  for (i = 0; i < 10; ++i) {
    int *a = &arr[ i ];
    int *b = &arr[ rand() % 10 ];
    
    int temp = *a;
    *a = *b;
    *b = temp;
  }

  for (i = 0; i < 10; ++i)
    printf( "%d ", arr[i] );
}

Hope this helps.

Salem commented: Yes, shuffling sounds good. +28
William Hemsworth 1,339 Posting Virtuoso

Or, if you're willing to spend a little, it's definitely worth buying yourself a decent book on C++. See this thread.

William Hemsworth 1,339 Posting Virtuoso

>BTW, your solution does not work for a string of only spaces.
Does for me.

>No one said it was particularly difficult; we were just trying to help without posting the answer.
Well, the poster seems to be tackling this the hard way, I guess it would help to provide an easier solution / technique.

William Hemsworth 1,339 Posting Virtuoso

I think yes.. only human knows the value of making sense & trying to meet - of making sense.. some beings may make sense w/out their conscious they does..

You're forgetting the universe is a rather large place :icon_rolleyes: In my opinion - no, with so much unexplored, I think there is a very high chance of more intelligent life within our universe. This book will really get you thinking about it, if you read it :D

William Hemsworth 1,339 Posting Virtuoso

I really don't see why this has to be so hard.

Just loop through each character, find the start and end of each word, then increment the word count.
I guess this is the general idea of what you want:

int wordCount(char *text) {
  int count = 0;

  for (int i = 0; text[i]; ++i) {
    // Find start of word
    while ( isspace(text[i]) )
      ++i;

    // Find end of word
    while ( text[i] && !isspace(text[i]) )
      ++i;

    // Increase word index
    if ( !isspace(text[i-1]) )
      ++count;
  }

  return count;
}

Hope this helps.

William Hemsworth 1,339 Posting Virtuoso

"When you have eliminated the impossible, whatever remains, however improbable, must be the truth"

William Hemsworth 1,339 Posting Virtuoso

Well, I was just judging your programming skills by how clueless you seemed to be in your question :icon_cheesygrin:
And yes, I think you will find it a little hard to implement in your own scripting language... but good luck :-)

William Hemsworth 1,339 Posting Virtuoso

mouse_event is used to emulate mouse events, not to detect them. If you want to be able to detect mouse clicks anywhere (even if your window doesn't have focus), you will have to use something like windows hooks. If your using the Windows API and just want to be able to detect a mouse click within your window, you can start with a Win32 Tutorial - then try and understand Windows Procedures. However, to do this in a console window, it's a little different, here's an example:

#include <windows.h>
#include <iostream>

int main()
{
  HANDLE hStdin; 
  DWORD cNumRead; 
  INPUT_RECORD irInBuf[1];

  if ( HANDLE(hStdin = GetStdHandle( STD_INPUT_HANDLE )) == INVALID_HANDLE_VALUE )
    return 1;

  while ( true )  { 
    if (! ReadConsoleInput( hStdin, irInBuf, 1, &cNumRead) )
      return 1;

    for (DWORD i = 0; i < cNumRead; ++i)
      if ( irInBuf[i].EventType == MOUSE_EVENT &&
           irInBuf[i].Event.MouseEvent.dwEventFlags != MOUSE_MOVED ) {
        if ( irInBuf[i].Event.MouseEvent.dwButtonState & FROM_LEFT_1ST_BUTTON_PRESSED ) {
          std::cout << "Left Click.\n";
        }
      }
  }

  return 0;
}

Sorry for the uncommented code, but either way, I think that is a little advanced for your level of programming.

Hope this helps.

William Hemsworth 1,339 Posting Virtuoso

Read this before posting. Seems nobody ever does.

William Hemsworth 1,339 Posting Virtuoso

I'm just as confused as you are, as expected with that mess.

William Hemsworth 1,339 Posting Virtuoso

Yukk :icon_eek: I don't even like bacon, so I doubt i'd eat that.

William Hemsworth 1,339 Posting Virtuoso

If you're using windows, here's a pretty straightforward solution to retrieving the console buffer size.

#include <windows.h>
#include <iostream>

int main()
{
  CONSOLE_SCREEN_BUFFER_INFO csbi;

  int ret = GetConsoleScreenBufferInfo(
    GetStdHandle( STD_OUTPUT_HANDLE ),
    &csbi );

  if ( ret ) {

    std::cout << "Console Buffer Width: " << csbi.dwSize.X << std::endl;
    std::cout << "Console Buffer Height: " << csbi.dwSize.Y << std::endl;

  }

  std::cin.ignore();
  return 0;			
}

Hope this helps.

William Hemsworth 1,339 Posting Virtuoso

Add this message handler to your windows proc:

case WM_PAINT:
  {
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint( hwnd, &ps );
    TextOut( hdc, 0 /* X */, 0 /* Y */, "Blah de blah de blah", 20 /* Number of chars */);
    EndPaint( hwnd, &ps );
  }
  break;

Should be enough to get you started - though google would have been even better.

William Hemsworth 1,339 Posting Virtuoso

I doubt it.

William Hemsworth 1,339 Posting Virtuoso

You need to move the part of the code which is displaying the grid out of the first for loop scope. Heres the simplest way for you to print the grid.

void displayMatrix(size_t numwords, std::ifstream &inFile, string *wordsPtr) {
  for (size_t i = 0; i < numwords; ++i) {
    inFile >> wordsPtr[i];
    cout << "Line #" << i << ": " << wordsPtr[i] << '\n';
  }
  for (int y = 0; y < 4; y++) {
    for (int x = 0; x < 4; x++) {
      cout << wordsPtr[y * 4 + x] << '\t';
    }
    cout << "\n";
  }
}

Hope this helps.

William Hemsworth 1,339 Posting Virtuoso

I don't get that error, but i'm not entirely sure what it is you're trying do on this line.

cout << "found in:" << words[i] << words[j] << endl;

If you want to display the column and row of the word found, simply print the index of the word like this:

for (int i = 0; i < 4; ++i) {
  for (int j = 0; j < 4; j++) {
    if ( words[i][j] == "0" ) {
      cout << "0 found in:  x: " << i << "\ty: " << j << endl;
    }	
  }
}

Hope this helps.

William Hemsworth 1,339 Posting Virtuoso

Quite a few errors here.


1 - You're missing a semi-colon after the curly brace at the end of your Matrix class.

class Matrix {
public:
  Matrix(size_t numwords) { };
  ~Matrix() {};

  void displayMatrix(size_t numwords) {
    for (size_t i = 0; i < numWords; ++i) {
      inFile >> wordsPtr[i];
      cout << "Line #" << i << ": " << wordsPtr[i] << '\n';
    }
  }
}[B]; // Fergot semi-colon here.[/B]

2 - Variables are case sensitive in C++, therefore, numwords is not the same as numWords as you have done in your displayMatrix member function.

void displayMatrix(size_t numwords) {
  for (size_t i = 0; i < [B]numWords[/B]; ++i) { [B]// Change to numwords[/B]
  inFile >> wordsPtr[i];
  cout << "Line #" << i << ": " << wordsPtr[i] << '\n';
}

3 - Both inFile and wordsPtr are only accessible in the main scope. You can fix this in a couple of ways, one way would be to make these variables global (accessible anywhere), or better - to add the parameters to the displayMatrix function:

void displayMatrix(size_t numwords, [B]std::ifstream &inFile, string *wordsPtr[/B]) {
  for (size_t i = 0; i < numWords; ++i) {
    inFile >> wordsPtr[i];
    cout << "Line #" << i << ": " << wordsPtr[i] << '\n';
  }
}

And in your main function, call the displayMatrix member like this:

newMatrix.displayMatrix( [B]numWords, inFile, wordsPtr[/B] );

You also have a parameter in the Matrix constructor which does nothing, so it may as well be removed.

…
William Hemsworth 1,339 Posting Virtuoso

Could that question be any more vague? You're going to have to post much more detail before you can expect a reply of any help.

William Hemsworth 1,339 Posting Virtuoso

Sour Cream & Onion Pringles :icon_cool:

William Hemsworth 1,339 Posting Virtuoso

It's an easy problem, just remember to split it up into parts.


>The program should ask the user how much the pallet weights by itself
First, make a program.

#include <iostream>

int main() {
}

Now, allow the user to input the weight of the pallet.

#include <iostream>

int main() {
  float palletWeight;
  std::cout << "Enter pallet weight: ";
  std::cin >> palletWeight;
}

There, that's one step done.

Now, try the next step. The same as I have just shown you. But this time..."with the widgets stacked on it". Make another variable with the type float, perhaps called totalWeight, then allow the user to input the total weight using std::cin.

Hope this helps.

William Hemsworth 1,339 Posting Virtuoso

I use VS2005, and I prefer it out of all the compilers that I've use in the past.

William Hemsworth 1,339 Posting Virtuoso

Hey David A. Newbie, use code-tags in future.

William Hemsworth 1,339 Posting Virtuoso

A portable way to retrieve the file size :icon_razz:

// Returns number of bytes in a file
int GetFileSize(char *file) {
  // Open file
  std::ifstream f(file, std::ios_base::binary | std::ios_base::in);

  // Make sure it's opened correctly
  if ( !f.good() || f.eof() || !f.is_open() ) {
    return 0;
  }

  // Beginning of file
  f.seekg(0, std::ios_base::beg);
  std::ifstream::pos_type begin_pos = f.tellg();

  // End of file
  f.seekg(0, std::ios_base::end);

  // Return the difference
  return static_cast<int>(f.tellg() - begin_pos);
}
William Hemsworth 1,339 Posting Virtuoso

To make things simpler, you can read the data in as a 1D array (pointer), but still be able to access the data through a 2D array. Like this:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
  //number of rows and cols
  string words[4][4];

  // Pointer to first element of words (acts as 1D array)
  string *wordsPtr = &words[0][0];

  //read in file
  ifstream inFile( "data.txt", ios::in );

  // Calculate number of words to retrieve
  size_t numWords = sizeof(words) / sizeof(string);

  for (size_t i = 0; i < numWords; ++i) {
    inFile >> wordsPtr[i];
    cout << "Line #" << i << ": " << wordsPtr[i] << '\n';
  }

  return 0;
}

Hope this helps.

AdRock commented: exactly what i wanted +1
William Hemsworth 1,339 Posting Virtuoso

>Some people might remember
Might remember?! you got me hooked on those games for weeks :icon_eek:

And, a nice story :icon_surprised: Took a while to read though.

William Hemsworth 1,339 Posting Virtuoso

You are an Organized Lifelong Learning Self-Improver. :)

William Hemsworth 1,339 Posting Virtuoso

Even after formatting this code, I can't make much sense of it. Just to get it to compile was hard enough, as you've used many non-standard functions. Here is the code I have (green parts are what I added to allow it to compile)

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>

void draw_line(int col, int row);
void show_score();
void add_segment();
void setup_level();
int randomize(int i);
void speed(int sec);

void gotoxy(int x, int y) {
  static HANDLE hOut = GetStdHandle( STD_OUTPUT_HANDLE );
  COORD c = {x, y};
  SetConsoleCursorPosition( hOut, c );
}

/* constants */
const int maxrow = 20,
          maxcol = 75;

const int snake_start_col = 5,
          snake_start_row = 5;

const char up_key    ='w',
           down_key  ='s',
           left_key  ='a',
           right_key ='d';

/* global variables */
int score, snake_length;
char screen_grid[maxrow][maxcol];
char direction = right_key;

struct snake_segment {
  int row, col;
} snake[50];

int main()
{
  setup_level();
  draw_line(1, 1);

  /* Variable declarations within main() only */
  char keypress = 0;

  do /* restart game loop */
  {
    score = 0;

    do /* main loop */
    {
      /* If key has been hit, then check it is a direction key - if so, change direction */
      if ( GetAsyncKeyState(VK_LEFT)  ) keypress = left_key;  else
      if ( GetAsyncKeyState(VK_RIGHT) ) keypress = right_key; else
      if ( GetAsyncKeyState(VK_UP)    ) keypress = up_key;    else
      if ( GetAsyncKeyState(VK_DOWN)  ) keypress = down_key;


      /* Add a segment to the end of the snake */
      add_segment();

      /* Blank last segment of snake */
      /* ... and …
William Hemsworth 1,339 Posting Virtuoso

>I have already write something!
If you have some code, then post it so we can help.

William Hemsworth 1,339 Posting Virtuoso

I think the author goes a little far with the 'beating your kids' part, but as Ancient Dragon said, a spank isn't too bad. Even though I obviously don't have kids yet... sometimes I think it's just the best solution to showing them that you're serious :icon_rolleyes: Especially after watching a parent being shouted at by their kids, and giving in to them, it's supposed to be the other way round :icon_exclaim:

William Hemsworth 1,339 Posting Virtuoso

In VS2005, right-click on the toolbox, click on the menu item 'Choose Items', go to the tab 'COM Components', and check the 'Windows Media Player' check box. You can then drag that control into your form application. You will need to use google to figure out how to use this control properly, as I doubt many people here have the answer to that :P

Hope this helped.

William Hemsworth 1,339 Posting Virtuoso

I would probably try and solve this by reading all the data into an array of chars, and then manually splitting up the text. You should have more control over whats happening if you do this. See my code snippet. As for converting to an int, it's no big task, you can either use std::stringstream, or use the C function atoi (which I would probably use as it's easier).
You can do as niek said, split up all the text into lines, and then split up the lines to retrieve the data you need.

Hope this helps.

William Hemsworth 1,339 Posting Virtuoso

Do one step at a time, and it's simple.

>Create an application

#include <iostream>

int main()
{
  // Code here
}

>remember it is possible to make arrays of structs

#include <iostream>

struct Student {
  std::string name;
  int id;
  int grade;
};

int main()
{
  // Create five students
  Student students[5];
}

See how easy it is at one step at a time? Try and continue by yourself, if you get stuck, we're here to help.

Next step... try to allow the user to input the students details.

William Hemsworth 1,339 Posting Virtuoso

The structure of the program should look something like:

#include <iostream>
#include <string>

int main() {
  std::string escape = "x";
  std::string choice;

  do {
    // Get input
    std::getline( std::cin, choice );

    // Code here

  } while (choice != escape);
}

Also, part of your code is incorrect.

// Math Choice Variables
int calcMinimum();
int calcMaximum();
int calcMean();
int calcStdDev();

You can't define functions inside a function, so you need to move these prototypes outside main.

Hope this helps.

William Hemsworth 1,339 Posting Virtuoso

I found this short one hilarious, not quite sure why :D. http://uk.youtube.com/watch?v=N4J73AnRE5A

William Hemsworth 1,339 Posting Virtuoso

>The most efficient way for swapping whole numbers is <snip xor swap>
>because xor is an assembly level instruction and its much faster than any move operation.
An empirical test disproves your claim. The swap using a temporary is noticeably faster on at least one compiler (Microsoft C++) given any optimization level.

#include <climits>
#include <ctime>
#include <iostream>

void swap_temp ( int& a, int& b )
{
  int temp = a;
  a = b;
  b = temp;
}

void swap_xor ( int& a, int& b )
{
  a ^= b;
  b ^= a;
  a ^= b;
}

int main()
{
  int a1 = 10, a2 = 20;
  int b1 = 10, b2 = 20;
  std::clock_t start;

  std::cout<< a1 <<"\t"<< a2 <<'\n';
  std::cout<< b1 <<"\t"<< b2 <<'\n';

  swap_temp ( a1, a2 );
  swap_temp ( b1, b2 );

  std::cout<< a1 <<"\t"<< a2 <<'\n';
  std::cout<< b1 <<"\t"<< b2 <<'\n';

  std::cout<<"swap_temp: ";
  start = std::clock();
  for ( unsigned i = 0; i < UINT_MAX; i++ )
    swap_temp ( a1, a2 );
  std::cout<< static_cast<double> ( std::clock() ) - start / CLOCKS_PER_SEC <<'\n';

  std::cout<<"swap_xor: ";
  start = std::clock();
  for ( unsigned i = 0; i < UINT_MAX; i++ )
    swap_temp ( b1, b2 );
  std::cout<< static_cast<double> ( std::clock() ) - start / CLOCKS_PER_SEC <<'\n';
}

I think I just noticed a mistake :icon_wink: You're comparing swap_temp for both tests, not quite sure why one takes longer than the other though?

William Hemsworth 1,339 Posting Virtuoso

I realize I was wrong about the data type, they should in fact be float's, my mistake :icon_redface:

To display the wage for each employee, simply loop through each one, and print the value of both multiplied together.

#include <iostream>
#include <typeinfo>
using namespace std;

int main()
{
  int employees;

  cout << "Please Enter The Amount Of Numbers You Want To Process: ";
  cin >> employees;

  float *hoursworked = new float[employees];
  float *payrate = new float[employees];

  for (int i = 0; i < employees; i++) {
    cout << "\nPlease Enter How Many Hours The Employee " << (i+1) << " Has Worked: ";
    cin >> hoursworked[i];

    cout << "Please Enter The Payrate Of Employee " << (i+1) << ": ";
    cin >> payrate[i];

    while ( hoursworked[i] < 0 )//keep looping until a valid employee amount is entered
    {
      cout << "Invalid Amount Of Employees Entered, Please Try Again.\n";
      cin >> hoursworked[i];
    }
  }

  cout << "\n\n";

  for (int i = 0; i < employees; i++) {
    cout << "Wages for employee " << (i+1) << ": " << (hoursworked[i] * payrate[i]) << '\n';
  }

  delete[] hoursworked, payrate;

  cin.ignore();
  cin.ignore();
}

Hope this helps.

William Hemsworth 1,339 Posting Virtuoso

Use code tags, dont just colour in your code green.

> int hoursworked[], payrate[]; What is this supposed to do? This does not make an array with an unlimited size if that's what you wanted.

You need to replace those arrays with dynamic ones as you dont know what size they are going to be (or you could use an std::vector). You also dont need the float data type for these arrays, they should be int. The code should look something more along the lines of:

#include <iostream>
#include <typeinfo>
using namespace std;

int main()
{
  int employees;
  float hourscompleted = 0.0f;

  cout << "Please Enter The Amount Of Numbers You Want To Process: ";
  cin >> employees;

  int *hoursworked = new int[employees],
      *payrate     = new int[employees];

  for (int i = 0; i < employees; i++) {
    cout << "Please Enter How Many Hours The Employee " << (i+1) << " Has Worked:";
    cin >> hoursworked[i];
    cout << "Please Enter The Payrate Of Employee " << (i+1) << ":";
    cin >> payrate[i];

    while ( hoursworked[i] < 0 )//keep looping until a valid employee amount is entered
    {
      cout << "Invalid Amount Of Employees Entered, Please Try Again";
      cin >> hoursworked[i];
    }
  }

  delete[] hoursworked, payrate;
}

Hope this helps.

William Hemsworth 1,339 Posting Virtuoso

Hehe, pretty cool :icon_cheesygrin:
I 95% of the time use google to check my spelling, as it's just plain awful.

I found this one funny.
http://graphjam.files.wordpress.com/2009/01/things-i-worry-about.gif

William Hemsworth 1,339 Posting Virtuoso

A simple solution.

int balance;

cout << "Balance : ";

if (cin.peek() == '$')
  cin.ignore();

cin >> balance;

Hope this helps :)

William Hemsworth 1,339 Posting Virtuoso

You can only initialize an array like that, otherwise you have to assign the string to each array element seperatly like this:

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

class desu {

public:
  string ok[4];

} s;

int main()
{
  s.ok[0] = "one";
  s.ok[1] = "two";
  s.ok[2] = "three";
  s.ok[3] = "four";

  cout << "Blah blah";
  return 0;
}

To do it by initialization, you do this:

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

int main()
{
  string ok[4] = {"one", "two", "three", "four"};
  return 0;
}

Hope this helps.

William Hemsworth 1,339 Posting Virtuoso

That is a windows only solution to clearing the screen, if you're using linux, you should either use system("clear") or use a portable library like ncurses.

William Hemsworth 1,339 Posting Virtuoso

The application begins at the main function, so you can't assign a value to a variable outside of a function unless it's initialization. Here, you have two options.

#include <iostream>
using namespace std;

class desu {
public:
  int lol;
} s;

int main()
{
  [B]s.lol = 5;[/B]
  cout << "Write some generic stuff here";
  return 0;
}

Or...

#include <iostream>
using namespace std;

class desu {
public:
  int lol;

  [B]desu(int val) {
    lol = val;
  }[/B]
} [B]s( 5 )[/B];

int main()
{
  cout << "Write some generic stuff here";
  return 0;
}

Hope this helps.