Hi! I have a problem. I'm trying to compare a single value with a value from my Linked list and if they are same, I want to add the value from the list to new list. In other words I want to create a new List with values with the first one. Here is the code that I made, but it's not working. Can someone help me please?
This is the code with which I search in the first list for a node with a value. In the main() function I have A.find_city(), so it can start from the start_pointer from the first list:
Part of the problem might be that you're comparing your character string wrong. In void List::find_city() you compare both cities using the following if-statement: if(temp1->city != city). It looks to like the statement will always return true this way.
Since you are using C++ I recommend you use the String class, especially when handling certain operations like string manipulation or comparisons. You could do this as the following:
1 2 3 4 5
std::string str1 ("green apple");
std::string str2 ("red apple");
if (str1.compare(str2) != 0)
std::cout << str1 << " is not " << str2 << '\n';
Thanks for the reply.
I changed my statement to if(strcmp(temp1->city, city) == 0)
but it adds every node no matter if it's the same or not with 'city' to the second list :/
Since you're using cin() to get the value name there is bound to be a newline \n an the end of the input. The cities which you are checking this with might not have this? As I mentioned before, try to use a debugging tool to see if the strings are what you are expecting them to be. It's really a useful tool :-)
It's still not working :/
Do you want to give you the code and test it by yourself?
Here it is. I will post it in two separate posts because it's too long:
I have been looking but I am unsure on how to get your program to work. Cause whatever I enter, I keep ending up in a endless loop. Maybe you can provide some sample input to run the program?
-----
That being said the strcmp code should work, but you need to include #include<cstring> . In you're example your including the string library... Which is great! But then you might as well use the previous string example which I gave you.
First you should enter the number of the nodes you want to enter in the list. For examle 2.
So this is a sample input:
2
Code: 1234 // Just a random number
Address:
City: London // The name of the city
Street: Some name
Plosht: 2 // Just a random number
Stai: 2 // Just a random number
Naem: 2 // Just a random number
Code: 4321 // Again just a random number but different from the previous (beacause it should be unique)
Address:
City: Manchester// The name of the city
Street: Some name
Plosht: 2 // Just a random number
Stai: 2 // Just a random number
Naem: 2 // Just a random number
Then it will ask you to enter a city name. For example you can enter London and the program should add to the new list every node in which for city is written London. And then it should output the list.
Maybe something with my loops is wrong but I don't know what :/
It's a bit confusing but I hope you get my point :)
Yes, thank you. That was very helpful. The endless loop appears on invalid input (maybe useful to know).
What's going wrong is that in your method find_city() you create a temporarily list named temp1 which points to the start of your list. In your loop here you walk through your list until pointer to the NEXT element is NULL. So, think to yourself what happens if a list has only one element or the element you want to check is the final element in the list? It would not find it ever because the reference to 'next' from that element is NULL. This is a implementation flaw in your list and you should work that out. One way of doing this is always checking the previous element, but I'll leave the implementation up to you.
In your if-else statement the check was still done wrongely. You had if(strcmp(temp1->city, city)) while it should have been if(!strcmp(temp1->city, city)). After this it only goes into your if-statement on the moment the city name matches.
I will have to look into why your code still prints all the items but I have to log of for now :)