| smrt (6) | |||
|
Hello, all. I am in a beginning CS class at my University and my assignment is to write a program that takes a series of restaurants and narrows them down tournament style until you have a single choice (That's not very descriptive, but that's not where my problem lies.) I am using vectors to store the restaurant names and have written a function so the user can input a restaurant. The function uses push_back to store the new element every time it's called. The problem is, the user input is not getting stored. Here's the code for the function:
I feel like I'm missing something really elementary here, but I've tried it with arrays too and it doesn't work. cout-ing r_in after push_back is called tells me that the var is getting stored properly as a var, just not in the vector. I'm pretty burned out on this one and any help would be appreciated. Thanks. | |||
|
|
|||
| Duoas (6734) | |
|
User input is non-obvious, but many professors treat it as if it were. You don't need line 5. (It does nothing for you but mask any errors you may have elsewhere.) You don't actually need the variable 'finder'. You can use 'r_in' just as easily. I don't actually see any reason why your function should be failing. Perhaps the problem is elsewhere in your code. Can you post more? | |
|
|
|
| LowestOne (768) | |
| Agreeing with Duoas, perhaps try "cout-ing" size() immediately before and after the push_back(). | |
|
Last edited on
|
|
| smrt (6) | |||
|
Thanks for the input. I didn't even notice that about the finder var. Here's the main function where the vector is declared and the add function is called.
returning the size after the function call returns 1 every time. EDIT: Okay, I think I've got it for those of you Googling this. When I call back the main function it re-initializes my vector as empty. The easiest fix that would be to make it a global variable, but I know that's discouraged. Any other suggestions? | |||
|
Last edited on
|
|||
| Moeljbcp (85) | |
| The easiest solution would be to not recall the main() function. Find a different way to control the flow of the main function. Just don't re-call it. Not a good idea. | |
|
|
|
| jlb (77) | |
|
The program shouldn't even compile with the recursive call to main(). In a C++ program main() can not be called by any function, including main(). This is prohibited by the standard. | |
|
|
|
| Duoas (6734) | |||
Yes, use a loop. Line 17 there is evil. You aren't allowed to do that, even if some dumb compilers let you try.
| |||
|
|
|||