I am starting to learn c++ by recreating my FreePascal code in C++
In the scenario below I am trying to cout the 3rd char in a given string, however the only thing returning is the number 112. am I doing something wrong?
"however the only thing returning is the number 112. am I doing something wrong?"
That's to be expected. Each character in C++ is secretly an integer (represented in decimal format) that ranges from 0:127. When you pass a character to an int, the compiler assigns the character's integral equivalent to the int. C++ follows the ASCII character table, which can be found here: http://www.asciitable.com/
To fix this, swap int with char in dastrings() definition.
The return type for the dastrings function should be char. At the moment it is returning an integer, and this is interpreted as a numerical value by the << operator. The function should return a char, which will be interpreted as an ascii character, and should print correctly.
Yup that did the trick, and I just learned something new so golf claps all around.
A bit different from the function / procedure stuff I am used to. Thanks again!
So I ran into a new problem, and was hoping you could help me out again. I am getting another error while working this w/ strings, was wondering what I am doing wrong.
std::string ReturnAString()
{
return("A String");
}
int main()
{
std::string ReturnedString(ReturnAString());
std::cout << "Size of the string: " << ReturnedString.length() << '\n';
}
I should point out that both "string.h" and "stdio.h" are C headers. "string.h" should be "string" and "stdio.h" should be "cstdio". Note the absence of file extensions. This is because C++ headers do not have extensions.
Thank you again Framework, it compiled and worked
So now I know that you cannot mix char and string, which is just silly, but if I just stick to strings then I think my conversion will go smoother. Good thing almost everyone has like 2 gigs of ram these days.
You are right, I started reading "how to convert char to string" and saw there is a process for doing that. Coming from Delphi and Freepascal I can tell that c++ is much more finicky, but there is way more code examples, documentation and great people willing to help so it really is not all bad. I have dated some rigid women with very specific demands and this is a lot like that. :-)