Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-08-05
6
Medium Priority
?
205 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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

597 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