| Texedova (9) | |||
|
Hey guys i just want to check to see if one word (string) or one sentance is equal to any others within an array. My code so basically i want to check if a is equal to either "sleep", "in", or "bed". Or even if the string within a is equal to "sleep in bed" as one string. I have used the following but even if i get it correct and it says correct it says incorrect another 2 times because it is saying yeah you have gotten one right out of the possible 3. Even if I type "sleep in bed" it still does it (prints out incorrect another 2 times. Also are there any good books to start off with c++?
Thank you in advance | |||
|
|
|||
| buffbill (416) | |
| You haven't initialized a so it is unlikely to equal any of the elements in array string aa[3]. E.g. change L1 to string a = {"in"}; Then when i==1 the snippet should return "correct | |
|
Last edited on
|
|
| Texedova (9) | |||
oh lol, soz it's meant to be like this...
so a = user input | |||
|
|
|||
| Smac89 (199) | |
|
Your for-loop is going to compare the value entered by the user to all the values in the array and since you are checking the single word entered by user against all the words, even if you did type in one word that works, you will still get INCORRECT for the other words that are not matching what you entered. So what you want to do is to have a boolean that is set to false before your for-loop. The for loop should only have the if-statement in it not the else. When you find a match, set the boolean to true and break out of the for-loop. Now you just have to check the boolean, if it is still false after the for-loop has run then print the "INCORRECT" statement. | |
|
|
|
| Texedova (9) | |||
ok, now my code is...
so now if i type, "sleep in bed" for the value of string a, it takes me to the next bit where i should enter Hello World, but, beacuse "sleep" is correct it takes me straight to the Hello World part and also beacuse "in" and "bed" are incorrect for the Hello World Part. This make the out put be "Incorrect" twice. | |||
|
Last edited on
|
|||
| Smac89 (199) | |||||||
| |||||||
|
Last edited on
|
|||||||
| vlad from moscow (3654) | ||||
In fact you need to check that at least one word of the string array is contained in string a. It can be done the following way
| ||||
|
Last edited on
|
||||
| vlad from moscow (3654) | |
|
Oh, it seems that I was wrong. If I have correctly understood your task then you should check whether the input string consists exactly from three words in the specified order in the array do not taking into account blanks or it equal to one of the words. For example, this input string "_____sleep____in____bed___" satisfies the condition (here undescores denote spaces) and this input string also satisfies ths condition "__________in___" because it will be equal to one of the words in the array if to remove spaces. If I am right then you need to write two functions. The first one (let name it as trim) will renove leading and trailling spaces. The second one will compare the result string with the words in the array. | |
|
Last edited on
|
|
| Andrei15193 (151) | |||||||
-1 for suggesting the use of break. If you need to iterate, at most, through all entries then use while/do.. while and not a for. The least is meant for loops where you know how many times you have to cycle (hence the name for).
I'd rather call the first slipt() and not trim since blanks between words matter not. The way I would go at it: Split the phrase to get an array of words. Split the "looking for" phrase to get an array of lookingForWords. Check if words contain lookingForWords. E.g.:
| |||||||
|
|
|||||||
| vlad from moscow (3654) | ||||
To compare one word it is enough to use trim. To compare a sentence there is no need trimming or splitting because there is std::istringstream that allows to compare its separate words. In fact the realization is simple
If I have not made a typo then this code will give what is required.:) EDIT: | ||||
|
Last edited on
|
||||
| Andrei15193 (151) | |
| Functional-ish solution. NICE! I didn't have istringstream in mind when I wrote my solution, was think more general. | |
|
|
|
| vlad from moscow (3654) | |||||
Here is a tested code
I did not use declarations const size_t N = 3; std::string a[N] = { "sleep", "in", "bed" }; becuase MS VC++ 2010 has a bug then a constant ( I mean N in this case) is used in a lambda expression. It requires to capture it though according to the C++ Standard it is not needed. If do not take into account this bug of MS VC++ 2010 I would write the lambda the following way
| |||||
|
Last edited on
|
|||||
| Texedova (9) | |
| Thank you everyone but, are there any tutorials on this stuff so i can understand your codes a bit more? I mean i understand basic stuff such as you know, functions, while loops, a bit of arrays, int, float, double, long, string, for loops etc. I am still a beginner in c++ and have moved over to it from java, so yeah some basic stuff from java is almost the same as c++. | |
|
Last edited on
|
|
| vlad from moscow (3654) | |
|
lIn my opinion a good book is "Thinking in C++" by Bruce Eckel and Chuck Allison ( in two volumes). Though it is based on the previous C++ Standard it is actual till now. | |
|
Last edited on
|
|
| Texedova (9) | |
| Are there any tutorials in on the internet such as video tutorials? I am planning on buying a book soon but i still want to understand this code i have been given as soon as possible. If it is any help for others to help me (that sounds so sad, makes me sound selfish :( anyway ), I am actually trying to create a small text adventure game, just as i go in learning c++. | |
|
|
|
| sebelius (33) | ||||
//This will check if your input is equal to any of the words in the array that are seperated with a "space"
If the current string of the array is "personperson", only the first person will be applied. because after the first person a space is still not encountered, and therefore it will proceed and add an 'p' to temp which will then equal "personp", and therefore not catch the second identical word. You could solve this by reassigning temp whenever temp = input. Like: if temp==input assign temp "" | ||||
|
Last edited on
|
||||
| Texedova (9) | |||
This code
Doesn't print any matches, i even added 2 cout lines to print matches and temp and nothing gets printed. Am i missing something or doing something wrong or... | |||
|
|
|||
| Smac89 (199) | ||||
|
line 13: You are not going to the end of your strings in elements i.e. element[0] is ignored line 16: I think you were concentrating far too much on not including space that you basically just eliminate any real matches when space is found line 17: You are just adding characters to your temp and not putting any checks to see if they are what you are looking for line 14: Instead of using elements[x][y]!='\0';, use y < elements[x].length();. It's not really a big deal, but if you happen to have a zero in your string, then the code will fail worse than it had
| ||||
|
Last edited on
|
||||
| sebelius (33) | ||||
Ok, i wrote that one in hurry. This one is even tested, and works.
output:
It got problems, with the first letter of the array, but you can fix that; it does not actually find "who" | ||||
|
Last edited on
|
||||