Passing arguments from command line to switch case statement in C


 
Thread Tools Search this Thread
Top Forums Programming Passing arguments from command line to switch case statement in C
# 1  
Old 05-02-2013
Passing arguments from command line to switch case statement in C

Hi
Am pretty new to C..
Am trying to pass the arguments from command line and use them in switch case statement..
i have tried the following
Code:
#include <stdlib.h>
main(int argc, char* argv[])
{
int num=0;
if ( argc == 2 )
num = argv[1];
printf("%d is the num value",num);
switch ( num )
        {
            case 1:            /* Note the colon, not a semicolon */
             printf("Playing the game\n");
             break;
            case 2:
                printf("Loading the game\n");
                break;
            default:
                printf("The value is not correct\n");
                break;
}
}

I get the output as
Code:
# ./wq 1
804400053 is the num valueThe value is not correct

Please help..

Last edited by Priya Amaresh; 05-02-2013 at 01:47 AM..
# 2  
Old 05-02-2013
The problem is:
Code:
num = argv[1];

num is an int, but argv[1] is a char* as declared in the main function. Those are not compatible data types. 804400053 is the address of argv[1] and is just a memory address inside the computer. You cannot assign argv[1] to num and expect to get a meaningful result.

Try using atoi or sscanf to convert argv[1] to num.
# 3  
Old 05-02-2013
Please find the corrected version, only u need to convert the char to atoi(), that was missing. Rest all are fine... keep going
Code:
#include <stdio.h>
#include <stdlib.h>
main(int argc, char* argv[])
{
        int num=0;
        if ( argc == 2 )
                num = (int) atoi(argv[1]);
        else
        {
                printf("Usage: a.out <<input>>\n");
                return -1;
        }
        printf("%d is the num value\n", num);
        switch ( num )
        {
                    case 1:            /* Note the colon, not a semicolon */
                     printf("Playing the game\n");
                     break;
                    case 2:
                        printf("Loading the game\n");
                        break;
                    default:
                        printf("The value is not correct\n");
                        break;
        }
}

Code:
$ a.out
argc : 1 - a.out
Usage: a.out <<input>>

$ a.out 1
argc : 2 - a.out
1 is the num value
Playing the game

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash case Statement and Using Line Anchors?

Hello All, I am writing a script that is to be placed on multiple servers, and of course I've started running into some compatibility issues for certain shell commands. The code below worked just fine on most of my machines except for a couple. Here I had 4 separate lines in my script that... (3 Replies)
Discussion started by: mrm5102
3 Replies

2. UNIX for Dummies Questions & Answers

passing command arguments from a file?

Hi Im trying to run zip shell command from an Oracle job, but this has limitations. This should take a few of explanaition,.. Oracle allows me to execute a command and then I can set up a fixed number of arguments. Ex: (summarizing in something like..): JOB DEFINITION job_name: test... (4 Replies)
Discussion started by: vegatripy
4 Replies

3. Shell Programming and Scripting

case statement for different cmd arguments

Hello friends, I have a boubt passing different arguments at a time for any one option in below code. I would also like to check which option has been selected (any one of i, r, u ) so that whether or not matching argument passed can be verified. for i and r - install and re-install -... (4 Replies)
Discussion started by: pd2
4 Replies

4. Shell Programming and Scripting

Passing arguments from a bash shell script to a command

I'm pretty new to bash scripting and I've found myself writing things like this (and the same with even more nesting): if $CATEGORIES; then if $LABEL_SLOTS; then $pyth "$wd/texify_grammar.py" "$input" "$texfile" "--label-slots" "--categories" "$CATEGORY_LIST" ... (9 Replies)
Discussion started by: burbly
9 Replies

5. Shell Programming and Scripting

How to use case and command line arguments in shell script?

Hi... can anyone please help me out in using the CASE and command line argument in shell script... i am bit new to shell scripting...below i have explained my proble with example... say i have an executable file with name 'new1.sh' and there are 3 functions in it a(), b() and c()....and there... (5 Replies)
Discussion started by: swap21783
5 Replies

6. Shell Programming and Scripting

Passing wildcards to cammand line arguments

I have a csh script plot-model.csh I want to be able to pass wildcards for the input file names such as ./plot-model.csh *8x6smp.cmod Currently I have to pass the full files names one after each other which I then store in a list. Then I proceed to plot the data within it. Now I... (7 Replies)
Discussion started by: kristinu
7 Replies

7. Shell Programming and Scripting

Something is wrong with this switch( case statement.

I started writing a script to save the files from a camera I got the other day, which mounts in /Volumes , and I got into it and started building this menu. The only problem is that the switch case is coming up as a syntax error at the parenthesis after a case. Here is the code: while : do ... (2 Replies)
Discussion started by: snakemasterAK
2 Replies

8. Shell Programming and Scripting

passing arguments in remote ssh command?

i have a bash script on serverA, when i run the script on the server, it runs fine. the way to run the script is like this ./script "option1" Now i am trying to call it from local laptop using ssh command, my command is as the following ssh serverA ~/script "option1" and i got error... (7 Replies)
Discussion started by: fedora
7 Replies

9. Shell Programming and Scripting

[BASH] recognise new line regex in case statement

Hi, I'm trying to write a routine to parse a file that contains data that will be read into arrays. The file is composed of labels to identify data types and arbitrary lines of data with the usual remarks and empty new lines as is common with config files. The initial pass is built as so:... (3 Replies)
Discussion started by: ASGR
3 Replies

10. UNIX for Dummies Questions & Answers

Case statement/sed command

The file dbnames.txt has 5 columns, what i'm trying to do is that when the fifth column equals A, store in the variable "access" the word, "admin access". If it equals B, then "business access" etc. I think their is a problem with my sed command, because it is not substibstituting the words... (1 Reply)
Discussion started by: ross_one
1 Replies
Login or Register to Ask a Question