Problem outputting arrays

Hi all.

I wrote this piece of code to test a function I would be using in a larger program. Basically, there's a function that takes a character and converts it to an integer (a=1, b=2, etc.), and after that, in the main method, I define a string and, using the function I previously defined, convert the whole string to an array of integers. I then try to print that array to the screen, but as of now the output I get is this:
0x22fe60

What am I doing wrong?

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
#include <iostream>
#include <string>
#include <cstring>
#include <list>
using std::cout;
using std::cin;
using std::string;

int charToInt(char character)
{
    int charAsInt;
    
    if (character == 'A') charAsInt = 1;
    else if (character == 'B') charAsInt = 2;
    else if (character == 'C') charAsInt = 3;
    else if (character == 'D') charAsInt = 4;
    else if (character == 'E') charAsInt = 5;
    else if (character == 'F') charAsInt = 6;
    else if (character == 'G') charAsInt = 7;
    else if (character == 'H') charAsInt = 8;
    else if (character == 'I') charAsInt = 9;
    else if (character == 'J') charAsInt = 10;
    else if (character == 'K') charAsInt = 11;
    else if (character == 'L') charAsInt = 12;
    else if (character == 'M') charAsInt = 13;
    else if (character == 'N') charAsInt = 14;
    else if (character == 'O') charAsInt = 15;
    else if (character == 'P') charAsInt = 16;
    else if (character == 'Q') charAsInt = 17;
    else if (character == 'R') charAsInt = 18;
    else if (character == 'S') charAsInt = 19;
    else if (character == 'T') charAsInt = 20;
    else if (character == 'U') charAsInt = 21;
    else if (character == 'V') charAsInt = 22;
    else if (character == 'W') charAsInt = 23;
    else if (character == 'X') charAsInt = 24;
    else if (character == 'Y') charAsInt = 25;
    else if (character == 'Z') charAsInt = 26;
    
    return charAsInt;
}

int main()
{
    string myStr = "abcdef";
    int ints[myStr.size()];
    int i = 0;
    
    while (i < myStr.size())
    {
          ints[i] = charToInt(myStr[i]);
          ++i;
    }
    
    cout << ints;
    return 0;
}
Oops.

You can't just output an array like that. You will need a while or for loop for that output as well.

-Albatross
int ints[myStr.size()];

That will result in an error when compiling. Arrays must be declared with a constant size.
@Archaic:
I just deleted all my compilation tools so I could later update them, so I can't verify this statement, but I'm pretty sure that myStr.size() returns a value that doesn't echo back through time and change itself.

Okay, I'm just kidding. But seriously, C++ would be quite dull if you couldn't dynamically allocate stuff.

-Albatross
Archaic is correct. You must use the heap to dynamically allocate memory:

 
int ints = new int[myStr.size()];


Arrays must be declared with a constant size like so:

 
int ints[10];


You MUST use constant values.
Okay. I'll be checking that in a little bit.

*Why do I insist on upgrading to GCC 4.5? 4.2.1 is what Apple offers and it worked for the longest time...*

Another detail @jarke: Your function is... overblown. Sorry, but there's another way of doing this that results in shorter and faster code. Characters can be converted directly to integers and back. This is a table of values that each character represents:
http://www.danshort.com/ASCIImap/

Just subtract 64 from your character to get your int and you're ready to roll.

-Albatross

If I compile this:
1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;
int main()
{
	int var = 10;
	char Array[var];
	cin.get();
}


I get:
 
error C2057: expected constant expression
I said I'd check, but it's good to know that for at least one compiler it doesn't work.

EDIT: I need some coffee. NOW.

-Albatross
Last edited on
Thanks for all of the help, everybody :)
Topic archived. No new replies allowed.