This question already has answers here:
How to output a character as an integer through cout?
(6 answers)
Convert an int to ASCII character
(11 answers)
Closed 3 years ago.
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?
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.
Related
This question already has answers here:
Why does subtracting '0' in C result in the number that the char is representing?
(8 answers)
Closed 2 years ago.
I'm trying to figure out a code that a friend sent me and I couldn't figure out why this certain code exists.
The goal is that when you type a 3 digit number sequence in string, it converts it to int and prints the sequence in reverse.
string s;
cout <<"enter integer sequence";
cin >> s;
int firstdig, seconddig, thirddig;
firstdig = s[0];
seconddig = s[1];
thirddig = s[2];
cout << thirddig << seconddig << firstdig;
Here is the problem with this code
When I input "123", the output becomes "515049" instead of "321"
However
This is the code that apparently fixes the problem
string s;
cout <<"enter integer sequence";
cin >> s;
int firstdig, seconddig, thirddig;
firstdig = s[0] - '0';
seconddig = s[1] - '0';
thirddig = s[2] - '0';
cout << thirddig << seconddig << firstdig;
This time, I input "123", the output becomes "321"
My main question is, where did "515049" come from, and what does the code "- '0'" do?
I don't know what that code does. C++ C++ C++
In the first code, you are converting a char into an int directly which, as it has been pointed out in the comments, gets the ASCII value of the characters.
In the second code, when you substract the ASCII value of the characters and '0' you get the original value of the character.
This question already has answers here:
The address of character type variable
(3 answers)
Closed 4 years ago.
I have this program
#include <iostream>
using namespace std;
int main ()
{
char c;
c = 'A';
cout<<"c = "<<c<<" &c = "<<&c<<endl;
return 0;
}
I expect the conosol to print out the following statement:
c = A &c = address in hex format
but instead I get this:
(Output: &c = A0 ")
why does the compiler treats the address of a character as an ASCII code, not a hex number
The problem is the "<<" operator is defined for char and char* to write a "character sequence" so if you want to see the address You have to cast to the number (int)&c
This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 5 years ago.
For example, i have the following string :
std::string s = "Hello, World!"
I want the address of the last element of s which is '!'.
I tried the following code, but it does not seem to output what I want it to.
std::cout << &s[s.length() - 1];
That outputs '!' not it's address, the same happens with s.back()
Is it because of the formatting caused by std::cout or is the problem elsewhere?
Basically I want a function that outputs the address of the last (and if possible, the first) element in a string.
Currently, you're using the overload of operator<< that takes a const char* as input. It treats the input as a null-terminated C string.
If you cast to (const void*) the problem will go away:
std::cout << (const void*)(&s[s.length() - 1]);
auto address_back = &s.back();
auto address_front = &s.front();
cout << static_cast<void*>(address_back) << endl;
cout << static_cast<void*>(address_front) << endl;
This question already has answers here:
Why in the code "456"+1, output is "56" [duplicate]
(3 answers)
Closed 6 years ago.
I am doing some exercises in C++ when I came upon something not so clear for me:
cout << "String" + 1 << endl;
outputs : tring
I suggest it is something with pointer arithmetic, but does that mean that everytime I print something in quotes that is not part of previous defined array,I actually create a char array ?
A quoted string (formally a string literal) is an array of const char, regardless of whether your printing it or doing anything else with it.
Code:
cout << "String" + 1 << endl;
has the same effect as this:
const char *ptr = "String";
cout << ptr + 1 << endl;
so no you do not create a new array, you just change pointer and pass it to std::cout
This question already has answers here:
Why is address of char data not displayed?
(8 answers)
Closed 6 years ago.
I'm going through the basics of learning C++, but keep hitting a wall when trying to decipher the following about chars and pointers. Included are line comments giving my current understanding of what's going on. Given that I have code like below:
using namespace std;
int main()
{
//String literal is an array of chars
//Array address gets assigned to a ptr of char
char myletters[] = {'h','i'};
char* lp = myletters;
cout << *lp << endl;
//Logically equivalent to above statements
char* letters2 = "hi";
cout << *letters2 << endl;
//String literal turns into array of chars
//Array of chars gets assigned to a ptr of chars
//Each ptr of chars gets stored into letters array
char* letters[] = {"hi","hello"};
cout << *letters << endl;
}
My output will be:
h
h
hi
My question is: when I use the final cout to print the contents of *letters, why do I get the string "hi" rather than the address of "hi" or the address of the first character in "hi"? I get that the first uses of cout are printing a char, and that the last cout is printing a char*, but I'm still wondering why it prints the complete string rather than the address as I would generally expect from a pointer.
Thanks kindly.
the << operator has a special definition for char* that prints the C-string it refers.
In your case, *letters has char* type (being letters a char*[], same as char**) and not char as *lp have.