5 Replies - 1751 Views - Last Post: 10 May 2009 - 03:15 PM Rate Topic: -----

#1 anya_ritika  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 16-March 08

converting from struct to classes

Post icon  Posted 05 May 2009 - 11:24 PM

i need to convert this code into classes...i tried many ways but i always get so many errors...could someone plz help me...its 10% of my total marks...please///
this code is supposed to do the following...
* The command line parameters will consist of a sequence of commands and data.
Individual commands are-
A uri ip : add URI and IP string pairs to the link list.
there is no printout in response to this command.
example "A www.google.com 64.125.19.2"
D uri : delete the URI and its associated IP from the link list.
example "D www.google.com"
there is no printout in response to this command.
U uri : given a URI find the IP address from the link list.
example "U www.google.com"
there shall be a printed response then a linefeed.
example "62.125.19.2"
if there is no match then print "nil" then a linefeed.
I ip : given an IP find the URI from the link list.
example "I 64.125.19.2"
there shall be a printed response then a linefeed.
example "www.gogle.com"
if there is no match then print "nil" then a linefeed.
N : print out the number of items in the link list then
a line feed.
? : any other command letter apart from "T" and "t"
should cause the program to terminate and print
"command error" thena linefeed.
The allowed commands can be used to provide
testing features (eg list out the link list).

* There may be several commands on the command line, for example-
- "A www.x.y 1.2.3.4 A www.a.b 7.8.9.10 N"
response "2".
- "A www.x.y 1.2.3.4 A www.a.b 7.8.9.10 U www.a.b"
response "7.8.9.10".
- "A www.x.y 1.2.3.4 A www.a.b 7.8.9.10 U www.a.B"
response "nil".
- "A www.x.y 1.2.3.4 A www.a.b 7.8.9.10 u www.a.B"
response "command error".

apart from changing this code to class, i dont know how to do U and I, as well as D...
#include <iostream>
#include <cstdlib>
using namespace std;
struct Node {	   
	   
	   char *ip;		
	   char *uri;		
	   
	   struct Node *next;
	   };
	   struct List {		
		  Node *head;
		  };
		  void optionA(List &list, char *ip, char *uri)
		   {			
		   Node * temp = new Node;		
				  
		 temp->ip = ip;		
		 temp->uri = uri;		
							 
		 temp->next = list.head;		
									  
		  list.head = temp;}
												void optionD(List &list, char *uri) { system ("pause");/* your code here */}
		 void optionU(List &list, char *uri) {
			  /* your code here */}
		  void optionI(List &list, char *ip) { /* your code here */}					  void optionN(List &list)
		   {		int count = 0;	   
		Node* current = list.head;	   
		 while (current != NULL) {				
		   count++;				
	   current = current->next;		
		   }		
	   cout<<count<<endl;
		   }
  // my function, list
 void optionL(List &list) {	   
Node* current = list.head;		
whiile (current != NULL) {			   
										
printf("%s %s\n", current->uri, current->ip); 
										//cout<<current->uri<<endl;
											  //cout<<current->ip<<endl;
												  //system ("pause");
											  current = current->next;		} 
											  
	  printf("\n");
													  }
int main(int argc, char * argv[]) 
{		List list = {NULL};	 
   for(int i=1; i<argc; i++) {
	char letter = argv[i][0];			   
	 switch (letter){					   
			 case 'A': optionA(list, argv[++i], argv[++i]); break;						
			 case 'D': optionD(list, argv[++i]); break;						
			 case 'U': optionU(list, argv[++i]); break;						
			 case 'I': optionI(list, argv[++i]); break;						
			 case 'N': optionN(list); break;						
			 case 'L': optionL(list); break;						
			 default:	  cout<<"command error"<<endl;break;							  
			  //exit(1);								
			  //break;				
			  }		
			 }  
							
			  system ("pause");
				  
			   return 0;
			   }


This post has been edited by anya_ritika: 05 May 2009 - 11:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: converting from struct to classes

#2 apw5020  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 666
  • Joined: 26-March 09

Re: converting from struct to classes

Posted 05 May 2009 - 11:57 PM

Well, do you know anything at all about classes? If not you'd probably better read up on them first.

You need to at least make some attempt at the program, and it looks like you haven't started it yet. When you do, post your code along with the errors you run into, and we will do our best to help you out.
Was This Post Helpful? 0
  • +
  • -

#3 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: converting from struct to classes

Posted 06 May 2009 - 03:22 AM

Reading here
http://www.cplusplus...ial/structures/

and here
http://www.cplusplus...torial/classes/

will help you start.
Was This Post Helpful? 0
  • +
  • -

#4 anya_ritika  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 16-March 08

Re: converting from struct to classes

Posted 09 May 2009 - 10:10 PM

i tried to convert struct to class, but not very successfulll...here is the code, gollowed by the error i get...
#include <iostream>
#include <cstdlib>
using namespace std;
class linklist
{private:

	   char ip;		
	   char uri; 
		public:
			 char Getip();
			 void Setip(char ip2);
			 char Geturi();
			 void Seturi(char uri2);
			 };
			 char linklist::Getip()
			 {
				 return ip;
				 }
				 void linklist::Setip(char ip2)
				 {
					  ip=ip2;
					  }
					  char linklist::Geturi()
					  {
						   return uri;
						   }
						   void linklist::Seturi(char uri2)
						   {
								uri=uri2;
}
		
struct Node { 
					 char *ip;
					 char *uri;	  
	linklist value;
	value.Setip(ip);
	
	value.Seturi(uri);
	   
	   struct Node *next;
	   };
	   struct List {		
			  Node *head;
			  };
			  
			  void optionA(List &list, char *ip, char *uri)
			   {		//Node *start_ptr = NULL;
					   // good		
					   Node * temp = new Node;		
					   // this doesn't make that much sense	   
						// if (start_ptr == NULL) start_ptr = temp;	   
						 // temp = start_ptr;				
						 // you haven't loaded any values		
						 // more, this is not part of the assignment		
						 // cout<<temp->ip<<endl;		
						 // cout<<temp->uri<<endl;		
						 // you forgot this		
						 temp->ip = ip;		
						 temp->uri = uri;		
						 // this is does nothing	   
						  //temp = temp->next;		
						  // Here's how to add a node to the list		
						  // next is the current head	   
						   // it doesn't matter if head is NULL	 
							  temp->next = list.head;		
							  // temp is now head		
							  list.head = temp;}
							  // I hate forword declarations
							  // you can fill them in here
							  void optionD(List &list, char *uri) { system ("pause");/* your code here */}
							  void optionU(List &list, char *uri) {
											  /* your code here */}
							  void optionI(List &list, char *ip) { /* your code here */}
							  void optionN(List &list)
							   {		int count = 0;	   
								Node* current = list.head;	   
								 while (current != NULL) {				
									   count++;				
									   current = current->next;		
									   }		
									   cout<<count<<endl;
									   }
									   // my function, list
									   void optionL(List &list) {	   
											 Node* current = list.head;		
											 while (current != NULL) {			   
													printf("%s %s\n", current->uri, current->ip); 
													  //cout<<current->uri<<endl;
													  //cout<<current->ip<<endl;
													  //system ("pause");
													  current = current->next;		} 
													  
																		  printf("\n");
																		  }
int main(int argc, char * argv[]) 
{		List list = {NULL};	 
   for(int i=1; i<argc; i++) {
	char letter = argv[i][0];			   
	 switch (letter){					   
			 case 'A': optionA(list, argv[++i], argv[++i]); break;						
			 case 'D': optionD(list, argv[++i]); break;						
			 case 'U': optionU(list, argv[++i]); break;						
			 case 'I': optionI(list, argv[++i]); break;						
			 case 'N': optionN(list); break;						
			 case 'L': optionL(list); break;						
			 default:	  cout<<"command error"<<endl;break;							  
			  //exit(1);								
			  //break;				
			  }		
			 }  
			  /*if (argv[i][0]="U")
			  {Node *it=list.head;
			  if (!strcmp(it->uri,argv[i+1]))
			  {cout<<it->ip<<endl;
			  return 1;
			  }
			  return 1;
			  }
			  }*/
			  
			  system ("pause");
				  
			   return 0;
			   }


and the error i recieved is...

Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Users\ritika\Desktop\vinnu\2009\class_link3.cpp" -o "C:\Users\ritika\Desktop\vinnu\2009\class_link3.exe" -g3 -I"lib\gcc\mingw32\3.4.2\include" -I"include\c++\3.4.2\backward" -I"include\c++\3.4.2\mingw32" -I"include\c++\3.4.2" -I"include" -L"lib" -g3
C:\Users\ritika\Desktop\vinnu\2009\class_link3.cpp:36: error: ISO C++ forbids declaration of `value' with no type

C:\Users\ritika\Desktop\vinnu\2009\class_link3.cpp:36: error: expected `;' before '.' token
C:\Users\ritika\Desktop\vinnu\2009\class_link3.cpp:38: error: ISO C++ forbids declaration of `value' with no type

C:\Users\ritika\Desktop\vinnu\2009\class_link3.cpp:38: error: expected `;' before '.' token

Execution terminated
---------------------------------------
please help me...
i really really need it...
Was This Post Helpful? 0
  • +
  • -

#5 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: converting from struct to classes

Posted 10 May 2009 - 12:30 AM

Where is the ";" at the end of your class code?
Was This Post Helpful? 0
  • +
  • -

#6 anya_ritika  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 16-March 08

Re: converting from struct to classes

Posted 10 May 2009 - 03:15 PM

there is " ; " at the end of my class code...and its giving error in lines 36 and 38...which is...
value.Setip(ip);
	
	value.Seturi(uri);



i dont know whats wrong here...please help me...please...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1