featured_spectre Posted March 26, 2013 Posted March 26, 2013 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.
Ric_ Posted March 26, 2013 Posted March 26, 2013 I don't know about C++ but I can do it quite easy in PowerShell Get-Random 1
featured_spectre Posted March 26, 2013 Author Posted March 26, 2013 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
LosOjos Posted March 26, 2013 Posted March 26, 2013 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
featured_spectre Posted March 26, 2013 Author Posted March 26, 2013 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).
LosOjos Posted March 26, 2013 Posted March 26, 2013 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: #include #include 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/Nephilim%20Number%20Crunch.zip
featured_spectre Posted March 26, 2013 Author Posted March 26, 2013 Looks good, however it shuts down at the end, before the end number is generated
LosOjos Posted March 26, 2013 Posted March 26, 2013 (edited) 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: //rearrange string cout << "Re-arranged string: "<< sResult.substr(4,2) << sResult.substr(0,2) << sResult.substr(2,2); To: //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 Edited March 26, 2013 by LosOjos
mac_shinobi Posted March 26, 2013 Posted March 26, 2013 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 ??
featured_spectre Posted March 26, 2013 Author Posted March 26, 2013 C++ would be preferred. Now to compile it. Any good compilers that are light?
mac_shinobi Posted March 26, 2013 Posted March 26, 2013 List of compilers - Wikipedia, the free encyclopedia OR Free c++ compilers - C++ Forum I also use the cplusplus site for tutorials : cplusplus.com - The C++ Resources Network C++ Language Tutorial - C++ Documentation
LosOjos Posted March 26, 2013 Posted March 26, 2013 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) 1
featured_spectre Posted March 26, 2013 Author Posted March 26, 2013 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
LosOjos Posted March 26, 2013 Posted March 26, 2013 (edited) 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") Edited March 26, 2013 by LosOjos
tom_newton Posted March 26, 2013 Posted March 26, 2013 Are you absolutely sure you don't want it done in Perl? 1
LosOjos Posted March 26, 2013 Posted March 26, 2013 Alternatively if you're short on time post or PM me your code and I'll compile an exe for you
featured_spectre Posted March 26, 2013 Author Posted March 26, 2013 This can be locked by an admin/mod
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now