Problems assigning the result of a function to a variable

#include <iostream> #include <string> using namespace std; int main() { string userInput; int stringSize = 0; userInput = "Hello"; userInput.size() == stringSize; cout << "Size of userInput: " << stringSize << endl; return 0; } 

I am trying to get the output from 5, but it returns 0. I cannot understand what happened. userInput "Hello" should be 5 and userInput.size() should return 5. This should be assigned to stringSize. Please note, can someone explain to me why this "==" is not "="? If I try it with "=", it will not compile, and I get this error:

 main.cpp: In function 'int main()': main.cpp:11:23: error: lvalue required as left operand of assignment userInput.size() = stringSize; ^~~~~~~~~~ 

My understanding is that โ€œ=โ€ is a purpose, and โ€œ==โ€ is a logical comparison that is either โ€œtrueโ€ or โ€œfalseโ€ depending on whether both sides are equal. I am trying to set the size of userInput.size() in a variable, so I think I should use "=". Maybe this is part of the problem?

+5
source share
5 answers

Here is the answer that was requested (PacO loans):

You always need to put the value that you want to assign to the variable on the right side of the = -operator, and the variable itself on the left.

What you did is trying to assign stringSize value stringSize userInput.size()

+2
source

My understanding is that โ€œ=โ€ is the purpose, and โ€œ==โ€ is the logical comparison, it is either โ€œtrueโ€ or โ€œfalseโ€ depending on whether both sides are equal.

So far your understanding is correct.

I am trying to set the size of userInput.size () in a variable so I think I should use "="

In general, you cannot write 5 = variable . Left and right in the task are not replaced , the order matters.

If you want to assign a value to a variable, the variable should be on the left side: variable = 5 .

I used this simplification, but the result of the size () function is similar to 5 in my example. This is called an rvalue (for example, "right value").

So in your case stringSize = userInput.size(); will be correct and set the size of userInput to the userInput variable (which is called the lvalue value, which can be assigned to the left of the job).

+2
source

The value of l can be considered as a named value that exists for its use, for example, a variable. The rvalue value is not saved after its use, as a rule, the result of a function call or what we often call simply โ€œvalueโ€.

You can save the rvalue to an lvalue ( int i = 2 + 3 ), but you cannot assign an lvalue to an rvalue. What is your problem there

The compiler throws an error because you have rvalue userInput.size() and you treat it as if it were an lvalue, trying to assign a value to it.

 userInput.size() = stringSize 

A simple example would be

 int n = 1; 3 = n; 

For obvious reasons, you can't just change what 3. In your example, your function just returns an integer value ... so why should you change this? So you get an error message

+1
source

Another perspective:

You cannot set the row size. The std::string automatically calculates the length for you.

Thus, there is no need for an expression:

 userInput.size() = stringSize; 

To clear the contents of std::string , use the clear() method:

 userInput.clear(); 
0
source

= - assignment operator in C, C ++ and other programming languages.

== assigns the value of the right expressions or variables to the value for the left variable.

A simple example:

 int x,y; x=10; y=10; if(x==y) printf("True"); else printf("False"); 

output will be: True

In your case, you need to use one operator = and put the function assignment to the right of the variable. for example, stringSize = userInput.size();

This is the final code:

 #include <iostream> using namespace std; int main() { string userInput; int stringSize = 0; userInput = "Hello"; stringSize = userInput.size(); //this line edited. cout << "Size of userInput: " << stringSize << endl; return 0; } 

see here online_C ++ _ compiler

0
source

Source: https://habr.com/ru/post/1271569/


All Articles