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?

Ali
  • 2,702
  • 3
  • 32
  • 54
Kibill
  • 1
  • 1
  • 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? – user4581301 Jan 03 '20 at 01:49
  • Please post the function check() – bhristov Jan 03 '20 at 01:49
  • Addendum, in ASCII, the dominant output encoding for C++, [everything under 8 is a non-printable control character](https://en.wikipedia.org/wiki/ASCII). 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. – user4581301 Jan 03 '20 at 01:51
  • Of you are trying to get a single digit out, consider `char c = '0' + ss;` – user4581301 Jan 03 '20 at 01:56
  • [Displaying a character as a decimal number with cout](https://stackoverflow.com/q/29712623/995714) – phuclv Jan 03 '20 at 02:06
  • 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. – bhristov Jan 03 '20 at 01:48
  • The `% 10` code will *ensure* that `c` will be a non-printable/non-visible character. – Adrian Mole Jan 03 '20 at 01:50
  • Yes, but I am trying to explain to the OP how to fix the problem now and in the future. The debugger and the ASCII table are very important. – bhristov Jan 03 '20 at 01:54
  • OK, good point! – Adrian Mole Jan 03 '20 at 01:56

1 Answers1

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.

fnisi
  • 1,181
  • 1
  • 14
  • 24
  • why use `to_string()` when `std::cout` can already print integers? – phuclv Jan 03 '20 at 02:07
  • that's right, but using `std::to_string` would let him to safely convert the value to `string` and manipulate, if needed, before printing. – fnisi Jan 03 '20 at 02:18
  • 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. – Kibill Jan 03 '20 at 02:22
  • I do not know what does `check()` do, you should post the entire code. That would help people to give spot on answers – fnisi Jan 03 '20 at 02:23
  • @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 – phuclv Jan 03 '20 at 02:30