PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 20, 2022

[FIXED] How to make the conversion from 'int' to 'char' reasonable under -Werror=conversion option? c++11

 July 20, 2022     algorithm, c++, c++17, char, integer     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing