Spacing is Off in XTerm?

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.

Here's the full code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <sstream>
#include <string>
using namespace 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;
        else if ( length = 2 )
            cout << "\n " << i << "   " << i2
                 << "\ni   = i  = " << answer;
        else if ( 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

Fixed code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <sstream>
#include <string>
using namespace 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;
        else if ( length == 2 )
            cout << "\n " << i << "    " << i2
                 << "\ni   = i  = " << answer;
        else if ( length == 3 )
            cout << "\n " << i << "    " << i2
                 << "\ni    = i  = " << answer;
        else
            cout << "\n " << i << "    " << i2
                 << "\ni     = i  =" << answer;
        cout << "\n\nRestart? (y/n): ";
        cin >> restart;
    }
}
Last edited on
Topic archived. No new replies allowed.