Issue
error: conversion from ‘int’ to ‘char’ may change value [-Werror=conversion]
build cmd example:
g++ -std=c++11 test.cpp -o a.out -Werror=conversion
auto index = 3;
char singleChar = 'A' + index; // I want to get A-Z
I hope sigleChar is dynamically assigned. could you pls help me to solve this error report without using switch? How would it be better to write code?
Solution
'A' + index; // I want to get A-Z
would only works for ASCII, not EBCDIC for example.
A more portable solution (and not int to char conversion involved) is array indexing:
char singleChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[index];
Answered By - Jarod42 Answer Checked By - Pedro (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.