what is wrong with my code?

forget about what is commented in the "//"
#include <iostream>
#include <vector>
#include <string>
using namespace std;

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

};

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


int main()
{
vector <lead> storage;

storage.push_back(enterLead());
// deleteLead(storage);
printLeads(storage);



return 0;
}

lead enterLead()
{
lead x;

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 >> x.active;
tolower(x.active);

if (x.active == 'y')
{
x.active = true;
}
else (x.active == 'n');

{
x.active = false;
}


return x;
}

/* lead 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)
{

cout << "Which lead would you like to edit" << endl;
cin >> userAnswer;
}
*/

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++)
{
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++)
{
cout << storage[i].name << endl;
cout << storage[i].active << endl;
cout << storage[i].budget << endl;
}
}
else
{
cout << "Invalid Input" << endl;
cout << "Would you like to see the active leads only <y/n> ?" << endl;
cin >> userAnswer;
}
}
1) Please use code tags when posting code, to make it readable.
2) What makes you think there's something wrong with it? What behaviour are you seeing that differs from the behaviour you expect?

We're not mind-readers. If there's a problem, then you have to tell us what it is.
You are mixing char and bool types in the following:
1
2
3
4
5
6
7
8
  cin >> x.active;
  tolower(x.active);  // tolower() of a bool makes no sense
  if (x.active == 'y')  // makes no sense to compare a bool to 'y'
  {  x.active = true;
  }
  else (x.active == 'n');  // makes no sense to compare a bool to 'n'
  { x.active = false;
  }

x.active is a bool. It can only accept a 1 or 0. You can't expect to store character data ('y' or 'n') in it.

edit:
Line 89: storage.size is a function call. It needs ().
 
  for (int i = 0; i < storage.size(); i++)


Line 91: You're using the assignment operator (=), not the equality operator (==).
 
  if (storage[i].active = true)


Line 89,101: The type of i in your for loops should be size_t to avoid a type mismatch.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
Topic archived. No new replies allowed.