Solved

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

762 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

18 Experts available now in Live!

Get 1:1 Help Now