Why am I getting outputs in ASCII all of a sudden? |
Do you mean instead of utf-8? That’s not the problem: the first 127 characters are the same in both tables.
ASCII table:
http://www.asciitable.com/
UTF-8 (first part):
https://www.utf8-chartable.de/unicode-utf8-table.pl?utf8=dec
You simply cannot assign a number to a std::string:
https://en.cppreference.com/w/cpp/string/basic_string/operator%3D
The overloaded assignment operators are:
♦ basic_string& operator=( const basic_string& str );
♦ basic_string& operator=( basic_string&& str );
♦ basic_string& operator=( basic_string&& str ) noexcept;
♦ basic_string& operator=( const CharT* s );
♦ basic_string& operator=( CharT ch );
♦ basic_string& operator=( std::initializer_list<CharT> ilist );
♦ template<class T> basic_string& operator=( const T& t );
It means you can assign to std::string:
- another std::string;
- a char *;
- a char;
- an “object convertible to std::basic_string_view”.
So, when you pass a number, the compiler attempts to turn it into a character, and the reference is the ASCII table.
To pass a number to a std::string you need to turn it into a string, for example by means of std::to_string:
https://en.cppreference.com/w/cpp/string/basic_string/to_string
MC.multiplication_func = std::to_string(10 * 5);
I am not sure if I am understanding this example correctly. |
Is it an example or an assignment? Anyway, I think you’re misinterpreting it.
Is there anything else I am doing wrong in my code example aside from getting a 2 result instead of a 50? |
Yes, definitely.
This is the code you’re asking about:
- - -
1 2 3 4 5 6 7 8
|
int main()
{
PrintName PN;
PN.studentName("Marcus", "Santoso");
MatchClass MC;
MC.multiplicationFunc(10, 5);
}
|
Create class(es) to get the below result:
Name : Markus Santoso
Result : 50 |
- - -
Look inside main(): do you see any assignment? What main() does is calling two functions, in this case two class methods.
Hint: is it possible that those methods modified some class properties?
Later you need to output the modified properties from your classes.