C++ Using enum

Okay, so I'm a bith confused on how to do this.
My program syntax is as follows.

User enters a string
String is validated for correct characters
After each character is validated, minus 1 from that characters count

The last part is what I'm not sure how to do.

I need that if the user enters A, and A has a starting value of 18, then after each A, int A--. Now I realize that I could use a switch statement for this, but I'm dealing with 48 characters. That's a lot of statments.

So how can I do it so that when the user enters A, the computer knows to take away 1 from int A's value?
I was thinking this might be able to be done with an enum, but I don't know how if it is possilbe.

Please also note that several of the values will start with the same number, so I'll be wantiing to use this method: a=b=c=d=18;

Thanks!
Use arrays.

1
2
3
4
5
6
7
8
9
10
int counts[ 48 ];

// Use for loop() to initialize all to 18 (or whatever)

// counts[ 0 ] is for 'A'
// counts[ 1 ] is for 'B'
// etc.

// when you read a character, you have to compute its index.
// for example, ch - 'A' == 0 if ch == 'A', 1 if ch == 'B', etc. 

I though about doing that, but the ASCII values are scattered, thus, I need to know how to convert A to 0 and , to 31 and " to 35. I'm thorwing out random values there, but still.

It seems like I'd have to do a multi-dimensional array, one side having the ASCII, the other having the value. Which as you know, ends up being a lot of code!
Then use a std::map<char, int>.

I've never learned maps yet. Would you happen to have a good tutoria, not reference, on hand? If not, I'll attempt to find one.
ASCII values are not "scattered". They are very conveniently located in organized, contiguous blocks. 'A'..'Z' is ASCII 65..90.

You will find your task easiest if you avoid reordering the characters and simply use the ASCII order. http://www.asciitable.com/

You may want to ignore control codes and the space, making your first character '!', or ASCII 33.
1
2
char c = 'A';
int index = c - '!';


If you really want to reorder the character codes, then you'll have to use a lookup table. A std::map has exactly that function.

Good luck!
For your usage, you can get by with using operator[]:

std::map<char, int > m;

m[ 'A' ] = 18;
--m[ 'A' ];
cout << m[ 'A' ];

They aren't hard to use or learn to use, particularly in your case because you don't
need much more than the above.
The ones that are valid are scattered. I have:

A-Z
, - :
And then
32
! 33
" 34
$ 36
& 38
' 39
? 63
ยข 155

So as you see, semi-scattered.


They aren't hard to use or learn to use, particularly in your case because you don't
need much more than the above.


Well, knowing absolutely nothing about them doesn't help much, but, I guess I get to look them up! :D
Alternatively, now that I think about, why don't you just use an array after all.

Since unsigned char has the range 0-255, you could just make your array 256 elements wide:

 
int counts[ 256 ];


You're storing extraneous data at that point, but it won't hurt anything.
Well, I would only need to go as high as 155, but still, that seems like a lot of extra code, but then a map seems about the same if not more.

So should I do a map, a new array, or make all my exising arrays multi-dimentional?
Topic archived. No new replies allowed.