What i am trying to to is the following

when user inputs ./test U www.google.com I <ip here>
same for ./test U <URL here> I <IP here>

using command line input argv and argc here is what i have done so far,

#include <iostream>
#include <cstdlib>
#include <string>


using namespace std;

struct str_pair
{
	char ip [50];
	char uri [50];
	str_pair *next;
	int counter;
};
str_pair *str_ptr;// pointer to an order struct, 
str_pair *list_head;// points to nothing initially.

const int list_size=; i need to know the list size!!
int main(int argc, char *argv[])
{
  if (argc == 1)
    { cout << "my name"<<endl;
      return(0);
    }
//--- initialize link list to empty.
list_head = NULL;// can test for NULL to see if anything is there.
//str_ptr = new str_pair;  //--- Create data at run time on the heap.
for (int i=0; i<list_size; i++)
{
str_ptr = list_head; // save old head of list.
list_head = new str_pair; // create new item.
list_head->next= str_ptr; // get next item to point to previous head.
list_head->counter =i;
}
strcpy(
str_ptr=list_head;
while(str_ptr != NULL)
{if(strcmp
return(0);
}

also a hint when where is showed add the ip addresses and uri.

Recommended Answers

All 7 Replies

Why don't you use c++ strings and vectors instead of re-inventing the wheel

#include <string>
#include <vector>
using std::string;
using std::vector;
// other using statements here

class str_pair
{
public:
     str_pair() { counter = 0; }

     str_pair(string ip, string uri)
     {
             this->uri = uri;
             this->ip = ip;
             counter = 0;
     }
     void setPair(string ip, string url)
     {
             this->uri = uri;
             this->ip = ip;
             counter = 0;

     }
public:
    string ip;
    string uri;
    int counter;
};

vector<str_pair> list_head;

int main(int argc, char* argv[])
{
    string_pair node;    
    node.setPair(argv[1],argv[2]);
    // add string_pair to the vector
    list_head.push_back(node);    
}

what are those pieces of code after line 35?

Its just the format we are suppose to follow, and the codes after line 35 are gonna be used to match the ip and the uri when they are passed on the command line.

these is what supposed to happen running the progam from command line and passing a parameter eg

./program_name and then the command U(uri) and then an ip address(192....)

now for the most important question where can I add the ip and the uri?

see the main() that I posted previously. If the format is ./test U <URL here> I <IP here> then url will be in argv[2] and ip will be in argv[4]. That assumes a space between U and the URL and another space between I and the IP address.

This is all good but we have to use pointers in the form i have shown in my code!!!!

1) "where can I add the ip and the uri?"

I'm not sure exactly you mean by that question.

It is possible, but ungainly, to have the user enter (add) all the data needed as input for the program from the command line. It is more common for the program to get the majority of the input it needs from the keyboard, a file, or some source other than the command line.

On the other hand I would assign (add) the data to each new node before it is added to the list, so probably between lines 31 and 32.

2) Having each str_pair object have it's own counter seems counter intuitive. Are you sure that's what you want?

3) All variables declared with the const keyword must have their value entered by the programmer before compilation, meaning the value of the const variable can not be information provided to the program at run time.

I want most of the data to be created at runtime that is why i am trying to use pointers. The purpose of the program is to find the corsponding uri for the given ip address!!

first the program needs to generate a linked list of those structures. To do that you will have to prompt for the structure values then add the structure to the list. Put that into a loop until the user says he/she wants to stop. You could also expedite this by reading the data from a data file so that you don't have to type them every time you run the program.

After that is finished prompt for and get input for the IP address to search for. Then iterate through the linked list comparing the IP address of each node to the IP address you just entered.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.