Jump to content

Recommended Posts

Posted

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.

Posted

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

Posted

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

Posted (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 by LosOjos
Posted (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 by LosOjos

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...