- Apr 5, 2013
- 175
- 0
- 0
Hey guys. I am trying to write up a little piece of software where you input 1 number, it does a set of functions, and outputs an answer.
This is my code (I have added various bits and pieces to change the size of the utility box, and input number being 1 colour, output being another).
Function required is as follows
User inputs random number (we will call this N)
Function 1 is 987654-N = X
Function 2 is (first 4 digits of X) * X = Y
Function 3 is Last 4 digits of Y = End Result.
Can anyone help me with this and where I am going wrong? (I realise I have not done the function 3 completely yet, but the coding escapes me, and when building this, it halts on the multiplication section).
This is my code (I have added various bits and pieces to change the size of the utility box, and input number being 1 colour, output being another).
Function required is as follows
User inputs random number (we will call this N)
Function 1 is 987654-N = X
Function 2 is (first 4 digits of X) * X = Y
Function 3 is Last 4 digits of Y = End Result.
Can anyone help me with this and where I am going wrong? (I realise I have not done the function 3 completely yet, but the coding escapes me, and when building this, it halts on the multiplication section).
Code:
//Number Generator
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <windows.h>
using namespace std;
void SetColor(int value){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), value);
}
struct console
{
console( unsigned width, unsigned height )
{
SMALL_RECT r;
COORD c;
hConOut = GetStdHandle( STD_OUTPUT_HANDLE );
if (!GetConsoleScreenBufferInfo( hConOut, &csbi ))
throw runtime_error( "You must be attached to a human." );
r.Left =
r.Top = 0;
r.Right = width -1;
r.Bottom = height -1;
SetConsoleWindowInfo( hConOut, TRUE, &r );
c.X = width;
c.Y = height;
SetConsoleScreenBufferSize( hConOut, c );
}
~console()
{
SetConsoleTextAttribute( hConOut, csbi.wAttributes );
SetConsoleScreenBufferSize( hConOut, csbi.dwSize );
SetConsoleWindowInfo( hConOut, TRUE, &csbi.srWindow );
}
void color( WORD color = 0x07 )
{
SetConsoleTextAttribute( hConOut, color );
}
HANDLE hConOut;
CONSOLE_SCREEN_BUFFER_INFO csbi;
};
//Set Console Size
console con( 25, 10);
string ToUpper(string str)
{
string converted;
for(short i=0; i < str.size(); ++i)
converted += toupper(str[i]);
return converted;
}
bool run()
{
int iStart;
int iCalculated;
int iCalculated2;
int iCalculated3;
string sResponse;
bool isRunning = false;
bool dbg = false;
string sResult;
ostringstream convert;
SetColor (15);
cout << "Enter Number: ";
SetColor (12);
cin >> iStart;
//perform calculations
iCalculated = (987654 - iStart);
if (dbg)
cout << "(987654 - " << iStart << ") = " << 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);
}
if (dbg)
cout << "String converted and length fixed: " << sResult << "\n";
//rearrange string
sResult = sResult.substr(0,4);
//perform second set of calculations
cin >> sResult.substr * sResult = iCalculated2;
if (dbg)
cout << "(sResult.substr * " << iCalculated2 << ") = " << iCalculated3 << "\n";
SetColor (15);
cout << "New Number: ";
SetColor (10);
cout << iCalculated3 << "\n";
SetColor (15);
cout << "Run again? (Y/N)" ;
cin >> sResponse;
sResponse = ToUpper(sResponse);
if (sResponse=="Y" || sResponse=="YES")
isRunning=true;
return isRunning;
}
int main()
{
bool isRunning = true;
while (isRunning)
{
isRunning = run();
}
}