Need help with arrays, structs, and vectors

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!
Your array contains words. That means strings.
So you would have an array like:
1
2
3
4
5
6
7
8
#include <string>

...

int main()
{
    std::string names[5] = { "Bobby", "Sara" , "etc", "etc", "etc" };
}


Your function will check the names in the array against the name you're looking for, comparing the search name to the name at each array index.
Last edited on
I think I understand what you mean. This is the code I came up with by myself before incorporating what you said.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct names
{
	int objectID;
	string result, name;
	char grade;
};

int main()
{
 
	//array
	names name_arr[2];

	name_arr[0].objectID = 0;
	name_arr[0].result = "Fail";
	name_arr[0].name = "Bobby";
	name_arr[0].objectID = 0;
	name_arr[0].result = "Fail";
	name_arr[0].name = "Sara";
	name_arr[0].objectID = 0;
	name_arr[0].result = "Fail";
	name_arr[0].name = "Stanley";
	name_arr[0].objectID = 0;
	name_arr[0].result = "Fail";
	name_arr[0].name = "Roger";
	name_arr[0].objectID = 0;
	name_arr[0].result = "Fail";
	name_arr[0].name = "Fred";

	name_arr[1].objectID = 1;
	name_arr[1].result = "Pass";
	name_arr[1].name = "Bobby";
	name_arr[1].objectID = 1;
	name_arr[1].result = "Pass";
	name_arr[1].name = "Sara";
	name_arr[1].objectID = 1;
	name_arr[1].result = "Pass";
	name_arr[1].name = "Stanley";
	name_arr[1].objectID = 1;
	name_arr[1].result = "Pass";
	name_arr[1].name = "Roger";
	name_arr[1].objectID = 1;
	name_arr[1].result = "Pass";
	name_arr[1].name = "Fred";

	for (int i = 0; i < 2; i++)
	{
		cout << name_arr[i].objectID << endl;
		cout << name_arr[i].result << endl;
		cout << name_arr[i].name << endl;

	}
}
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.

So you would have a function like:
1
2
3
4
bool contains_name(string names[], string name_to_find)
{
    // ...
}


Of course, if your assignment is actually more than what you just wrote, perhaps a struct is more appropriate.
Last edited on
This does same as what your program does:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct names
{
	int objectID;
	string result, name;
	char grade;
};

int main()
{
	names name_arr[2] {
	    { 0 , "Fail", "Fred" },
	    { 1 , "Pass", "Fred" }
	};

	for (int i = 0; i < 2; i++)
	{
		cout << name_arr[i].objectID << endl;
		cout << name_arr[i].result << endl;
		cout << name_arr[i].name << endl;
	}
}


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
  );


Should function could be used:
1
2
3
4
5
6
7
	std::vector<std::string> names { "Fred", "Jane" };

	if ( something( names, "Fred" ) ) {
	    cout << "Found\n";
	} else {
	    cout << "Not Found\n";
	}
Last edited on
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:
1
2
3
4
5
6
7
8
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string name[5][2]{ { "Fail", "Pass" }, { "Fail", "Pass" }, { "Fail", "Pass" }, { "Fail", "Pass" }, { "Fail", "Pass" } };
}


I'm not entirely sure how I would get the names to title the rows, or if this is even correct
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.

For example, going off what I wrote earlier,
1
2
3
4
5
6
7
8
9
10
string names[5]; 
// ...
if (contains_name(names, "Sara"))
{
    cout << "Pass\n";
}
else
{
    cout << "Fail\n";
}


The literal strings "Pass"/"Fail" need not be part of the array.
Last edited on
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
	string names[5] = {"Bobby", "Sara", "Stanley", "Roger", "Fred"};

}

bool contains_name(string names)
{

	if (contains_name(names, "Sara"))
	{
		cout << "Pass\n";
	}
	else
	{
		cout << "Fail\n";
	}
}

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
Last edited on
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
	string names[5] = {"Bobby", "Sara", "Stanley", "Roger", "Fred"};

}

bool contains_name()
{
	string names[5];

	if (contains_name(names, "Sara"))
	{
		cout << "Pass\n";
	}
	else
	{
		cout << "Fail\n";
	}
}
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.

Site tutorials:
http://www.cplusplus.com/doc/tutorial/arrays/
http://www.cplusplus.com/doc/tutorial/functions/

_____________________________________________

Let's focus on a simpler problem, first.

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.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
using namespace std;

bool something(string answer);

int main()
{
	string answer;

	cin >> answer;
}

bool something(string answer)
{
	if (answer == "Hello")
	{
		cout << "True\n";		
	}
	else
	{
		cout << "False\n";	    
	}
	return ;
}


What would I return from the function if there is no value to return?
You did not call your function in main.
Call this function from main, and print "Yes!" in main if the function returns true.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace 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
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
using namespace std;

bool something(string word);

int main()
{
	string answer;
	cin >> answer;

	if (something(answer))
	{
		cout << "Yes!\n";
	}
}

bool something(string word)
{

	if (word == "Hello")
	{
		return true;
	}
	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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
using namespace std;

bool contains_name(string people);

int main()
{
	string names[5] = {"Bobby", "Sara", "Stanley", "Roger", "Fred"};
	string answer;

	cout << "Enter a name from the assignment: ";
	cin >> answer;

	if (contains_name(answer))
	{
		cout << "True\n";
	}
	else
	{
		cout << "Incorrect";
		return 0;
	}
}

bool contains_name(string people)
{
	if (people == "Bobby", "Sara", "Stanley", "Roger", "Fred")
	{
		return true;
	}
	else
	{
		return false;
	}
}
Last edited on
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?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>

using namespace std;

bool contains_name(string[], int n, string);

int main()
{
    string names[5] = {"Bobby", "Sara", "Stanley", "Roger", "Fred"};
    string answer;
    
    cout << "Enter a name: ";
    cin >> answer;
    
    if (contains_name(names, 5, answer))
    {
        cout << "True\n";
    }
    else
    {
        cout << "Incorrect\n";
    }
    
    return 0;
}

bool contains_name(string people[], int no_people, string word)
{
    for( int i = 0; i < no_people; i++)
    {
        if (people[i] == word)
        {
            return true;
        }
    }
    
    return false;
}


Enter a name: Sara
True
Program ended with exit code: 0
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

bool contains(const std::vector<std::string>& people, const std::string& name) {
	return std::find(people.begin(), people.end(), name) != people.end();
}

int main()
{
	const std::vector<std::string> names {"Bobby", "Sara", "Stanley", "Roger", "Fred"};

	std::string answer;

	std::cout << "Enter a name: ";
	std::getline(std::cin, answer);

	std::cout << (contains(names, answer) ? "True\n" : "False\n");
}

A function that takes two arguments, an array/list, and a word,

Yep, that's what I did - only two parameters.
bool contains_name(string[], int, string);

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")?

Turns out that the comma operator is at the bottom. See https://en.cppreference.com/w/cpp/language/operator_precedence
The answer is thus (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

What does comma operator do: https://en.cppreference.com/w/cpp/language/operator_other#Built-in_comma_operator

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.
Topic archived. No new replies allowed.