I got a weird assignment to make an array, struct, vector program but we weren't given information on any of that and what I find else where is hard to comprehend. The assignment itself is strange to me too as I don't understand the point of what I'm trying to make the program do.
The assignment:
Write a program:
Has an array/list that contains the words, Bobby, Sara, Stanley, Roger, and Fred
A function that takes two arguments, an array/list, and a word, and then looks for the word for a match. Return true or false depending on whether or not a match is found.
I have no code to go off of since none was provided. Any/all help is appreciated
I somewhat have an idea after looking things up more. My idea is I have an array that has two options (fail being 0, pass being 1), the word im looking for is pass. So if that person has a 1 on the array it should mean pass(I think thats how this works?) and it returns true. That's how I understand this so far, I'll try to make a code for that but help/advice is still greatly appreciated!
Based on what you said in the first post, I don't see why the objectID/result is necessary. It's just checking for names, and the function returns a bool.
That, however, is not what your instructions were.
Has an array/list that contains the words, Bobby, Sara, Stanley, Roger, and Fred
1 2 3 4 5 6 7 8
#include <string>
#include <vector>
int main()
{
// an array that contains the words, Bobby, Sara, Stanley, Roger, and Fred:
std::vector<std::string> names { "Bobby", "Sara" , "etc", "etc", "etc" };
}
A function that takes two arguments, an array/list, and a word
Return true or false depending on whether or not a match is found.
1 2 3 4 5
bool // returns true or false
something( // name of the function
const std::vector<std::string>&, // array argument
const std::string& // word argument
);
I've been playing with what you mentioned before, seeing as that way seems easier. I looked up multidimensional arrays which I think is how I'm supposed to go about this. I'm a little tripped up on how they work. I'm trying to get 5 rows and 2 columns, the names as the row titles, and "fail" "pass" for the columns but I'm not sure how that would write. What I have now is this:
Think about the information in your current multidimensional array. What information does it provide? You just duplicate the values "fail" and "pass" 5 times. Essentially, it gives no information. It doesn't tell you if finding a particular name passed/failed.
The pass/failure is meant to be encoded via the return value of the function you're supposed to create.
The bool return value -- true or false.
At this point I'm completely lost and don't understand what to do haha. I've never had to do bool functions before so I didn't know about them. With what you said, does that only make Sara pass and the others fail? How would I add others names to the pass list like this?
I know I have to make the return function eventually, I'm just unsure of what I'm even doing so far is working. I tried to have the program display but it doesn't want to
Even if I take the arguement out of the bool function and identify 'names' in the function, I end up getting the error that there's too many arguments, when there are none.
A few issues.
(1) Your assignment says the function "takes two arguments, an array/list, and a word".
You currently have no arguments for your function's definition.
(2) You aren't calling your function from main.
(3) Even if it did work, you are calling your function recursively on line 11. Don't call contains_name from within contains_name.
(4) Don't declare another names array on line 9. Pass it in as an argument from main.
(5) You aren't returning anything from your function. You need to return a bool value (true or false). Printing "Pass" from within the function is not the same thing as returning true.
Write a function that takes in a string, and returns true if the string equals "Hello", and false otherwise. Call this function from main, and print "Yes!" in main if the function returns true.
#include <iostream>
#include <string>
usingnamespace std;
bool something(string word);
int main()
{
string answer;
cin >> answer;
// call function and use the value it returns
if ( something(answer) ) {
cout << "Yes!\n";
}
}
bool something(string word)
{
// return true here, if word is "Hello"
// else return false
}
I see that I am meant to use a different word in the argument for the function than the actual word in the main. That was the issue I was having with this small problem, thinking i needed something other than true or false as a return.
So if that works then why does this code not work after I added the changes:
Imho it seems like you're over complicating your original problem. If I'm not mistaken, and assuming you've posted the entire problem, it's not even asking you to output anything (of course that's not to say you shouldn't); you simply need to create a function that returns either true or false.
A simple solution is for your function to return the value of std::find and then you can output the return value as a 1 or 0 to represent true or false, unless I'm missing something?
The search function has to have only 2 arguments - container and word to search. So unless a c-style array is passed as a templated argument (or std::span in C++20), then it needs a container.
Technically, string[], int, string are three arguments.
Logically, the string[], int are one array and the string is the second argument.
When one has expression: people == "Bobby", "Sara", "Stanley", "Roger", "Fred"
one has to ask: What is the operator precedence, the order of evaluation?
Is the above same as (people == "Bobby"), "Sara", "Stanley", "Roger", "Fred"
or is it people == ("Bobby", "Sara", "Stanley", "Roger", "Fred")?
Now the question is, what does the comma operator do and what is the order of evaluation of chained operators?
Does A, B, C do same as (A, B), C or same as A, (B, C)?
It is left to right, i.e. (A, B), C
In other words, the people == "Bobby", "Sara", "Stanley", "Roger", "Fred"
returns exactly same (since expressions in it do not have side-effects) as "Fred" and "Fred" as condition converts to true.
No surprise that the comma operator is seldom used.
Another way of putting it is without the int, the array/list doesn’t functionally exist. Essentially the same can be said about <vector> which @OP decided not to use. All a vector does is encapsulate and hide the int so it’s same-same.
Aside from that the question is ambiguous because of this esoteric argument. At best the school teacher has created a discussion point for a tutorial, or at worst doesn’t have a clue. I know where @seeplus is on this spectrum and why I summarily dismiss the comment as rubbish.