Hello, I am writing a small program that'll calculate the imaginary number i and say you're given this:
1 2
23
i
The code will automatically shorten it down to -i
However I added a small part that will detect the length of the digit and push the output over a little so it looks nice. But when I try to run it, the spacing is never changed.
#include <iostream>
#include <sstream>
#include <string>
usingnamespace std;
int main()
{
char restart = 'y';
while ( ( restart == 'y' ) || ( restart == 'Y' ) )
{
int i = 83;
cout << "\nType the exponent over i: ";
cin >> i;
ostringstream convert;
convert << i;
string intLength;
intLength = convert.str();
int i2 = i;
i2 %= 4;
string answer;
switch ( i2 )
{
case 0:
answer = "1";
break;
case 1:
answer = "i";
break;
case 2:
answer = "-1";
break;
case 3:
answer = "-i";
break;
default:
answer = "???";
break;
}
int length = intLength.length();
if ( length = 1 )
cout << "\n " << i << " " << i2
<< "\ni = i = " << answer;
elseif ( length = 2 )
cout << "\n " << i << " " << i2
<< "\ni = i = " << answer;
elseif ( length = 3 )
cout << "\n " << i << " " << i2
<< "\ni = i = " << answer;
cout << "\nRestart? (y/n): ";
cin >> restart;
}
}
And the expected output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Type the exponent over i: 1
1 1
i = i = i
Restart? (y/n): y
Type the exponent over i: 11
11 3
i = i = -i
Restart? (y/n): y
Type the exponent over i: 111
111 3
i = i = -i
Restart? (y/n): n
And the actual output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Type the exponent over i: 1
1 1
i = i = i
Restart? (y/n): y
Type the exponent over i: 11
11 3
i = i = -i
Restart? (y/n): y
Type the exponent over i: 111
111 3
i = i = -i
Restart? (y/n): n
Thanks in advance,
NarWhat
**EDIT** wow. How did I mess this up... the 'if' statements from line 40 to line 48 needed '==', not '='. That's my problem... Thanks anyways ;P