Hi, I've been self-studying c++ for about a month and I'm reading "Programming: Principles and Practice Using C++", I am at the end of chapter 4 and I've learnt some conditional statements, loops, functions and vectors and now I have come across this exercise: ----------Write a program where you first enter a set of name·and·value pairs,
such as Joe 17 and Barbara 22. For each pair, add the name to a vector
called names and the number to a vector called scores (in corresponding
positions, so that if names=="Joe" then scores==17). Terminate
input by the line No more ("more" will make the attempt to read another
integer fail ). Check that each name is unique and terminate with an error
message if a name is entered twice. Write out all the (name,score) pairs,
one per line.----------
So I have done the easy part but can't think of a way to solve the part it asks to terminate with an error if a name is entered twice.
I know that there are functions out there that can easily solve this but the book has not covered them yet, so how am I suppose to do this with what I have already learnt ? is this book asking me to research about solutions that it has not taught yet or I need to read these 4 chapters again ?
It can be done with what you've already learned. Though keep in mind that that particular book does occasionally expect you to do some independent digging to find solutions.
Before pushing the name and scores inside the while loop, insert another loop that will run through the current vectors and check if the entered name already exists. If it exists, display error message. If it doesn't, push the new values onto vectors.
Thanks Esslercuffi for replying,
I'm confused now, will I not lose the data if they are not pushed right after the input ? I mean before pushing them the vector has no elements.
I have tried to run through the current vectors after pushing them, like all_names[i] == all_names[i-1] but they are only good if the repeated names are next each other or if we sort them which in this case is not possible. I have tried all sort of things but can't find the way to check elements one by one with all the other elements within a for loop.
the data will be stored in the 'names' and 'scores' variables until you overwrite them with new values the next time through the while loop. so within the while loop, you could add another loop. Something like this: