Ask Your Question
0

why switch(a) converts char a into int decimal

asked 2020-04-10 09:57:49 -0600

andrei186 gravatar image

the code needs to execute something depending on the .png filename passed as argv[1].

C++ switch does not accept strings, so all the files are named as cN.png where N is a digit between 0 and 9 and this digit is accessed with

string a=argv[1]; 
char aa=a[1];

On filename c6.png line

cout<<"aa="<<a[1]<<"; isdigit(aa)="<<isdigit(aa)<<endl;

returns aa=6; isdigit(aa)=1

yet switch(aa) seems to convert value 6 into its ASCII decimal code 54

I can get round it by replacing case 6 with case 54 in swich. Yet keen to know how to use swich in a more direct way. Perhaps aa should be of a different type?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-04-10 11:27:29 -0600

mvuori gravatar image

You are asking a very basic general C++ programming question (stuff for the first day of a programming course - you should clearly take one), that doesn't belong in this forum.

However, the character has a value of '6' in the character set used, which represents a numeric value of 54. Switch can use any integral or enumerative variable as its argument, including ints and chars. Here, it obviously uses char, and in the switch's case statement the character's value, which can be expressed as '6' or 54.

edit flag offensive delete link more

Comments

Thanks, this is great for I use my good old 1011 pages long and 2 inches thick "How to program C++" by N.M. Deitel and associates (Prentice-Hall 1998) and it says nothing about Switch can use only integral or enumerative variables. Yet now I see my mistake - earlier I tried character's value in case statement, but used 6 in double commas and it failed - thanks again for most valuable hint

andrei186 gravatar imageandrei186 ( 2020-04-10 12:53:45 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-04-10 09:57:49 -0600

Seen: 173 times

Last updated: Apr 10 '20