How to check if values are inside an array?

The below code is able to see if a value is inside the string array of names in the main function. However, how can I set the function to return the number "7" instead of byte address when the input does not match any of the names in the array? I have tried various iterations of else statements, but cannot get the program to provide "7" only after it has checked all the names and does not just stop at the first mismatch.

#include "stdafx.h"
using namespace std;
#include <iostream>
#include <string>

int nameCheck(string array [], int num){
string input;
cout << "Enter name: " << endl;
cin >> input;
for (int i = 0; i < num; i++){
if (input == array[i]){
return i;


int _tmain(int argc, _TCHAR* argv [])
string firstNameArray[7] = { "Jim", "Tuyet", "Ann", "Roberto", "Crystal", "Valla", "Mathilda" };

cout << nameCheck(firstNameArray, 7) << endl;

return 0;
Put a return num; as the last line before the closing bracket for nameCheck. Please use code tags so others can easier comment on your code (the button with "<>" next to your edit box)
Topic archived. No new replies allowed.