Why is (10*5) giving me 2?

I mean... I really don't get this. Line 21... Yet when I run the program it gives me:
Name : John Doe
Result :2

...what?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 #include <iostream>

using namespace std;

class PrintName{
public:
    string studentName;
};

class MathClass{
public:
    string multiplicationFunc;
};

int main(){
    cout << endl;
    PrintName PN;
    PN.studentName = ("John Doe");

    MathClass MC;
    MC.multiplicationFunc = (10*5);

    cout << "Name     : " << PN.studentName << endl;
    cout << "Result   : " << MC.multiplicationFunc << endl;
}


https://i.ibb.co/ygJ9BSX/A-14-2.png

^^ This is what I am trying to learn how to do.
I am not sure if I am understanding this example correctly.
Is there anything else I am doing wrong in my code example aside from getting a 2 result instead of a 50?

Thanks
What output are you expecting? Something like this?

Name     : John Doe
Result   : 50


Hint, what is the ASCII code for '2'?
... is it 50? @Furry Guy

Why am I getting outputs in ASCII all of a sudden?
I don't see anything about it mentioned in the screenshot example above. Did I miss a step somewhere?
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.
I don't see anything about it mentioned in the screenshot example above. Did I miss a step somewhere?

The code you wrote for main isn't what the screenshot shows to use.

You are assigning a value to a class data member, you should be calling a class method (class function):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>

// I really, really, REALLY don't like using this,
// your assignment requires it.
using namespace std;

class PrintName
{
public:
   void studentName(std::string first, std::string last)
   {
      std::cout << "Name:\t" << first << ' ' << last << '\n';
   }
};

class MathClass
{
public:
   void multiplicationFunc(int one, int two)
   {
      std::cout << "Result:\t" << (one * two) << '\n';
   }
};

int main()
{
   cout << endl;
   PrintName PN;
   PN.studentName("John", "Doe");

   MathClass MC;
   MC.multiplicationFunc(12, 7);
}


Name:   John Doe
Result: 84
Topic archived. No new replies allowed.