Taking inputs using vectors

I want to make a programm in which i will add a device, than i will add its voltage, at the end i will get the total voltage of the devices added. Now the problem is i am unable to get the input of devices and voltages at the same time. can someone help me ? I have written the code but it does this job in two seprate loops.







#include<conio.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <string>
static int voltage;
using namespace std;

int main()
{
vector<string> device_name;
vector<int> device_voltage;
string name;
char answer;
int remove;
string rm;

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

while (answer == 'y')
{
cout<<"voltage : : "<<voltage;
cout << "Enter value: ";
cin >> name;

// Now that we have the number from the user,
// append it at the end of the vector

device_name.push_back(name);

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

// Now print all the values; notice that we didn't need to
// count how many elements were entered: we can always use
// the size() method to ask student_marks how many are there!

for (int i = 0; i < device_name.size(); i++)
{
cout << "Device #" << i+1 << '\t' << device_name[i];

}

// cout<<"Which device you want to remove " ;
// cin>>remove;

device_voltage.resize (device_name.size());

vector<string>::iterator s;
vector<int>::iterator m;

for (s = device_name.begin(), m = device_voltage.begin();
s != device_name.end();
++s, ++m)
{
do
{
cout << "Enter Voltage (0 to 100) for " << *s << ": ";
cin >> *m;
voltage+m;

if (*m < 0 || *m > 100)
{
cout << "Error: Voltage must be between 0 and 100" << endl;
}
}
while (*m < 0 || *m > 100);
}

if (!device_voltage.empty()) // equivalent to device_voltage.size() != 0
{
// cout << "There are " << count (voltage_device.begin(), voltage_device.end(), 100)
// << " students that got highest mark!" << endl;

cout << "Lowest voltage: "
<< *min_element (device_voltage.begin(), device_voltage.end())
<< "\nHighest voltage: "
<< *max_element (device_voltage.begin(), device_voltage.end()) << endl;

// cout << "Average: " << accumulate (student_marks.begin(), student_marks.end(), 0) / student_marks.size() << endl;
}

cout<<"All the Devices" ;
for (int i = 0; i < device_name.size(); i++)
{
cout << "Student #" << i+1 << '\t' << device_name[i]<<endl;
}

cout <<"Enter the name to delete \n" ;
cin>>rm;

// myvector.erase (myvector.begin()+5);
device_name.erase(device_name.begin()+'rm');
//device_name.erase(device_name.begin()+0);

cout<<"All the names without deleted names" ;
for (int i = 0; i < device_name.size(); i++)
{
cout << "Device" << i+1 << '\t' << device_name[i]<<endl;


}


// '\t' is the code for the TAB character
getch();
return 0;
}

1
2
3
4
5
6
7
while(...){
   string name = get_a_single_device_name();
   int voltage = get_a_single_device_voltage();
   device_name.push_back(name);
   device_voltage.push_back(voltage);
   ...
}
Topic archived. No new replies allowed.