6 Replies - 561 Views - Last Post: 15 December 2014 - 12:03 AM Rate Topic: -----

#1 perksofbeingawildflower   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 14-December 14

printing a map that contains a string and a class with integers?

Posted 14 December 2014 - 03:06 PM

I'm new to c++, so if this is obvious, I'm sorry, but i'm struggling with finding good examples on how to print the contents of a map that contains a string and a class of integers for option 1. what formatting should I use? Let me know if you need to see any other files.

#include <iomanip>
#include <vector>
#include <sstream>
#include <map>
#include "IPHost.h"
#include "EncryptedConnection.h"
#include "Connection.h"
#include "Packet.h"



int main ()
{
    int selection1;//user's selection from main menu options
    std::map <std::string, IPHost> Map;
    Map[std::string("uiowa.edu")] = IPHost(128,255,67,14);
    Map[std::string("google.com")] = IPHost(74,125,255,20);
    Map[std::string("twitter.com")]= IPHost(199,16,156,43);
    Map[std::string("news.slashdot.org")] = IPHost(216,34,181,48);
    Map[std::string("www.engineering.uiowa.edu")] = IPHost(128,255,17,182);
    Map[std::string("sports.yahoo.com")] = IPHost(98,139,199,204);
    Map[std::string("m.twitter.com")] = IPHost(199,16,156,43);
    Map[std::string("x.org")] = IPHost(18,7,25,161);

    std::string encryptFrom = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    std::string encryptTo = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm";

  while (1) // while loop that keeps the menu available until user chooses to leave the program
  {
    //present user with menu options
    std::cout << std::endl;
    std::cout << "Menu Options" << std::endl;
    std::cout << "1. Show all known hosts" << std::endl;
    std::cout << "2. Set source and destination hosts" << std::endl;
    std::cout << "3. Set message to send" << std::endl;
    std::cout << "4. Send message packets" << std::endl;
    std::cout << "5. Send encrypted message packets" << std::endl;
    std::cout << "6. Quit" << std::endl;
    std::cout << "Enter your selection now:" << std::endl;
    std::cin >> selection1; // reads in integer that represents users selection

    std::cout << std::endl;
    std::cout << "You Entered option: "<<selection1 << std::endl; // lets user know which option they chose


    if (selection1 == 1)
    {
    std::cout << "All known hosts and their IP:" << std::endl;

 /* std::map <std::string, IPHost> IPHost::iterator myIter;
    for(myIter = Map.begin(); myIter != Map.end(); myIter++)
    {
        std::string host = myIter->first;
        int IPhost address;
        address = myIter->second;
        std::cout << host << " " << address << std::endl;
    }
 */

    }
    if (selection1 == 2)
    {
        std::cout << "Enter source:" << std::endl;
        std::cout << "Enter destination:" << std::endl;
        std::cout << "there is no host named:" << std::endl;
        std::cout << "Source and destination cannot be the same host" << std::endl;
        std::cout << "provided host has the IP address: " << std::endl;
        std::cout << "" << std::endl;
    }
    if (selection1 == 3)
    {
        std::cout << "Enter message:" << std::endl;
        //std::getline
    }
    if (selection1 == 4)
    {
        std::cout << "Message Packets" << std::endl;
    }
    if (selection1 == 5)
    {
        std::cout << "Encrypted Message Packets" << std::endl;
    }


    if (selection1 == 6)
    {
      std::cout << "Exiting program" << std::endl;
      break;
    }



  if (selection1 >= 6)
    {
      std::cout << "Invalid selection, please select a different option" << std::endl;

    }

  }

    }


Is This A Good Question/Topic? 0
  • +

Replies To: printing a map that contains a string and a class with integers?

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,929
  • Joined: 08-August 08

Re: printing a map that contains a string and a class with integers?

Posted 14 December 2014 - 08:07 PM

Something like this could work for you.
#include <map>
#include <iostream>


class IPHost {
	int a,b,c,d;
public:
	IPHost(int A, int B, int C, int D) {
		a=A; b=B; c=C; d=D;
	}
	void output() {
		std::cout << a << ", " << b << ", " << c << ", " << d << std::endl;
	}
};

int main ()
{
	std::map <std::string, IPHost> Map;
	Map.insert(std::pair<std::string, IPHost>("uiowa.edu",IPHost(128,255,67,14)));
	Map.insert(std::pair<std::string, IPHost>("google.com",IPHost(74,125,255,20)));
	Map.insert(std::pair<std::string, IPHost>("news.slashdot.org",IPHost(216,34,181,48)));
	Map.insert(std::pair<std::string, IPHost>("www.engineering.uiowa.edu",IPHost(128,255,17,182)));
	Map.insert(std::pair<std::string, IPHost>("sports.yahoo.com",IPHost(98,139,199,204)));
	Map.insert(std::pair<std::string, IPHost>("m.twitter.com",IPHost(199,16,156,43)));
	Map.insert(std::pair<std::string, IPHost>("x.org",IPHost(18,7,25,161)));

	std::cout << "Map:\n";
	std::map<std::string,IPHost>::iterator it;
	for (it = Map.begin(); it != Map.end(); ++it) {
		std::cout << it->first << " ";
		it->second.output();

	}
	return 0;
}

I don't have your class, so I set up a simple one to get this to work, but all I'm doing is iterating over the map.
Was This Post Helpful? 1
  • +
  • -

#3 perksofbeingawildflower   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 14-December 14

Re: printing a map that contains a string and a class with integers?

Posted 14 December 2014 - 10:36 PM

Thank you for your response!!! If it helps, here are my .h and .cpp files:


#ifndef IPHOST_H
#define IPHOST_H
#include <string>
#include <iostream>

class IPHost
{
public:

    //Default constructor defaults to 0.0.0.0
    IPHost();

    //Constructor takes the four integers of the IP address
    IPHost(int ip0, int ip1, int ip2,int ip3);


    //Returns the address as a string of length exactly 15 padded with blanks
    //if needed
    std::string toString() const;

    //Compares two IP addresses, they are equal if and only if all four integers
    //are the same
    bool operator==(const IPHost& h)const;



private:
    int m_ip0;
    int m_ip1;
    int m_ip2;
    int m_ip3;
};


#endif





and the .cpp
#include <iostream>
#include "IPHost.h"
#include <sstream>


IPHost::IPHost()
{
    m_ip0 = 0;
    m_ip1 = 0;
    m_ip2 = 0;
    m_ip3 = 0;
}

IPHost::IPHost(int ip0, int ip1, int ip2,int ip3)
{
    m_ip0 = ip0;
    m_ip1 = ip1;
    m_ip2 = ip2;
    m_ip3 = ip3;
}

std::string IPHost::toString() const
{
    std::stringstream HostIP;
    int a = m_ip0;
    int b = m_ip1;
    int c = m_ip2;
    int d = m_ip3;


    HostIP << a << "." << b << "." << c << "." << d;

    //if statement to ensure the ip address is 15 characters long
    while(HostIP.str().length() < 15 )
    {
        HostIP <<" ";
    }
    return HostIP.str();
}

bool IPHost::operator==(const IPHost& h) const
{
   if (h.m_ip0 == m_ip0 && h.m_ip1 == m_ip1 && h.m_ip2 == m_ip2 && h.m_ip3 == m_ip3)
   {
        return true;
   }
   else
   {
       return false;
   }
}



For the void output part you suggested, I wasn't sure where to include it. should it be in main.cpp? or should it be in the .h/.cpp? Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1860
  • View blog
  • Posts: 6,698
  • Joined: 19-February 09

Re: printing a map that contains a string and a class with integers?

Posted 14 December 2014 - 10:47 PM

You have a toString function that outputs data from the class. How would you print a string?
Was This Post Helpful? 1
  • +
  • -

#5 perksofbeingawildflower   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 14-December 14

Re: printing a map that contains a string and a class with integers?

Posted 14 December 2014 - 11:10 PM

I'm still struggling, sorry. am I headed in the right direction with this? it doesnt compile because its an invalid use of IPHost :: IPHost

std::map<std::string,IPHost>::iterator it;
for (it = Map.begin(); it != Map.end(); ++it)
 {
   std::cout << it->first <<"has the IP address: ";
  it->second.IPHost.str()<< "\n";

 }

Was This Post Helpful? 0
  • +
  • -

#6 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1860
  • View blog
  • Posts: 6,698
  • Joined: 19-February 09

Re: printing a map that contains a string and a class with integers?

Posted 14 December 2014 - 11:25 PM

IPHost is a class so it is like a data type, it->second is a variable of that type. The function that returns a string from an IPHost object is called toString. To output the string to the console you use cout.
Was This Post Helpful? 1
  • +
  • -

#7 perksofbeingawildflower   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 14-December 14

Re: printing a map that contains a string and a class with integers?

Posted 15 December 2014 - 12:03 AM

Thanks! I figured it out!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1