invalid conversion from 'int' to main()::letterType'?

Jul 22, 2009 at 10:43pm
I am getting this error when compiling my program. I dont even know if it will work. But i just want it to compile to see what happens. Can someone tell me whats up? The problem is on line 20.

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
56
57
58
59
60
// input a letter
// output a pyramid with letters according to that letter

// build the pyramid based on the letter
#include <iostream>
#include <cstdlib>
#include <cctype>

using namespace std;

const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int main()
{
    enum letterType {A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z}userLetter;
    int spaceCount, spaceLimit, innerloop,loopcount;
    char inputLetter;

    cin >> inputLetter;
    userLetter = toupper(inputLetter);
    
    
    spaceCount = 1;
    spaceLimit = int(userLetter);
    
    do
    {
                 
                 
                 while(spaceCount <= spaceLimit) // insert spaces
                 {
                                  cout << " ";
                                  spaceCount++;
                 }
                 spaceLimit--;
                 spaceCount = 1;
                 innerloop = 1;
                 
                 while(innerloop <= loopcount)
                 {
                                 cout << char(alphabet[innerloop-1]);
                                 innerloop++;
                 }
                 
                 while(innerloop >= 1)
                 {
                                 cout << char(alphabet[innerloop-1]);
                                 innerloop--;
                 }
                 
                 cout << endl;
                 loopcount++;
                 
    }while(loopcount <= int(userLetter));
    
    system("pause");
    
    return 0;
    
}
Last edited on Jul 22, 2009 at 10:47pm
Jul 22, 2009 at 10:52pm
The enum item A is in no way connected to the integral value 'A' (65 in ASCII).
If you want them to be, change line 15 to:
enum letterType {A='A',B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z}userLetter;
and line 20 to:
userLetter = (letterType)toupper(inputLetter);
Jul 22, 2009 at 11:01pm
from what i understood from enumeration is that the const values would be A=0 B=1. my program kind of depends on that. if you look at the while portion of my do while loop im trying to use it as a control variable.
Jul 22, 2009 at 11:04pm
Could i just take the user input and assign it straight into an enum variable?
Last edited on Jul 22, 2009 at 11:11pm
Jul 22, 2009 at 11:27pm
No! but you can convert it with a cast like the static_cast<int>(value) the static_cast changes the base type of value to the one in the < > tags
or the version everyone knows (int)value
Jul 23, 2009 at 12:04am
Thanks. Helios is right. It seems like its taking the ASCII value of the letter and storing it. Program is not working. Probably have to re-write. Thanks for the input. I think i'm just going to move on to the next program.
Jul 23, 2009 at 2:02am
I didnt give up on it and have been running it and finding problems with the logic. Like not initializing loop values. If anyone wants to see the messed up pyramid i've created and maybe try and fix it heres the 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
56
57
58
59
60
61
62
63
64
65
66
67
// input a letter
// output a pyramid with letters according to that letter

// build the pyramid based on the letter
#include <iostream>
#include <cstdlib>
#include <cctype>

using namespace std;

const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int main()
{
    enum letterType {A=0,B=1,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z}userLetter;
    int spaceCount, spaceLimit, innerloop,loopcount;
    char inputLetter;

    cin >> inputLetter;
    userLetter = (letterType)toupper(inputLetter); // supposed to take user input and convert to constant value
    
    
    spaceCount = 1;
    spaceLimit = int(userLetter); // supposed to assign int from enum where a=0, b=1
    loopcount = 1;
    
    
    
    do
    {
                 
                 
                 while(spaceCount <= spaceLimit) // insert spaces
                 {
                                  
                                  cout << " ";
                                  spaceCount++;
                 }
                 
                 spaceLimit--;
                 spaceCount = 1;
                 innerloop = 1;
                 
                 while(innerloop <= loopcount) // first half of the pyramid
                 {
                                 
                                 cout << char(alphabet[innerloop-1]); 
                                 innerloop++;
                 }
                 
                 while(innerloop <= 1) // second half of the pyramid
                 {
                                 cout << char(alphabet[innerloop-1]);
                                 innerloop--;
                 }
                 
                 cout << endl;
                 loopcount++;
                 
    }while(loopcount <= int(userLetter)); // supposed to assign int from enum where a=0, b=1, but is getting assigned ASCII value
    
    system("pause");
    
    return 0;
    
}
Jul 23, 2009 at 3:27am
On line 20, before casting to the enum, subtract 'A' from the result of toupper(). That way, 'A' will be converted to zero, 'B' to 1, and so on, and the cast to the enum won't turn out garbage.
Topic archived. No new replies allowed.