How to fix this "signed/unsigned mismatch "

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

struct lead
{
string name;
bool active;
int budget = 0;

};

lead enterLead();
void deleteLead(vector <lead> &storage);
void printLeads(vector <lead> storage);
void menu(vector <lead> storage);



int main()
{
vector <lead> storage;

menu(storage);


return 0;
}

lead enterLead()
{
lead x;
char userAnswer = 'z';

cout << "Enter the name of the employee: " << endl;
cin >> x.name;

cout << "Enter the budget" << endl;
cin >> x.budget;

cout << "Is the employee active <y/n> :" << endl;

cin >> userAnswer;
tolower(userAnswer);

if (userAnswer == 'y')
{
x.active = true;
}
if (userAnswer == 'n')
{
x.active = false;
}
else
{
cout << "invalid input, re-enter" << endl;
cin >> userAnswer;
}


return x;
}

void deleteLead(vector <lead> &storage)
{

int userAnswer = 0;
cout << "which lead would you like to delete?" << endl;
cin >> userAnswer;
userAnswer -= 1;

storage.erase(storage.begin() + userAnswer);

}

lead editLead(vector <lead> storage)
{
lead x;
cout << "Which lead would you like to edit" << endl;
printLeads(storage);
//lead[n].name
return x;
}


void printLeads(vector <lead> storage)
{
char userAnswer;


cout << "Would you like to see the active leads only <y/n> ?" << endl;
cin >> userAnswer;

if (userAnswer == 'y')
{
for (int i = 0; i < storage.size(); i++) //signed/ unsigned mismatch
{
if (storage[i].active = true)
{
cout << storage[i].name << endl;
cout << storage[i].active << endl;
cout << storage[i].budget << endl;
}
}
}
else if (userAnswer == 'n')
{
for (int i = 0; i < storage.size(); i++) // signed/ unsigned mismatch
{
cout << storage[i].name << endl;
cout << storage[i].active << endl;
cout << storage[i].budget << endl;
}
}
while (userAnswer != 'y' || 'n')
{
cout << "Invalid Input" << endl;
cout << "Would you like to see the active leads only <y/n> ?" << endl;
cin >> userAnswer;
}
}

void menu(vector <lead> storage)
{
char userInput;

cout << "Do you wish to (1) add a lead, (2) print leads, (3) delete a lead, or (4) edit leads, (5) quit" << endl;
cin >> userInput;

bool again = true;
{
while (again)
{

switch (userInput)
{
case '1':
enterLead();
break;
case'2':
printLeads(storage);
break;
case '3':
deleteLead(storage);
break;
case '4':
editLead(storage);
break;
case '5':
again = false;
}
}
}
}

for (int i = 0; i < storage.size(); i++) // signed/ unsigned mismatch
{
cout << storage[i].name << endl;
cout << storage[i].active << endl;
cout << storage[i].budget << endl;
}


Unless your compiler is from over 6 years ago and can't be upgraded, use the appropriate loop;
1
2
3
4
5
6
for (const auto& s: storage)
{
    cout << s.name   << '\n'
         << s.active << '\n'
         << s.budget << '\n';
}


Last edited on
 
for (int i = 0; i < storage.size(); i++)

You can change it to
 
for (unsigned int i = 0; i < storage.size(); i++)
@Hengry

First, listen to Cubbi - he is an expert :+)

Second, not quite unsigned int, STL size functions return a std::size_t type - which is usually the largest unsigned int the system has. So that would be unsigned long on a 64 bit system, or unsigned long long on a 32 bit system.
Here's the definition of std::size_t from section 18.2 of the draft C++/11 standard (which is the only one I have handy):
The type size_t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object.

So on a 32 bit system, it will be a 32 bit value (probably unsigned int) rather than unsigned long long, which is typically 64 bits.
Topic archived. No new replies allowed.