# Help quickly

Show 25 post(s) from this thread on one page
Page 1 of 2 12 Last
• 26th March 2013, 09:58 AM
nephilim
Help quickly
I want to have a program, where I input a 6 digit number, it does a sum to the 6 digit number, then, takes the various digits, and output the nearest whole number (rounded down), then puts the digits in a certain order (EG last 2, first 2, middle 2)

So it would do for argument sake (135791 + 123123)/3 = 086394 OUTPUT 940863

I am time short on this one, and wondered if someone could lend a hand?

Ideally in C++

I will post up later when home my code to date, but as I am at work, it is a bit difficult to do so.
• 26th March 2013, 11:09 AM
Ric_
I don't know about C++ but I can do it quite easy in PowerShell

Code:

`Get-Random`
• 26th March 2013, 11:53 AM
nephilim
HA!

No it is for the company I work for. They are using excel right now to run the calculations etc, but agreed it would be easier to have a program whipped up and done. However I can't seem to get the last bit done where it pulls the digits in groups of 2
• 26th March 2013, 12:02 PM
LosOjos
Quote:

Originally Posted by nephilim
HA!

No it is for the company I work for. They are using excel right now to run the calculations etc, but agreed it would be easier to have a program whipped up and done. However I can't seem to get the last bit done where it pulls the digits in groups of 2

Convert the number to a string?

Converting numbers to strings and string - C++ Articles
• 26th March 2013, 12:06 PM
nephilim
Thats fine, but how would I do it then if it registers as a 5 digit number, as it needs to be 6 (with a 0 at the front).
• 26th March 2013, 12:52 PM
LosOjos
OK just had a play, here's a very basic program which should demonstrate how to do this. there is no error checking, just standard IO to console, but you should be able to adapt it to your needs:

Code:

```#include <iostream> #include <sstream> using namespace std; int main() {     int iStart;     int iCalculated;     string sResult;     ostringstream convert;     cout << "Enter a 6 digit number: ";     cin >> iStart;     //perform calculations     iCalculated = (iStart + 135791) / 3;     cout << "(" << iStart << " + 135791) / 3 = " << iCalculated << "\n";     //convert result to string     convert << iCalculated;     sResult = convert.str();     //prepend with 0's if neccessary     if (sResult.length() < 6)     {         string sZeroes;         sZeroes.append(6 - sResult.length(), '0');         sResult = sZeroes.append(sResult);     }     cout << "String converted and length fixed: " << sResult << "\n";     //rearrange string     cout << "Re-arranged string: "<< sResult.substr(4,2) << sResult.substr(0,2) << sResult.substr(2,2);     return 0; }```
And an exe if you want to see a demo: https://dl.dropbox.com/u/4779199/Nep...r%20Crunch.zip
• 26th March 2013, 01:53 PM
nephilim
Looks good, however it shuts down at the end, before the end number is generated
• 26th March 2013, 02:00 PM
LosOjos
Quote:

Originally Posted by nephilim
Looks good, however it shuts down at the end, before the end number is generated

The end number is simply that last cout that pulls the number pairs out as substrings and prints it to console. If you want that number actually stored (as a string, otherwise you won't have your preceding zeroes, you could change the last part from:

Code:

```//rearrange string     cout << "Re-arranged string: "<< sResult.substr(4,2) << sResult.substr(0,2) << sResult.substr(2,2);```
To:

Code:

```//rearrange string     sResult = sResult.substr(4,2) + sResult.substr(0,2) + sResult.substr(2,2);     cout << "Re-arranged string: "<< sResult;```
Really, how you deal with the final result will depend what it is you actually want to do with it. Will this code be added to an existing C++ program? If so, whack it in a function. If not, you'll have to adapt it to take an integer as a CLI argument then output the result to a file or something where you can read it... happy to help with any of that, just need more specifics :)

EDIT: obviously you wouldn't need all the 'cout' everywhere in the final program, I just put those there to make it obvious how it works
• 26th March 2013, 02:27 PM
mac_shinobi
Does it have to be in C++ or could you not do a HTA in vbscript or use dot net based IDE ie vb.net or c#.net etc ??
• 26th March 2013, 03:13 PM
nephilim
C++ would be preferred. Now to compile it. Any good compilers that are light?
• 26th March 2013, 03:19 PM
mac_shinobi
• 26th March 2013, 03:20 PM
LosOjos
Personally I use CodeBlocks as a nice simple IDE, but if you simply want a compiler then MinGW is popular (assuming you're on a Windows machine)
• 26th March 2013, 03:31 PM
nephilim
Quote:

Originally Posted by LosOjos
Personally I use CodeBlocks as a nice simple IDE, but if you simply want a compiler then MinGW is popular (assuming you're on a Windows machine)

I have downloaded codeblocks, however can't seem to get it to compile into an EXE
• 26th March 2013, 03:34 PM
tom_newton
Are you absolutely sure you don't want it done in Perl? :)
• 26th March 2013, 03:34 PM
LosOjos
Quote:

Originally Posted by nephilim
I have downloaded codeblocks, however can't seem to get it to compile into an EXE

Does it execute if you click "Build and Run" or is it throwing an error?

If it runs, the exe has been created but finding it could be awkward. Easiest way is create a new project, console application, set the path. Then put the code in to "main.cpp" and build. Open up the folder where you created the project, and look in "bin\Debug" to find your exe

EDIT: when you're sure it's working, you should change the build target to "Release" to cut out unnecessary debug symbols which in turn reduces the footprint of the exe (this will be built in "bin\Release")
Show 25 post(s) from this thread on one page
Page 1 of 2 12 Last