0

I have a program that takes a string. Using the check() function that calculate the sum of all the value in the string of integers, I make additional computations, aka ss. The issue comes when I try to convert ss, which is an int, into a char, c. When I try to print out the newly converted value, nothing prints out on the console, not even an error message.

I have tried using static_cast<char>(ss), and it won't work. Yet when I try to print out the ss value, I get it to print it out.

Source Code

void sum(string input) { int s = check(input); int ss = (s * 9) % 10; char c = ss; cout << "val is: " << c << endl; } int main() { string x = "7992739871"; sum(x); return 0; } 

Can someone explain what I might be doing wrong and how it can be fixed?

9
  • Use the debugger to see what is the value of the int before you cast it to a char and see what it corresponds to in the ASCII table. This way you will know if the value that you are casting to a char is visible or not. Commented Jan 3, 2020 at 1:48
  • Print a char and you will get the symbol encoded for the number provided. If the number provided has no visible symbol, you print nothing you can see. Why do you want to print a number as a character? Commented Jan 3, 2020 at 1:49
  • Please post the function check() Commented Jan 3, 2020 at 1:49
  • The % 10 code will ensure that c will be a non-printable/non-visible character. Commented Jan 3, 2020 at 1:50
  • Addendum, in ASCII, the dominant output encoding for C++, everything under 8 is a non-printable control character. 8 is a backspace, not that useful to you and 9 is a tab, also hard to see unless you know what you are looking for. Commented Jan 3, 2020 at 1:51

1 Answer 1

0

You can use std::to_string() (C++11) but making sure that the value of c is something that can be printable is a better practice.

Sign up to request clarification or add additional context in comments.

5 Comments

why use to_string() when std::cout can already print integers?
that's right, but using std::to_string would let him to safely convert the value to string and manipulate, if needed, before printing.
Hi! Thx a lot. I changed char c to string c = to_string(ss). This solution works for the purpose of what I want to achieve, but I am just wondering why char was not working. I would like to say that it might be due to the size of the ss. Is that a fair assumption? For example, for one of the test cases I made ss is the value 2. I assumed that converting that int value to a char would have been sufficient. Later in main, I use: string s(sum(input)); char p[s.length()]; p[0] = s[0]; to retrieve the value 2.
I do not know what does check() do, you should post the entire code. That would help people to give spot on answers
@Kibill you shouldn't use to_string in this case. It'll create an expensive new string object unnecessarily. Just use cout << +c like mentioned in the duplicates. Without the + the specialized char overload will be called which prints out a char and not the integer value

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.