Solved

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

Posted on 2004-08-05
6
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where is my core dump file in Ubuntu? 12 461
negation in C function 14 170
Line meaning 9 91
How do I set up two external hard drives with Acronis True Image? 12 103
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…
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 how to create, access, and change arrays 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.

749 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