Ask your own question, for FREE!
Computer Science 20 Online
OpenStudy (anonymous):

C++: I want to check if a user input matches one of the contents of a vector. If it does not, it should ask the user again for input and check it again. How do I do that?

OpenStudy (anonymous):

Could you be a bit more explicit at some points? In two points, more precisely, do you actually want a vector type or you mean one-dimensional arrays? And secondly, do you mean to check only once whether a given number is within the array and if not, ask to enter another number, or to check multiple given numbers? i.e., if the number is found, do you want to return the number / exit the function, or to check for another input?

OpenStudy (anonymous):

Okay, by "vector" i really meant a vector type, i'm sorry. Also, if the program doesn't find the input in the vector, it should ask again and again until the user's new input matches one of the contents of the vector.

OpenStudy (anonymous):

the input is just a single letter by the way, the vector is type string.

OpenStudy (anonymous):

it should exit if the input matches one of the vector contents

OpenStudy (anonymous):

What about the STL find function? Check here http://www.cplusplus.com/reference/algorithm/find/ and here http://stackoverflow.com/questions/571394/how-to-find-an-item-in-a-stdvector And it's okay, it's a bit ambiguous that we call both ADTs vectors.

OpenStudy (anonymous):

well okay, now I'm confused how to use find() for vectors whose type is user-defined and that user-defined type contains many different member data..

OpenStudy (anonymous):

hi, if u only have a string inside which to find out ur character, u can just use the built-in type string and a simple function such as following: bool match (string s, char c) { for (int i = 0;i < s.size(); i++) { if (c == s[i]) { return true; } } return false; } use it in a while loop such as: which (match is not found) { input a new value of char to test call match () with the string and a new value. i hope this is what u were looking for. However if u have any doubts or u really wanted to use it to learn more about vectors, do let me know. I will be happy to help. Have fun!

OpenStudy (anonymous):

after hours of trying to figure out how could I use find() even though I had user-defined types for my vectors, I finally solved this problem with all your help. the final step of my project is done, thank you very much.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!