Sponsored Content
Top Forums Programming Missing Logic Looping Through Switch Statement Post 303022567 by Azrael on Monday 3rd of September 2018 01:15:00 AM
Old 09-03-2018
Missing Logic Looping Through Switch Statement

Having trouble with the logic when looping over this switch case again:

Code:
for (j = 0; data[j] != 0; j++){
     switch(data[j]){

    case 'c': 
            output[j] = ranit(r_brace_array);
            break;
    case 'h': 
            output[j] = ranit(pipe_array);
            break;            
    case 'e':
            output[j] = ranit(tilday_array);
            break;
    case 's':
            output[j] = ranit(comma_array);
            break;
    default:    
            break;        
         }
     }


     for (i = 0; output[i] != 0; i++) {
         printf("%x\n", output[i]);
     }


It calls a function (ranit) that assigns a letter a random hex number from an array and it works fine. However, if I give this program the phrase 'cheese' you will noticed that '85f31aae' is displayed for every instance of the letter 'e':


Code:
$ ./test
Gimme something: cheese

a4d19a86
1ad33008
85f31aae
85f31aae
3927bca6
85f31aae

What I want is the function ranit() to be called again if a letter repeats. This way a repeating character does not get a repeating number. I tried making an additional outer loop, but the results did not change.

Again, had to post a code snippet due to length of code.

If anyone can help with me with the logic its greatly appreciated.
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

using getopt with a switch statement

hi all, i have been trying to get a script working that can take in more than one option using getopt. But for some reason, even when i type in a covered option, it skips directly to my error message of "no match." Any ideas of what might be wrong? set - 'getopt frd*: $*' for i in $* do... (6 Replies)
Discussion started by: gammarays
6 Replies

2. Programming

Comapilation error with Switch statement

Hello , How to resolve below compilation error.activity_type is a member of structure and the output from databse will be stored in structure.Expected output wil l be either D or N or C . sample struct format: struct a{ char acAtivity_type; } code: switch (a->activity_type)... (1 Reply)
Discussion started by: jagan_kalluri
1 Replies

3. Shell Programming and Scripting

Tcl switch statement

I am just learning Tcl and there are few things about it that is perplexing me. I have a question about the switch statement. Why are these two switch statements giving me different results? $ cat test_switch.tcl #!/usr/bin/tcl set foo "abc" switch abc a - b {puts "No. 1"} $foo {puts... (2 Replies)
Discussion started by: SFNYC
2 Replies

4. Shell Programming and Scripting

Missing Assigned Variable within logic operator

Hey , I'm trying to perform the following command, however it cannot read the variable assigned earlier. I'm not sure why this happen. Please help thanks while : do echo "what's ur name? (if none just press )" read name changeName = echo $name | sed "s/on/ey/" echo $changeName #this... (8 Replies)
Discussion started by: sexyTrojan
8 Replies

5. 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

6. Emergency UNIX and Linux Support

Looping using cut statement

i want to create loop for below mentioned A1=`echo $obj1 | cut -d "," -f3` A2=`echo $obj1 | cut -d "," -f4` A3=`echo $obj1 | cut -d "," -f5` A4=`echo $obj1 | cut -d "," -f6` A5=`echo $obj1 | cut -d "," -f7` A6=`echo $obj1 | cut -d "," -f8` A7=`echo $obj1 | cut -d "," -f9` A8=`echo $obj1... (3 Replies)
Discussion started by: pasricha.kunal
3 Replies

7. Shell Programming and Scripting

Logic needed to recursive looping in the script

Hello i have a requirement where in a file i will get string. The length could be from 1 to 20. if the string is less than 6 characters ( ex: ABCD) . i need to append 'X' on right hand side to make it 6 characters (ex: ABCDXX). if suppose i get the same string from the file as ABCDXX then i... (5 Replies)
Discussion started by: dsdev_123
5 Replies

8. Programming

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 #include <stdlib.h> main(int argc, char* argv) { int num=0; if ( argc == 2 ) num = argv; printf("%d is the num value",num); switch ( num ) ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

9. UNIX for Dummies Questions & Answers

Looping Logic, Need to implement

I need to implement a looping logic. If then Go to /path1/file* Get all the filename starting with file* and store it in a array count file number and store it in variable like run Ex: I found 3 file with starting file* so my run = 3 means my loop should run three time May be... (1 Reply)
Discussion started by: Nsharma3006
1 Replies
getopt(3C)																getopt(3C)

NAME
getopt(), optarg, opterr, optind, optopt - get option letter from argument vector SYNOPSIS
DESCRIPTION
returns the next option letter in argv (starting from that matches a letter in optstring. argc and argv are the argument count and argu- ment array as passed to optstring is a string of recognized option characters; if a character is followed by a colon, the option takes an argument which may or may not be separated from it by whitespace. is the index of the next element of the vector to be processed. It is initialized to 1 by the system, and updates it when it finishes with each element of returns the next option character from argv that matches a character in optstring, if there is one that matches. If the option takes an argument, sets the variable to point to the option argument as follows: o If the option was the last character in the string pointed to by an element of argv, then contains the next element of argv, and is incremented by 2. If the resulting value of is greater than or equal to argc, this indicates a missing option argument, and returns an error indication. o Otherwise, points to the string following the option character in that element of argv, and is incremented by 1. If, when is called, is NULL, or the string pointed to by either does not begin with the character or consists only of the character returns -1 without changing If points to the string returns -1 after incrementing If encounters an option character that is not contained in optstring, it returns the question-mark character. If it detects a missing option argument, it returns the colon character if the first character of optstring was a colon, or a question-mark character otherwise. In either case, sets the variable to the option character that caused the error. If the application has not set the variable to zero and the first character of optstring is not a colon, also prints a diagnostic message to standard error. The special option can be used to delimit the end of the options; -1 is returned, and is skipped. RETURN VALUE
returns the next option character specified on the command line. A colon is returned if detects a missing argument and the first character of optstring was a colon A question-mark is returned if encounters an option character not in optstring or detects a missing argument and the first character of optstring was not a colon Otherwise, returns -1 when all command line options have been parsed. EXTERNAL INFLUENCES
Locale The category determines the interpretation of option letters as single and/or multi-byte characters. International Code Set Support Single- and multibyte character code sets are supported. ERRORS
fails under the following conditions: [EILSEQ] An invalid multibyte character sequence was encountered during option processing. EXAMPLES
The following code fragment shows to process arguments for a command that can take the mutually exclusive options and and the options and both of which require arguments: #include <stdio.h> #include <unistd.h> main (int argc, char *argv[]) { int c; int bflg, aflg, errflg; extern char *optarg; extern int optind, optopt; . . . while ((c = getopt(argc, argv, ":abf:o:")) != -1) switch (c) { case 'a': if (bflg) errflg++; else aflg++; break; case 'b': if (aflg) errflg++; else { bflg++; bproc( ); } break; case 'f': ifile = optarg; break; case 'o': ofile = optarg; break; case ':': /* -f or -o without arguments */ fprintf(stderr, "Option -%c requires an argument ", optopt); errflg++; break; case '?': fprintf(stderr, "Unrecognized option: - %c ", optopt); errflg++; } if (errflg) { fprintf(stderr, "usage: . . . "); exit(2); } for ( ; optind < argc; optind++) { if (access(argv[optind], 4)) { . . . } WARNINGS
Options can be any ASCII characters except colon question mark or null SEE ALSO
getopt(1), thread_safety(5). STANDARDS CONFORMANCE
getopt(3C)
All times are GMT -4. The time now is 02:17 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy