Solved

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

Posted on 2004-08-05
6
196 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

919 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now