Searching an array

Program works, but trying to figure out how to get it to search the array without telling me " not found" after each index.

So if I enter "Daisy" it will say

Daisy not found
Daisy not found
Daisy has a 98.2
Daisy not found
Daisy not found
Daisy not found
Daisy not found

I hope that makes sense. Just introduced to arrays so no suggestions to high on the expert category.

Thank you



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

int _tmain(int argc, _TCHAR* argv[])
{
ifstream GradesFile;

string answer = "DONE";
string names[7];
double grade[7];
int index = -1;

GradesFile.open("Grades.txt");

if(GradesFile.fail())
{
cout << "Can't locate Grades.txt, ending program.\n";
exit(1);
}


for (int i = 0; i < 7; i++)

GradesFile >> names[i] >> grade[i];

do
{
cout << "Enter student name or DONE" << endl;
cin >> answer;
for(index = 0; index < 7; index++)
{


if(answer == names[index])
{
cout << names[index] << " has " << grade[index] << endl;

} else
cout << answer << " not found " << endl;

}


}while(answer != "DONE");


system("pause");

return 0;
}
do
{
cout << "Enter student name or DONE" << endl;
cin >> answer;
for(index = 0; index < 7; index++)
{

if(answer == names[index]) break;

}

if(index != 7)
{
cout << names[index] << " has " << grade[index] << endl;

} else
cout << answer << " not found " << endl;


}while(answer != "DONE");

But in any case this snip of code is wrong because you will search even word "DONE"
Last edited on
Topic archived. No new replies allowed.