I'm trying to compare a string value, defined by user input, to values in a struct, so it will print out the names associated with that value.

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

#define n_fencers 5

struct Fencer {
	string name;
	string weapon;
	int score;
	int score2;
} fencers [n_fencers];

void getTeam()
{
	for(int n=0; n < n_fencers; n++)
	{
		cout<<"Info for fencer #" << n <<endl;
		cout<<"Please enter the fencer's name: ";
		cin>>fencers[n].name;
		cout<<"Please enter the fencer's weapon: ";
		cin>>fencers[n].weapon;
		cout<<"Please enter the fencer's scores: ";
		cin>>fencers[n].score >> fencers[n].score2;
		if(fencers[n].score > fencers[n].score2)
			fencers[n].score2 = fencers[n].score;
		else if (fencers[n].score2 > fencers[n].score)
			fencers[n].score = fencers[n].score2;

	}
}

void printTeam(Fencer info)
{
	
	cout<<info.name << ", " << info.weapon << ": " << info.score <<endl;

}


void weaponChoice() ///////PROBLEM HERE
{
	Fencer info;
	string favorite;
	cout<<"Please select a weapon: ";
	cin>>favorite;
	for(int n=0; n<n_fencers; n++)
	{
		if(favorite == info[n].weapon)
		cout<<info.name<<endl;
	}
}

int main()
{
	
	getTeam();
	for(int n=0; n<n_fencers; n++)
	{
		printTeam(fencers[n]);
	}
	
	weaponChoice();
	



	system("PAUSE");

}

I'm having issues in my weaponchoice function while trying to compare, "no operator matches these operands.

someone point me in the right direction?

Recommended Answers

All 2 Replies

THe following line

if(favorite == info[n].weapon)

Should probably be

if(favorite == fencers[n].weapon)

Similar to the way you handle things in getTeam

THe following line

if(favorite == info[n].weapon)

Should probably be

if(favorite == fencers[n].weapon)

Similar to the way you handle things in getTeam

....so dumb...how did I miss that!!?!??!

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.