Deleting corresponding values in Vector

I want to delete voltage coresponding to the device name. For example the earlier code deletes name only.I want to delete the voltage also, so that voltage will be subtracted from the total voltage.in easy words when device will be deleted its voltage will also be gone. i dont have any idea how to do it




#include<conio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <string>

static int bat=100;
static int voltage;
using namespace std;

struct Device
{
std::string name;
int volt;
int consump;

};


int main()
{
// vector< string >::iterator iter;
vector<Device> device ;
vector<Device>::iterator it ;

//typedef std::vector<std::string> TVec;
Device temp;
int vol=0;
char answer;
string rm;
string who;
//int consump;

cout << "Do you want to enter Devices (y/n)? ";
cin >> answer;

while (answer == 'y')
{
cout << "Enter Voltage threshold: ";
cin >> temp.volt;
if(temp.volt<50)
{
cout<<"system has enough voltage you may enter the device" ;
cout << "Enter device name: ";
cin >> temp.name;
cout << "Enter Consumption level in Percentage: ";
cin>>temp.consump;
cout<<"remaining battery";
bat=bat-(bat*temp.consump/100);
cout<<bat;
device.push_back(temp);
}
else {cout<<"sorry system dont have enough poewer" ;}




std::cout << "Do you want to enter more devices (y/n)? ";
std::cin >> answer;
}




cout << "Please enter name to remove:" <<endl;
std::getline(std::cin,who);

/// find and remove name on list

for( it=device.begin(); it!=device.end() ; ++it )
{


if( (*it).name == who )
{
device.erase(it); // iterator passed to erase
// found=true; // exit the loop
}
}

/// inform user of status


// std::cout << who << " is removed" << std::endl;



// std::cout << who << " is not on list" << std::endl;



for(size_t i = 0; i < device.size(); ++i)
{
std::cout << "Device " << device[i].name;
std::cout << " Voltage: " << device[i].volt << std::endl;
}

getch();
return 0;
}
1
2
3
4
5
6
7
8
for( it=device.begin(); it!=device.end() ; ++it )
{
    if( (*it).name == who ) //alternate: it->name ;)
   {
       //subtract device's voltage value from total voltage value here too! 
       device.erase(it); // iterator passed to erase
   }
}


or just re-calculate the total voltage after removing the vector element

edit: use code tags!!!!!!!!! >:)
Last edited on
Topic archived. No new replies allowed.