Sponsored Content
Full Discussion: While loop problem in C
Top Forums Programming While loop problem in C Post 302736853 by Raj 89 on Wednesday 28th of November 2012 04:47:59 AM
Old 11-28-2012
Quote:
Originally Posted by gabam

Code:
 

    int cont = 1;
 
    while( cont == 1 )   
    {
             printf( "Enter i\n" );
             scanf( "%d", &i );
 
             switch( i )
             {
                    case 1:
                              printf( "1\n" );  
                              cont = 0;
                              break;
                    case 2:
                              printf( "2\n" );
                              cont = 0;
                              break;
                    default:
                              printf( "Invalid value" );
             }
    }

Your program is working perfectly fine.I am not able to understand your logic clearly. What is that you are trying to do. You have normally written the while loop in such a way that it will be exited whenever the value of cont is other than 1. So at first the while loop satisfies and the user provides the input for "i".
But when you provide 1 or 2 for the switch case, the value const will be replaced with zero and then the execution goes to while. This will act as an infinite loop till the value of i tends to be other than 1 r 2 .
but if the value of i is other than 1 or 2, cont will be same and the while loop fails the second time.

---------- Post updated at 03:17 PM ---------- Previous update was at 03:15 PM ----------

Quote:
Originally Posted by gabam

Your code is replaced into this. Well this works better.

Code:
 
int main()
{
    int i = 0;
    int cont = 1;
 
    while( cont == 1 ) 
    {
             printf( "Enter i\n" );
             scanf( "%d", &i );
 
             switch( i )
             {
                    case 1:
                              printf( "1\n" );
                              break;
                    case 2:
                              printf( "2\n" );
                              break;
                    default:
                              printf( "Invalid value" );
                              cont = 0 ;
             }
    }
 
    return 0;
}

Maybe this could help you...
 

10 More Discussions You Might Find Interesting

1. Programming

problem with while loop

hi all, i have written the following code: while(proceed !='Y' && proceed!='N' && proceed!='y' && proceed!='n') { printf("\nPress \n\t 'Y' or 'y' to continue \n\t 'N' or 'n' to cancel:"); scanf("%c",&proceed); } the output i am gettin is: Press 'Y' to continue ... (1 Reply)
Discussion started by: mridula
1 Replies

2. Shell Programming and Scripting

for loop problem

Hi, I have a directory called logs in which i have the log files. i have to touch the file before deleting it. i am doing like this filestodelete="*.log* *log*" for files in $filestodelete do touch $files $files.$(date +%a) rm -f $filestodelete done touch is not working... (5 Replies)
Discussion started by: namishtiwari
5 Replies

3. UNIX for Advanced & Expert Users

loop problem

Hi guys my while do loop is not working properly; As soon as the load_date and run_date is same it should stop can somebody tell me where I am having the problem? In an oracle table I have LOAD_DT=5/1/2009 DATE datatype RUN_DT=5/5/2009 DATE datatype Now in a script with a spool file I get the... (15 Replies)
Discussion started by: henrysmith
15 Replies

4. Shell Programming and Scripting

Problem in While loop

Hi Guys, This is my code.. I am getting an error in the inside while statement when comparing.. I am not able to figure it out.. Pls help me... while read value fatherid son_id top_id r_type do inside_value=1; echo $inside_value; echo $r_type; while do echo... (6 Replies)
Discussion started by: mac4rfree
6 Replies

5. Shell Programming and Scripting

problem with while loop

hi I had created a while loop in a script file called whiletest.sh as follows as follows: count=0 max=10 while do echo $count echo count=$(count+1)" " done echo "value of count:$count" then i run the script with the command sh whilestest.sh but its giving me an error... (5 Replies)
Discussion started by: angel12345
5 Replies

6. UNIX for Dummies Questions & Answers

While loop problem

I have a while loop with -f and -o option.Can any one please tell me what that stands for?The Sample code is as follows:- while do ## some processing done (10 Replies)
Discussion started by: Param0073
10 Replies

7. Shell Programming and Scripting

Problem with While loop.

Hi Script Gurus, I am facing issue with while loop in bash. The while loop is running as end less loop even after given criteria does not meet. STATE_BEFORE_SPLIT () { for clone in $Clonegroups; do state=$( $Navicmd -listclone -name $clone -cloneid $Cloneid |awk... (3 Replies)
Discussion started by: RobP
3 Replies

8. Shell Programming and Scripting

Problem with loop within loop

Hi, I work on Ab-initio ETL tool which is based on Unix. I made a small script which has two loop's one with in another. All the functionality is working for the first line of outer loop but when it comes to other lines of outer loop it is throwing error as command not found. Below is the... (4 Replies)
Discussion started by: Ravindra Swan
4 Replies

9. Shell Programming and Scripting

Problem with While Do loop

I am trying to do a while do loop that asks for a password and returns if the password entered is incorrect. I have that part working however i want to modify it so that if a similar word is entered it will read "that is close but incorrect" and i cannot seem to get it working, being new in the... (6 Replies)
Discussion started by: lm5522
6 Replies

10. Shell Programming and Scripting

Problem with loop

Hi all, a problem with a loop. Imagine it starts with var1="$(cat txtif.out )"while do echo file1 echo file2 echo y > txtif.out if then break fi done exit 0 the problem is that if the file is changing during the loop seems to continue as it reads "x" state,... (5 Replies)
Discussion started by: Board27
5 Replies
SMIME_read_CMS(3SSL)						      OpenSSL						      SMIME_read_CMS(3SSL)

NAME
SMIME_read_CMS - parse S/MIME message. SYNOPSIS
#include <openssl/cms.h> CMS_ContentInfo *SMIME_read_CMS(BIO *in, BIO **bcont); DESCRIPTION
SMIME_read_CMS() parses a message in S/MIME format. in is a BIO to read the message from. If cleartext signing is used then the content is saved in a memory bio which is written to *bcont, otherwise *bcont is set to NULL. The parsed CMS_ContentInfo structure is returned or NULL if an error occurred. NOTES
If *bcont is not NULL then the message is clear text signed. *bcont can then be passed to CMS_verify() with the CMS_DETACHED flag set. Otherwise the type of the returned structure can be determined using CMS_get0_type(). To support future functionality if bcont is not NULL *bcont should be initialized to NULL. For example: BIO *cont = NULL; CMS_ContentInfo *cms; cms = SMIME_read_CMS(in, &cont); BUGS
The MIME parser used by SMIME_read_CMS() is somewhat primitive. While it will handle most S/MIME messages more complex compound formats may not work. The parser assumes that the CMS_ContentInfo structure is always base64 encoded and will not handle the case where it is in binary format or uses quoted printable format. The use of a memory BIO to hold the signed content limits the size of message which can be processed due to memory restraints: a streaming single pass option should be available. RETURN VALUES
SMIME_read_CMS() returns a valid CMS_ContentInfo structure or NULL if an error occurred. The error can be obtained from ERR_get_error(3). SEE ALSO
ERR_get_error(3), CMS_type(3) SMIME_read_CMS(3), CMS_sign(3), CMS_verify(3), CMS_encrypt(3) CMS_decrypt(3) HISTORY
SMIME_read_CMS() was added to OpenSSL 0.9.8 1.0.0e 2008-04-10 SMIME_read_CMS(3SSL)
All times are GMT -4. The time now is 09:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy