Taking input using Vectors

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 1992 Views - Last Post: 13 April 2011 - 06:25 PM Rate Topic: -----

#1 mansoorazeem  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 09-April 11

Taking input using Vectors

Posted 09 April 2011 - 01:24 AM

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; 
    }




Is This A Good Question/Topic? 0
  • +

Replies To: Taking input using Vectors

#2 manasij7479  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 09-April 11

Re: Taking input using Vectors

Posted 09 April 2011 - 02:38 AM

Make a class for the device and then overload the >> operator.
Then for input, make a vector of that class and iterate.
Was This Post Helpful? 0
  • +
  • -

#3 mansoorazeem  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 09-April 11

Re: Taking input using Vectors

Posted 09 April 2011 - 02:46 AM

Thank for the reply but i have just learned Vectors from internet so i dont know how to use them exactly .I can make class but overloading with >> opperator what does that mean ? :(
Was This Post Helpful? 0
  • +
  • -

#4 manasij7479  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 09-April 11

Re: Taking input using Vectors

Posted 09 April 2011 - 02:58 AM

View Postmansoorazeem, on 09 April 2011 - 02:46 AM, said:

overloading with >> opperator

http://www.cplusplus...orial/classes2/
Was This Post Helpful? 1
  • +
  • -

#5 mansoorazeem  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 09-April 11

Re: Taking input using Vectors

Posted 09 April 2011 - 09:56 AM

my dear friend if tutriols were any help to me, i would not have been asking questions here.
Was This Post Helpful? 0
  • +
  • -

#6 manasij7479  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 09-April 11

Re: Taking input using Vectors

Posted 09 April 2011 - 10:05 AM

At least try to understand it...!.... You can always ask questions about the specific part you are having problems with. But don't assume that you can learn programming by never bothering to read manuals or tutorials.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,563
  • Joined: 25-December 09

Re: Taking input using Vectors

Posted 09 April 2011 - 10:39 AM

First you might want to consider using a struct or class to to hold your data.

#include <iostream>
#include <string>
#include <vector>

struct Device
{
   std::string name;
   int voltage;
};


int main()
{
   std::vector<Device> device;
   Device temp;

   char answer;

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

   while (answer == 'y')
   {
      std::cout << "Enter device name: ";
      std::cin >> temp.name;
      std::cout << "Enter voltage: ";
      std::cin >> temp.voltage;

      device.push_back(temp);

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

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



Jim
Was This Post Helpful? 0
  • +
  • -

#8 mansoorazeem  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 09-April 11

Re: Taking input using Vectors

Posted 09 April 2011 - 11:03 AM

Thank you Jim very much i really appreciate your help you saved my hours of work.
Now what i want to do is , to add voltage of all devices. Than can you kindly tell me how would i delete a specific device ? so that the corresponding voltage will also be reduced ? I am really near to completion of this project,if u can help me with this functionality i will be really really thankful to you .
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,563
  • Joined: 25-December 09

Re: Taking input using Vectors

Posted 09 April 2011 - 11:10 AM

Quote

Now what i want to do is , to add voltage of all devices.


Check how the final print statement is working in my last post. Instead of printing the entire vector total the voltages.

Quote

Than can you kindly tell me how would i delete a specific device


Check out the documentation for std::vector. Do you see any Member Functions that will do exactly what you want?

Jim
Was This Post Helpful? 0
  • +
  • -

#10 mansoorazeem  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 09-April 11

Re: Taking input using Vectors

Posted 09 April 2011 - 11:19 AM

Thanks again, i am able to get the total of voltage now. And i have seen the link

this is the function vector::erase which might help me. I can give u an example what i want to do. let say i have added device "Bulb" with Voltage 5 and i have added another device "Meter" with voltage 15. Now if i will delete device "Bulb" i want that the voltage 5 should also be deleted. Hence the remaining voltage which i will get will be 15.

I want to get the input from user as which device to delete.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,563
  • Joined: 25-December 09

Re: Taking input using Vectors

Posted 09 April 2011 - 12:30 PM

The erase member looks about right. Put you thoughts into code and if you have problems post the code, any error messages and a specific question about the code and I'm sure someone will be able to help further.


Jim
Was This Post Helpful? 0
  • +
  • -

#12 mansoorazeem  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 09-April 11

Re: Taking input using Vectors

Posted 09 April 2011 - 12:49 PM

Thanks friend.I am almost there. I was able to delete device after taking input its name. The only thing remaining now is that i want delete the corresponding voltage value related to device.hope someone will help that out. Thanks again for your help


#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()
{
   std::vector<Device> device;
   Device temp;
int vol=0;
   char answer;
   int rm;
   //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;
   }

   for(size_t i = 0; i < device.size(); ++i)
   {
      std::cout << "Device " << device[i].name;
      std::cout << " Voltage: " << device[i].volt << std::endl;
      voltage=voltage+device[i].volt;
   }
    cout<<"Total voltage : "<<voltage<<endl;
    cout <<"Enter the name to delete \n" ;
        cin>>rm;
        
       
        device.erase(device.begin()+'rm');
      //  device.erase(device.begin()+'rm')
        
    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;
}


Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,563
  • Joined: 25-December 09

Re: Taking input using Vectors

Posted 09 April 2011 - 01:00 PM

Please explain what this line is doing?

device.erase(device.begin()+'rm');



Jim
Was This Post Helpful? 0
  • +
  • -

#14 mansoorazeem  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 09-April 11

Re: Taking input using Vectors

Posted 09 April 2011 - 01:38 PM

this line takes input from the user and than erases that item.
Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,563
  • Joined: 25-December 09

Re: Taking input using Vectors

Posted 09 April 2011 - 02:12 PM

There is a couple of things wrong with this line:

device.erase(device.begin()+'rm');
First: 'rm' the single quotes are used to signify a character constant. Since this is two characters it is incorrect a character can only be one character. Multi-character constants would use the double quotation marks.

Second: To erase an element of a vector you need to provide the location of the item you want to erase. The device.begin() is correct now you would add the index for the correct element. If you want to delete the third element then this would be device.begin() + 2.

Third: No where are you locating this index number based on the name of the provided name, which I believe is held in the variable with the name of rm. You will need to iterate through the vector until you find the name that matches the item you want to remove, then remove that item.

Since you want to remove the item when you find it I would recommend that you use iterators for your loop. See this link for information on vector iterators.

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2