Solved

Examining Two inputs if any of them in enum {} then assign it to a 3rd VAR.

Posted on 2004-08-05
6
200 Views
Last Modified: 2010-04-15
Hi there,
I'm trying to examin two variables and check if any of them has a value that is in an enum {23,80,25} then I will assign that variable to a 3rd variable; follow this example:

enum {23,80,25,21}

dst_port = 23; //telent
src_port=2000;
I have to examin dst_port and src_port for any value that is in enum;

  then service=dsp_port;

other chance:
dst_port=2000;
src_port=23
  then
    service=src_port;


Hopefuly it's clear.
I'm expecting the replys in c lang.


Even a tiny help is highly appreciated.

Ahmed.
0
Comment
Question by:thagafi
  • 2
6 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11732727
Hi thagafi,

> I have to examin dst_port and src_port for any value that is in enum;
you will have to use switch case

switch(dst_port)
{
       case 1:
            //do something
            break;
       case 2:
            //do something
            break;
       case 3:
            //do something
            break;
       case 4:
            //do something
            break;
       default:
            printf ("value not found\n");
}

same for src_port

Sunnycoder
0
 

Expert Comment

by:jinumjoy
ID: 11732743
y do u want the ports in enum? take it in an array..

#define NUM_OF_PORTS
int valid_ports[NUM_OF_PORTS] = {23,21...};

then call this function with the port u want to validate..

int isPortValid(int port)
{
   for(i=0;i<NUM_OF_PORTS;i++)
      if(valid_ports[i] == port)
            return 1; //valid port

  return 0; // invalid port
}

int main()
{
  int dst_port = 23; //telent
  int src_port=2000;
  int service;

  if(isPortValid(dst_port))
      service = dst_port;
  else if(isPortValid(src_port))
      service = src_port;
  else
      return -1; // src_port & dst_port not valid

  // use service from here....
  ...
  ...
  ...
}

0
 

Expert Comment

by:jinumjoy
ID: 11732754
I ofcourse dint compile the code.. int i in isPortValid() is missing..
u be the compiler.. ;) but this is surely better approach for ur requirement..

-Jinu
0
 
LVL 8

Accepted Solution

by:
ssnkumar earned 50 total points
ID: 11732882
Use enum like this:
enum PORT {port1 = 23, port2 = 25, port3 = 80}

Here is a sample program which demonstrates what I am trying to tell:

#include<stdio.h>

enum PORT { port1 = 23, port2 = 25, port3 = 80 };

main()
{
        int myport = -1;

        printf("Enter PORT No. => ");
        scanf("%d", &myport);

        switch (myport)
        {
        case 23:
                printf("Telnet - Port = 23\n");
                break;
        case 25:
                printf("smtp - Port = 25\n");
                break;
        case 80:
                printf("http - Port = 80\n");
                break;
        default:
                printf("Invalid Port = %d!\n", myport);
                break;
        }

        exit(0);
}

Hope this helps.....

-ssnkumar
0

Featured Post

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question