0

Here is some code that I am trying to use. Why wouldn't this work the way I am assuming it should?

here is a sample of vector<string> mine:
***
*2*
*1*
so mine[1][1] = 2.

// In a function that has passed in vector<string> &mine
int dist[2000][100];
cout << mine[1][1] << endl;   // This correctly displays 2
cout << dist[1][1] << endl; // This correctly displays 0 (not initiallized yet)
dist[1][1] = mine[1][1];
cout << mine[1][1] << endl;   // This correctly displays 2
cout << dist[1][1] << endl; // This incorrectly displays 50
3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by kylcrow
0

Well 50 is the decimal value of the ASCII character '2'
If you still want a '2', then try a char array and not an int array.

Chars are just small ints, but the magic of seeing '2' rather than 50 only works if you store them in char variables.

0

>cout << dist[1][1] << endl; // This correctly displays 0 (not initiallized yet)
Which means either you got extremely lucky, your snippet is misleading and dist has static storage duration, or you're relying on a compiler extension that automagically fills arrays with 0. The following would be more correct:

int dist[2000][100] = {0};

>cout << dist[1][1] << endl; // This incorrectly displays 50
It looks correct to me. You just weren't expecting this behavior. For your homework, I want you to figure out the integer value of the character '2'. You can do this by looking at an ASCII table (which matches your result), or by writing a simple test program that casts the character to an integer.

0

Using a char array did let me do that assignment correctly for the character 2,
But I also have to do integer addition, like this

dist[2][1] = dist[1][1] + maze[2][1];

2 + 1 = 3 is what I need,
this is giving me character 2(50) + character 1(49) = character c (99)

This is resulting in the character c (decimal 99) when I need it to be decimal 3.
I feel like I need to use the atoi() function or something...

0

Subtract '0' from '2' and see what happens. Then do it for '0' through '9' to verify that it works across the board.

0

Thanks a lot, seems I was so caught up on using dijkstra's that I forgot my basic char manipulation. Solved.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.