daviddoria 334 Posting Virtuoso Featured Poster

I would step back for a second and ask why you are writing your own bitmap writing function? I bet there are bunches of libraries that include such a thing. Then you can spend your time writing new code!

daviddoria 334 Posting Virtuoso Featured Poster

Seems like a simple for loop should do the trick, no? Show us what you've tried and we can help if you get stuck.

David

daviddoria 334 Posting Virtuoso Featured Poster

Kanoisa is right, you are returning the variable by reference AND by value, which is not at all necessary.

Also, in your latest post, you are calling the function with only one argument:

incr10(num);

when you have declared it as taking two!

int incr10(int& num, int& value)

David

daviddoria 334 Posting Virtuoso Featured Poster

While this is a pretty short program, I think it would still help to break it down in to functions. Some ideas for functions would be:

void GuessLetter(char letter); //the main logic - every user input should trigger this function
bool ContainsLetter(string word, char letter); //determine if 'letter' is contained in 'word'
void ReplacePlaceholder(string &word, char letter); //replace an '*' with 'letter'
bool IsDone();

Those are just examples, but the point is that then your code is very readable, like this:

do
{
  cin >> letter;
  GuessLetter(letter);
}while(!IsDone())

void GuessLetter(char letter)
{
  if(ContainsLetter(word, letter)
  {
  ReplacePlaceholder(word, letter);
  }
}

Good luck,

David

daviddoria 334 Posting Virtuoso Featured Poster

Welcome to DaniWeb!

(Please use code tags when posting code, it makes it much easier for us to read :) )

What you have there looks good. To add a second variable, simply do this:

int incr10(int& num, int& var2)

You didn't specify what this second variable is for, but hopefully that will get you started down the right path.

Good luck!

David

daviddoria 334 Posting Virtuoso Featured Poster

What do you mean by "does not work"? What is an example triple of input? The current output? The expected output?

I'd recommend making a small demo program to test the functions without any user interaction - that is, hard code three values and see if it gives you the correct result. Separating the input state from the computation stage is always a good idea to at least make sure you're looking in the right place for the problem!

daviddoria 334 Posting Virtuoso Featured Poster

I don't understand why current_loc even needs to be a pointer? Why not just have each player have a member object of your location enum? Maybe I missed something?

daviddoria 334 Posting Virtuoso Featured Poster

Hi Mike - welcome to DaniWeb!

Can you detail the problem more for us? What is wrong? Are there are any compiler errors? Give an example input, the current output, and the expected output.

Looking at this code:

Rational obj1,obj2,obj3;
	{
		obj1 = numerator,denominator;
		obj2 = numerator,denominator;
		obj3 = t;
	}

The first line declares three instances of the Rational class. The braces are unnecessary.

As far as I know, this syntax is not valid:

obj1 = numerator,denominator;

what are you trying to do here?

Good luck,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

I use VTK for everything 3D (vtk.org). You ask for c++ and VB in the same sentence though - which would you prefer?

daviddoria 334 Posting Virtuoso Featured Poster

My suggestion (same as Mike's) is to convert everything to a CMakeLists.txt file. It will take some initial time, but it will make everything MUCH easier.

daviddoria 334 Posting Virtuoso Featured Poster

You should read a line at a time and use this:
http://programmingexamples.net/index.php?title=CPP/Strings/Split

You should have a Data class and overload the input operator to handle parsing the dates (with the /'s etc)

daviddoria 334 Posting Virtuoso Featured Poster

The problem is that in this line:

TicTacToe game(char X, char E);

You cannot have the type again - it needs to be:

TicTacToe game(X, E);

However, now you will get a "X is not declared" error. You either need to make X public and use game.X or do something else.

Good luck,

Dave

daviddoria 334 Posting Virtuoso Featured Poster
daviddoria 334 Posting Virtuoso Featured Poster

Can you explain the problem that you are having? If you have a specific problem, if you could trim the code down to < 20 compliable lines that demonstrates the problem, that would me much easier for us to look at than just dumping 200 lines on us!

Dave

daviddoria 334 Posting Virtuoso Featured Poster

With first one, it looks like you need to compare to 'largest' each time. That is,

if(num2 > largest)
	largest = num2;
	if(num3 > largest)
	largest = num3;
	if(num4 > largest)
	largest = num4;

For the second one, I would suggest using more descriptive variable names so you can keep them clear. It looks like you are increasing 'num' each time, but I'd say you need to be increasing 'count' until it gets to 'num'.

Give those a shot and let us know if it works.

Dave

empror9 commented: thanks *_^ +1
daviddoria 334 Posting Virtuoso Featured Poster

tesuji is correct. You can only switch on int and char.

I have demonstrated the difference between char and string here:
http://programmingexamples.net/index.php?title=CPP/Switch#Switch.cpp

Dave

daviddoria 334 Posting Virtuoso Featured Poster

There are "disassemblers", that will go from an executable to assembly, but you'll never get back to c++ :(

daviddoria 334 Posting Virtuoso Featured Poster

You may find more help here: http://www.eclipse.org/forums/

daviddoria 334 Posting Virtuoso Featured Poster

Good question.

Place the code between code tags, like this: [ code] here [ /code]

(without the spaces between the brackets and the words)

daviddoria 334 Posting Virtuoso Featured Poster

I'm not familiar with wxWidgets, but I suggest you weight your options before diving in. I have chosen Qt. There are other options too, particularly if you're developing for Windows. If you've already chosen wxWidgets, maybe someone else here can help.

daviddoria 334 Posting Virtuoso Featured Poster

wxWidgets is a GUI system. Here is an example: http://www.wxwidgets.org/docs/tutorials/hello.htm

You have to run

wx-config --libs
wx-config --cppflags

to get the settings from your system. Then you have to setup your build. I use cmake, so this is how to do it in cmake:

cmake_minimum_required(VERSION 2.6)

PROJECT(Basics)
INCLUDE_DIRECTORIES(/usr/include/wx-2.8)
INCLUDE_DIRECTORIES(/usr/lib/wx/include/gtk2-unicode-release-2.8/)

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__WXGTK__")
ADD_EXECUTABLE(Basics Basics.cpp )
TARGET_LINK_LIBRARIES(Basics wx_gtk2u_richtext-2.8 wx_gtk2u_aui-2.8 wx_gtk2u_xrc-2.8 wx_gtk2u_qa-2.8 
wx_gtk2u_html-2.8 wx_gtk2u_adv-2.8 wx_gtk2u_core-2.8 wx_baseu_xml-2.8 wx_baseu_net-2.8 wx_baseu-2.8 )

What are you trying to do?

Dave

daviddoria 334 Posting Virtuoso Featured Poster

No no, don't do that. There is no reason to use a global variable here. It is very bad practice. Your setuparray() function should return the vector<string> :

std::vector<std::string> setuparray()
{
std::vector<std::string> yourArray;
yourArray.push_back("one");
yourArray.push_back("two");
return yourArray;
}

Then in main:

std::vector<std::string> yourStringArray = setuparray();

Dave

daviddoria 334 Posting Virtuoso Featured Poster

nbaztec, he is inputting those variables before using them, so any initialization would be overridden anyway.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

It is not going to stop asking for input until you enter 9:99. while (hournum != 9 && minnum != 99) Dave

daviddoria 334 Posting Virtuoso Featured Poster

Welcome to DaniWeb!

(First, please use code tags when you post code!)

You should use a std::vector<std::string>; Give that a shot and let us know if you have a specific problem.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

That line is the constructor. It says to use a default value of 1.0 if nothing is passed. If something is passed, the : radius(r) sets the member variable 'radius' to the value of 'r'. This is called an 'initializer list'.

Dave

nbaztec commented: Yep. :) +1
daviddoria 334 Posting Virtuoso Featured Poster

You really shouldn't be scared of STL containers. They will save you tremendous amounts of time (in exactly cases like this!) Rather than thinking of it as "learning maps and arrays at the same time" you should see this as an opportunity not to get stuck in the array way of thinking of things (as I had done for years before I was introduced to STL containers!) and consider it a good time to learn about containers/data structures. There is not really a hierarchy (array before set, before map, etc) - they are all on equal ground, just have different uses.

If I couldn't use a map, my second option would be this:

1) Store the input array in an std::vector.
2) Copy the input vector into a std::set. This removes all duplicate elements
3) Create a vector the same size as the set. This is where you will store the count of each unique element
4) Iterate over the set and use std::count (from STL <algorithm>) on the original vector, searching for the current element in the set
5) Store the result in the ith position of the vector you created in step 3.

This is exactly what a map would be doing :)! The algorithm would be much more involved if you only wanted to use arrays (vectors), as you'd have to do the uniqueness checking and counting manually.

Good luck,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

Hi Andrew,

Unfortunately I don't know the answer to your question. However, I urge you to make the titles of your threads as descriptive as possible. For example, I came to look at "A little problem", but quickly found out that it was related to the Windows API and I don't know anything about that. If you named it something like "Determine which key was pressed with WH_KEYBOARD hook", I would have known to leave it to the Windows guys :)

Good luck,

David

daviddoria 334 Posting Virtuoso Featured Poster

I was hoping for something that I could compile and poke around at :)

daviddoria 334 Posting Virtuoso Featured Poster

I would indeed try to make a "20-line" demo of the problem that you can post for us.

daviddoria 334 Posting Virtuoso Featured Poster

Here is how to iterate through the whole map:

void IterateOverWholeMap()
{
  std::map <std::string, int> myMap;
  myMap["testone"] = 111;
  myMap["testtwo"] = 222;
  
  std::map<std::string,int>::iterator iter = myMap.begin();
    
  for(; iter != myMap.end(); ++iter) 
  {
   std::cout << iter->first << " " << iter->second << std::endl;
  } 
}

Here are some more notes I have:

void SetValue()
{
	//create a map
	std::map <std::string, int> MyMap;
	
	//create a mapping from "test" to 111
	MyMap["test"] = 111;
}

void SetAndGetValue()
{
	//create a map
	std::map <std::string, int> MyMap;
	
	//create a mapping from "testone" to 111
	MyMap["testone"] = 111;
	
	//create an iterator
	std::map<std::string,int>::iterator iter;

	//try to find "testone"
	iter = MyMap.find("testone");
	
	//we assume "testone" was found, so output the value that "testone" maps to
	std::cout << iter->second << std::endl;
}

void ResetValue()
{
	std::string Name = "testone";
	std::map <std::string, int> MyMap;
	MyMap[Name] = 111;
	
	std::map<std::string,int>::iterator iter;

	iter = MyMap.find(Name);
	
	double FirstValue = iter->second;
	std::cout << FirstValue << std::endl;
	
	MyMap[Name] = FirstValue + 1;
	
	double SecondValue = iter->second;
	std::cout << SecondValue << std::endl;
	
}

void NonExistentValue()
{
	std::map <std::string, int> MyMap;
	MyMap["testone"] = 111;
	
	std::map<std::string,int>::iterator iter;

	iter = MyMap.find("testone");
	
	if(iter == MyMap.end())
	{
		std::cout << "Element not found!" << std::endl;
	}
	else
	{
		std::cout << "Element found!" << std::endl;
		std::cout << iter->second << std::endl;
	}
}

Be sure to vote here: http://daniweb.uservoice.com/forums/62155-general/suggestions/830529-create-a-wiki-to-catalog-answers-and-examples-?ref=title so we can catalog examples like this!

Dave

daviddoria 334 Posting Virtuoso Featured Poster

They are really, really useful! Try not to get caught up in the template/iterator terminology. The main idea is the key->value associations. In this case, the key is the number you want to know how many times has occurred, and the value is the number of times it has occurred. When you insert something into a map, if it is already in the map, nothing will happen. If you want to be able to map a key to multiple values, there is a std::multimap.

Let us know if you have any questions -

Good luck!

Dave

daviddoria 334 Posting Virtuoso Featured Poster

That is exactly what a map does, except it doesn't waste space with numbers that have count = 0.

daviddoria 334 Posting Virtuoso Featured Poster

You should use the count() function from STL algorithm

#include <algorithm>

start = Vect.begin() ; 

end = Vect.end() ;
result = count(start, end, value) ;

(There is also a sort() function, but you shouldn't need it here)

If you wanted to do it more manually you should use an std::map.

Be sure to vote for cataloging answers like this, I've definitely answered this one before :) http://daniweb.uservoice.com/forums/62155-general/suggestions/830529-create-a-wiki-to-catalog-answers-and-examples-?ref=title

Dave

daviddoria 334 Posting Virtuoso Featured Poster

Please post

File1.cpp

//file1.cpp here

File2.cpp

//file2.cpp here

So the next guy knows where you are so far.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

You absolutely should not #include "file2.cpp" . Since you're using visual studio the linking should be setup automatically for you - but I'll let a VS user reply with the details.

daviddoria 334 Posting Virtuoso Featured Poster

Just #include <cstdio> in file2.cpp.

daviddoria 334 Posting Virtuoso Featured Poster

This compiles fine for me:

#include <iostream>
#include <cstdio>

int main()
{
 printf("teste");
 return 0;
}

Dave

daviddoria 334 Posting Virtuoso Featured Poster

That is because you have not #include <cstdio> Also, you should pretty much never include a .cpp file.

daviddoria 334 Posting Virtuoso Featured Poster

Right, I stuck a "perl" in front of the file name inside the system() call.

A batch file would also work (cd to the directory you want and then "perl file.pl"), but it doesn't seem necessary here.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

What is wrong with

system("perl C:\test\perl\a.pl");

?

daviddoria 334 Posting Virtuoso Featured Poster

I'm glad you found your solution. Please mark the thread as solved.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

First, I'm not sure if you actually copied and pasted, but in your A class, public; should be public: .

Second, you need semicolons after the closing brace of all of the class definitions.

The main problem is that you are defining m_b in the constructor of A (a local variable), then trying to access it from a member function of A. You need to define m_b as a member variable of A (outside of any functions, including the constructor).

Good luck,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

I have a feeling that most of us geeks here are going to need a better explanation of what these rankings (0-3) mean, and a bit of an English explanation of what is going on before we can look at your code simulation.

daviddoria 334 Posting Virtuoso Featured Poster
daviddoria 334 Posting Virtuoso Featured Poster

OpenCV is just as heavy as VTK, so take your pick. But ImageMagick would be much more lightweight than VTK, so that is probably the best suggestion.

daviddoria 334 Posting Virtuoso Featured Poster

I guess I didn't post a link? haha. Here is the code you'll need.

#include <vtkSmartPointer.h>
#include <vtkJPEGReader.h>
#include <vtkImageData.h>
 
int main ( int argc, char *argv[] )
{
  //parse command line arguments
  if ( argc < 2 )
    {
    std::cout << "Usage: " << argv[0]
              << " InputFilename(.jpg)" << std::endl;
    return EXIT_FAILURE;
    }
 
  vtkstd::string inputFilename = argv[1];
   
  // Read JPG file
  vtkSmartPointer<vtkJPEGReader> reader =
    vtkSmartPointer<vtkJPEGReader>::New();
  reader->SetFileName ( inputFilename.c_str() );
  reader->Update();
  vtkSmartPointer<vtkImageData> image = reader->GetOutput();
  
  // Get dimensions of the image
  int* dims = image->GetDimensions();
  
  // Access elements of the image
  for (int row = 0; row < dims[1]; row++)
    {
    for (int col = 0; col < dims[0]; col++)
      {
      //double* pixel = static_cast<double*>(image->GetScalarPointer(row,col,0));
      //std::cout << "(" << row << " , " << col << ") = (" << pixel[0] << " , " << pixel[1] << ")";
    
      unsigned char* pixel = static_cast<unsigned char*>(image->GetScalarPointer(col,row,0));
      std::cout << "(" << row << " , " << col << ") = (" << int(pixel[0]) << " , " << int(pixel[1]) << ")" << std::endl;
      }
    }
    
  return EXIT_SUCCESS;
}

I could have row and col reversed, you'll have to check it out.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

These are very "heavy" libraries. That is, they are DEFINITELY overkill for just reading images, but unless someone recommends something more "lightweight", it will get the job done. The one I am most comfortable with is VTK. You can download it from here

http://vtk.org/VTK/resources/software.html
(direct link: http://www.vtk.org/files/release/5.6/vtk-5.6.0-win32.exe)

Once it is installed, you will have to link to vtkHybrid. Then you should be able to use the code in the example I sent above. I will actually fix that example to show you how to access individual pixels. I'll let you know when that is done.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

You absolutely cannot read jpg files with iostream! jpeg is a lossy format which means you need the inverse of the jpeg compression algorithm in order to read it! BMP may be possible, but you're crazy to read it yourself! My suggestion would be to use a library that has image reading capabilities. You could use VTK, VXL, and probably OpenCV to read a variety of image formats.

Good luck,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

Great, I'm glad it's working. Let me leave you with some words of advice.

It's a small program (< 1000 lines) and we'll never be modifying it in the future so it's not a big deal.

EVERY time I have said something like that, I have later said "Dang, I should have done this better/figured this out the first time!". I try to build a database of examples out of every little project so I have organized code snippets to refer to when I come across the same problem or type of situation in the future.

Good luck,

Dave