466 Posted Topics

Member Avatar for anukavi

Seems like the task could be to use a *bitshift* operation: int i = 1; std::cout << i << std::endl; i << 1; // Shift bits to the left std::cout << i << std::endl; i << 1; // Shift bits to the left std::cout << i << std::endl; This should …

Member Avatar for Lucaci Andrew
0
1K
Member Avatar for chris.vargas.773

This task is all about *operator overloading*. Search for "C++ operator overloading" in your favourite search engine and you'll get lots of examples. You first need to make a `TrashCan` class though, have you done that?

Member Avatar for chris.vargas.773
0
120
Member Avatar for jcrck

You have a few choices. 1. Make an array that's sufficiently big to meet the likely amount of data 2. Dynamically allocate the memory at run-time, using `new` 3. Use `std::vector` to manage the memory allocation for you I'd say that you probably need to use option 2 for this …

Member Avatar for jcrck
0
250
Member Avatar for Riteman

You can use a [icode]stringstream[/icode] for this. Try something like [code] #include <iostream> #include <string> #include <sstream> int main() { for ( int i = 0; i < 10; ++i ){ std::string filenameRoot("file_"); std::stringstream ss; ss << filenameRoot << i << ".txt"; std::cout << ss.str() << std::endl; } return 0; …

Member Avatar for angham kh
0
4K
Member Avatar for rizwan.ali.1656

You can modify your function to take a function as one of the arguments. I find it easiest to add another template parameter to do this: template< typename value_t, typename op_t > value_t reduceArray( value_t myArray[], size_t size, value_t initial, op_t op) { value_t out = initial; for ( size_t …

Member Avatar for ravenous
0
353
Member Avatar for ShEeRMiLiTaNt

Is the array that you pass in to this function as `dynamicArray` allocated using `new[]`? If not this would cause a crash. If you could, it might help if you post a fragment of the code that calls this function. This kind of thing is one reason why you should …

Member Avatar for ravenous
0
99
Member Avatar for shanna62

> ptr is a pointer to first position in array and arr is pointing to where number 33 exists in array. so (ptr-arr) is the offset from begining of the array where 33 exists (probably equal to 2). Almost. You have `ptr` and `arr` the wrong way round in this …

Member Avatar for shanna62
0
327
Member Avatar for Magic681

Making parts of your program into function calls is easy: // Function prototype/declaration // * Needs to be before the function call. // * Ends in a semi-colon // * The 'int' at the start is the "return-type", it can be any type // * the things in the brackets …

Member Avatar for ravenous
0
388
Member Avatar for Dudearoo

Since you mentioned Boost, have you looked at [Boost program_options](http://www.boost.org/doc/libs/1_51_0/doc/html/program_options.html)? It has a nice syntax for parseing these kind of files. Lots of the tutorials on the site are about using it to parse command line arguments, but the is a section about parsing config file [here](http://www.boost.org/doc/libs/1_51_0/doc/html/program_options/overview.html#id2549206). The only thing …

Member Avatar for ravenous
0
189
Member Avatar for Albino

If you're OK using external libraries, then boost has `boost::lexical_cast`, which can be used to do these conversions: std::string s1( "123" ); int i = boost::lexical_cast< int >( s1 ); std::string s2 = boost::lexical_cast< std::string >( i ); If it can't do the conversion, `lexical_cast` throws a `boost::bad_lexical_cast` exception. I …

Member Avatar for Lucaci Andrew
0
188
Member Avatar for muhammad.khan.3576

The maximum value that `rand()` will return is determined by a variable in `cstdlib` called `RAND_MAX`. I think it's compiler/system-dependent, but should be at least 32767. What is it on your system?

Member Avatar for muhammad.khan.3576
0
195
Member Avatar for juliani91

On the static variable part: you have to initialise a static variable to something, so just doing this won't work: class MyClass { public : void AddOne(){ ++myStaticVar; } float Get() const { return myStaticVar; } private : float myStaticVar; }; You will get a compilation error. The variable must …

Member Avatar for ravenous
0
677
Member Avatar for Bubbles26

I think that the question intends that you use a loop to read in and print out the information about the bills. For instance, the bits like this: cout << "Enter the number of One Hundred Dollar Bills you have: "; cin >> bill; cout << '\n'; bills.push_back( bill ); …

Member Avatar for Bubbles26
0
195
Member Avatar for madhusudan.srinivasan

Just a note. Your algorithm isn't particularly efficient. You can see from your loops that this algorithm requires `n1 * n2` iterations. You should consider if you *need* to loop over the whole of `string2` for each element in `string1`? For instance, you can take advantage of the fact that …

Member Avatar for ravenous
0
243
Member Avatar for riccardo-m

It sounds like you need to pass the serial comms object (as a reference or pointer) into the functions that need to use it.

Member Avatar for riccardo-m
0
258
Member Avatar for vikuseth

You could `new` a `JointDetails` object in `main` before you make your other classes, then add a constructor `Analyzer` that takes a pointer to a `JointDetails` and constructors to `Test1` and `Test2` that also take pointers to `JointDetails`. The `Test1` and `Test2` constructors would pass the pointers to `JointDetails` that …

Member Avatar for ravenous
0
152
Member Avatar for breezeonhold

You should be careful not to leak memory. The `Car` pointer that you `new` on line 99 goes out of scope on line 108 without being deleted. This means that you leak a `Car` every time you go round the while loop. In C++ there are so-called *smart pointers* that …

Member Avatar for breezeonhold
0
278
Member Avatar for Despairy

Have you checked how long it takes to just run through the array and add 1 to all the values? I suspect it's not that long. I was recently profiling some code that was doing arithmetic on arrays like this and the compiler-optimised, release code was running over each element …

Member Avatar for Despairy
0
299
Member Avatar for streetalex310

I don't know about decoding it, but you can easily see that there is some structure in this data. If you insert line-breaks in the right places you can see that there are two main sections to the data that feature different repeating motifs: 4B 50 7B F1 F4 F5 …

Member Avatar for ravenous
0
144
Member Avatar for TheEroteme

One way that I can immediately see is that you only need to look for factors up to `number / 2`, instead of `number` like you currently do. That should give you a factor of 2 speed up in total run-time Another thing would be to make your search multi-threaded.

Member Avatar for Kwetal
0
180
Member Avatar for lmd141

You need to take that call to `print_pdb` out of the loop that it's in (move it after line 59). `print_pdb` trys to loop over `simbox_TotalNparticles` elements and print them out, but they don't all exist yet. You allocate your array of pointers to doubles on line 29. You then …

Member Avatar for ravenous
0
258
Member Avatar for Geowil

From Phorce's original example: vector<string> names(2); // Define a vector with a size of 2 elements names.push_back("Phorce"); names.push_back("Bob"); names.push_back("Jimmy"); /* Note how I have 3 elements, in an array, it would force a memory error if not handled. */ This doesn't do what is stated. When you do `vector< string …

Member Avatar for Geowil
0
256
Member Avatar for Microno

I think you should re-evaluate your approach to parsing the equation. It would be greatly simplified and more understandable if you take a more object-oriented approach. If you don't know how to use classes in C++, look it up; it's really useful :) So, a polynomial is a collection of …

Member Avatar for ravenous
0
175
Member Avatar for Remzz

You could use `boost::lexical_cast` to try and convert the string to a number and catch the `boost::bad_lexical_cast` exception that's thrown if the conversion can't be done. This feels a bit weird though, as I generally try not to use exceptions for flow control. I guess you should wrap it in …

Member Avatar for Remzz
0
210
Member Avatar for Peppercat101

From this line: `./oemsort(_Z11printToFilePiiPc+0x5b)[0x40be51]` It looks like the problem is in the function printToFile. Looking at line 212 of your code, you pass in `argv[3]` as the file name. If you check the command line that you invoked the program with: `mpirun -n 5 ./oemsort 2 unsorted.txt sorted.txt` Then, `argv[3]` …

Member Avatar for ravenous
0
832
Member Avatar for WolfShield

>Does #include <wingdi.h> not include those libraries? No, only the function declarations are generally in the header file. The declarations are enough for the compiler to compile the code that is calling the function. However, when the linker (that's what the `ld` part of the error is referring to) tries …

Member Avatar for ravenous
0
584
Member Avatar for majimboo04

You need two things to do this. Firstly, you need to make a `for` loop that runs over all the numbers. Secondly, you need to use the `%` operator on each. `%` gives you the "remainder" of an integer division. If you divide an integer by 2 and look at …

Member Avatar for ravenous
0
87
Member Avatar for tensity

This isn't really an answer to your question, but if I were you, I'd just read in the whole line in one go and then have a separate piece of code to split the line into its individual fields. If you think about it, they are logically separate operations: 1. …

Member Avatar for tensity
0
217
Member Avatar for harirock
Member Avatar for lxXTaCoXxl

In gereral it's a really bad idea to use macros with very short names, like `n`. It removes the posibility of ever using that symbol as a variable name in your code. I think it's generally a good idea to try and use longish upper-case names for macros. Additionally, if …

Member Avatar for sepp2k
-4
271
Member Avatar for glao

To make a simple periodic-boundary-condition-like access to an array, you can do size_t size = 5; double array[ size ]; // ... Fill the array with data std::cout << "enter index:" << std::endl; int index; std::cin >> index; double value = array[ ( size + ( index % size ) …

Member Avatar for glao
0
3K
Member Avatar for mochiboo5

This is not directly related to your question, but it will make things easier for you. From your code, it looks like you're representing your polynomial in the order that it's typically written down, e.g. a*x^3 + b*x^2 + c*x + d This is fine, but it's easier to work …

Member Avatar for DavidB
0
3K
Member Avatar for Alochai

Some answers (not to all the questions though)... SnipRobotDlg.cpp: Line 12: You should always try and initialise variables to a known value. In this case, after `get_body` has returned, you should check `pElementBody != NULL`. Line 22: You should check that "Snip" is actually found in the string. Line 34: …

Member Avatar for Alochai
0
162
Member Avatar for 111100/11000

You don't need to use a `goto` here. You should remove it from your program. I think, with a bit of tweaking, you can replace the `goto` with a `while` loop. I think that labels have the same kind of scoping rules as other variables and functions in C++, so …

Member Avatar for ravenous
0
146
Member Avatar for kirenemook12

As mike_2000_17's comprehensive reply said, anything in the Arduino family of things is pretty easy. I've used freeduino in the past. You get the board for about $30. There's some soldering to do, but not too much and all of the surface-mounted componented are done for you, so you don't …

Member Avatar for mike_2000_17
0
274
Member Avatar for ScottFountaine

I know this isn't an answer to your question, but why do you need to do all this adding of spaces and such? It sounds like it could be a bit of a hack? If you don't mind, what is it that you're actually trying to accomplish with the `find_keyword` …

Member Avatar for ScottFountaine
0
130
Member Avatar for np complete

I've never used it to flush the input stream, but I have used it to flush the output stream. Say you want to produce output like: Doing something... done! Then I'd have code like this: std::cout << "Doing something... "; fflush( std::cout ); /* Some code that takes some time …

Member Avatar for ravenous
1
156
Member Avatar for Sprewell184

This code builds fine for me. Which compiler/platform are you using? There are a couple of changes that I'd make though. For example, You don't need to do find the length of the number in the way that you do on line 32. In fact, I don't think that there's …

Member Avatar for Sprewell184
0
578
Member Avatar for Sendy Hipo

You probably don't need the debug libaries, unless you actually expect to be debugging Qt itself. If you just want to use Qt in your program, then you can use the non-debug version of Qt. You'll still be able to debug your program, it's just that you won't be able …

Member Avatar for Sendy Hipo
0
358
Member Avatar for lil_bit

Er, this looks *very* similar to the [this example](http://www.cplusplus.com/reference/clibrary/cstdio/remove/) on cplusplus.com

Member Avatar for mrnutty
0
211
Member Avatar for Chuckleluck

You could set up some enums like this: enum EDirection { eNone = 0x0000, eNorth = 0x0001, eSouth = 0x0010, eEast = 0x0100, eWest = 0x1000, eNorthEast = eNorth | eEast, eNorthWest = eNorth | eWest, eSouthEast = eSouth | eEast, eSouthWest = eSouth | eWest }; And the, check …

Member Avatar for Chuckleluck
0
634
Member Avatar for muditvijay

One way you could do it would be to make a class that stores the details, something like: class CPersonDetails { public : CPersonDetails( const std::string& name, unsigned class, std::string number ); private : std::string m_name; unsigned m_class; std::string m_phoneNumber; }; You could then overload `operator<` for this class and …

Member Avatar for Cross213
0
129
Member Avatar for rdx05

this isn't doing what i think you think it's doing. `array[i]` is an array of 5 strings, not an array of characters. There's no default way to insert arrays of things into a streaam, so the compiler will complain.

Member Avatar for rdx05
0
207
Member Avatar for Labdabeta

Visual studio has a special file called `autoexp.dat` that tells the debugger how to format things when it's presenting them to you. You can add your own definitions to the file, so you can have a custom presentation of pretty much any structure in the debugger. It's one of the …

Member Avatar for Labdabeta
0
306
Member Avatar for jillianking8751

Classes generally have two aspects to them: Things they *know* and things they *do*. The first is generally taken care of by the class *member variables*, while the second is the job of the class's *member functions* (sometimes called *class methods*). The task you posted already states that you're going …

Member Avatar for jillianking8751
0
232
Member Avatar for monmondiesel

It looks like you're used to programming in C a bit more than C++. As Deceptikon mentioned, you should write some constructors to handle copying and other object instantiation events. This will greatly improve the level of *encapsulation* in your program. In general, it's desirable for an object to handle …

Member Avatar for deceptikon
0
4K
Member Avatar for кодирование

[QUOTE=WaltP;1745749]Add another [iCODE]getline()[/iCODE] just before the [I]return[/I] in main....[/QUOTE] To add to what WaltP said, I like to make a small function called something like [icode]WaitForExit()[/icode] which encapsulates this: [code] #include <iostream> void WaitForExit() { std::cout << "Press ENTER key to exit...." << std::endl; std::cin.get(); } int main() { WaitForExit(); …

Member Avatar for georgeduroa
0
175
Member Avatar for XodoX

Er, I don't think that any of these examples do what you want them to do. If you take Lucaci Andrew's code for example. If you have a file in the format shown in the example, then the program will try to open a file called "1," and then fail, …

Member Avatar for Lucaci Andrew
0
270
Member Avatar for wesk18

Er, do you use MS Outlook for your email? If so it has built in features to allow you to do this kind of thing with templates, see [here](http://office.microsoft.com/en-us/outlook-help/create-an-e-mail-template-HA001086500.aspx)

Member Avatar for ravenous
0
72
Member Avatar for n1csaf3

Just a small point but, in C++, you don't need to put the `struct` keyword before the typename when you're using your structs. Also, I don't think that you need to do all the casting that you're doing. I think that this would be fine: struct AIstruc_word { // Set …

Member Avatar for ravenous
0
156

The End.