C++ Find & Change Value in Map [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I've scoured but not finding a solution; this is part of a homework assignment so looking more for tips/explanation than outright solution.
Problem:
I am parsing a file and extracting key elements into a map. I've declared my standard non-const map as : map<label,element>. In a second phase of the program, I am needing to locate if exists in "map" and replace its value.
I'm able to find the element, and print it, but I can't seem to get it to change. It's not a constant, so it should be editable, but maybe I'm using the wrong function?
((For reference, i is a line number (19, current value stored in map), value_i is a stored int variable I'm trying to insert into my second element (current value is 0) ))
for (auto &el : labels) {
if (el.second == i) {
el.second == value_i;
std::cout << "Label " << el.first << " value changed to: " << el.second << std::endl;
}
Output:
Label n value changed to: 19
Desired Ouput:
Label n value changed to: 0
Thanks in advance!!

you made a simple mistake which is el.second == value_i; - you didn't assign value for second, you checked if its equal value_i. If your compiler didn't give you any warning about it, I recommend setting a higher level of warnings ( you can read online on how to do it on probably every compiler), that way you won't miss such small mistakes.
If you change this line of code to:
el.second = value_i;
It will do what you desired.

Related

Why does == work and = does not in an if statement? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
for (unsigned int i = 0; i < list.size(); i++) {
if (list.at(i) = n) {
cout << "True";
return 0;
}}
I was wondering why this would not work I understand that tou should use list.at(i) == n.
However i thought that a single = means assigning, and a double == means equal to. I understand it is different but wouldn't using only one = still be correct when using it in an if statement?
It would not necessarily be correct. When you use an assignment expression as a boolean for integers, it will return true if the integer is not zero, and it will return false if the integer is zero.
Suppose our list looks like this: 1, 2, 0, 5. Now, suppose we have this if-statement:
if (list.at(0) = 1) {
cout << "True";
}
Since the 1 in list.at(0) = 1 is not 0, the if-condition will be satisfied. If we used ==, it would be satisfied since the first value is indeed 1.
Now let's suppose we have this if-statement:
if (list.at(1) = 3) {
cout << "True";
}
The "True" would be printed because 3 is not equal to 0. However, if we replaced = with ==, the "True" would not be printed since the second value is not 3.
Let's look at one last example.
if (list.at(2) = 0) {
cout << "True";
}
This would not print out "True" since we are assigning list.at(2) to 0. However, if we replaced the = with ==, the "True" would be printed since the third value in the list is actually 0.
This shows that = cannot be used as ==.
P.S. And, if you wanted to use the list later, your list would be modified into a different list.
if (condition) {
// block of code to be executed if the condition is true
}
Here is your code list.at(i) = n is an assignment and not a condition. Seeing your code I can say that you want to check if any value in the list is equal to n or not if it is then you want to print True.
So to do this you have to use a condition if(list.at(i)==n).
For more information read about condition statements, comparison operators, and assignment operators.

save variable from if statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Couldn't find an answer on google because I didn't know how to phrase is.
I have a regular function as below and would like to update the variable number in the first if statement. I've tried all sorts of combos but nothing works.
int main()
{
int apple, number;
cout << "Enter you number"<< endl;
cin >> apple;
if (apple == 1){
number = 2;
}
else {
number = 3;
cout << number << endl;
}
How would I change the above so I get 2 to output to the screen?
Thanks in advance!
You need to use
if (apple == 1)
instead of
if (apple = 1)
== is used for comparison. Also to note that your code will always assign the value 2 to the variable apple as in your condition you are not comparing rather you are assigning. So in your case the output will always be 2.

How to display Degree Celsius in a string in C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I need to display a string with values like 36 Deg Celsius.
string sFinish = NULL;
string sValue = "36";
sFinish.append(sValue);
sFinish.append(" Deg Celsuis");
cout<<"Degree = "<<sFinish;
I am not able to figure out how to display degree (o symbol) instead of writing "Deg Celsius".
If you just copy paste "°" string into code - it shows extra character - like this "°".
Try:
std::cout << "Temperature: " << sValue << "\370";
You might find the following link helpful for the full ascii table.
Here is a solution I found here on SO: Including decimal equivalent of a char in a character array
But to summarize, this would do fine
char * val = "37";
string temp(val);
temp.append("\xB0");
cout << temp;
Just in-case if anyone wants to try this:
sFinish.append("\u2103");
this will display Deg celsius :)

How to find string that contain dash in a string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I try to get the position of the string "-a" with this code
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string command("test –a string");
size_t pos = command.find("-a");
cout << "position found = " << pos << endl;
}
this produce this output:
position found = 4294967295
If I remove the '-' it's work as expected. 8 is returned.
You get the string::npos value returned, because the library thinks that it cannot find -a in the string.
The reason for this is that you use different dashes a long dash – in the string and a short dash - in the search string.
Once you replace the character with the correct one in both places, your code starts working fine (demo).
It means that there are different the first characters.
You can check this using the first characters and placing them in statement
std::cout << ( '–' == '-' ) << std::endl;
As they are different function find returns value std::string::npos that is defined as std::string::size_type( -1 ) or equal to 4294967295
If you look really close you will find the '–' is no '-'.

Understanding find and vectors C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to understand this line of code
vector<int>::iterator it = find(list_vector.begin(), list_vector.end(), 5)
where I have vector<int> list_vector; declared before hand.
What does the 5 do? What does it return? Does it return the 5 if it can find it at the beginning and end? If I wanted to make an if statement, and I wanted to find if the number 10 was in the statement (if it was, return true) how would I go about doing that?
vector<int>::iterator it = find(list_vector.begin(), list_vector.end(), 5)
std::find searches in the range defined by its first two arguments. It returns an iterator pointing to the first element that matches. If no element matches, it returns its 2nd parameter.
list_vector.begin() returns an iterator that points to the first element of list_vector.
list_vector.end() returns an iterator that points one element beyond the final element of list_vector.
5 is the target of the search. find() will look for an element that has the value 5.
If you'd like to determine if 10 is present anywhere in the vector, do this:
if(std::find(list_vector.begin(), list_vector.end(), 10) == list_vector.end())
std::cout << "No 10, bummer\n";
else
std::cout << "I found a 10!\n";
Or, if you'd like to simultaneously determine if 10 is present and determine its location:
std::vector<int>::iterator it = std::find(list_vector.begin(), list_vector.end(), 10);
if(it == list_vector.end())
std::cout << "No 10\n";
else
std::cout << "Look what I found: " << *it << "\n";