Thread: Changing do while loop

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    1

    Changing do while loop

    How can i convert this do while loop within this segment of code into a for loop or while loop. My current attempt to change the do loop seems to work somewhat, but im having a problem where it does not go to the next line.
    Code:
    ...
    while(fgets(transinput,sizeof(transinput),in)!=NULL){
        i=0;
    
        do {
                for(j=0;j<npos;j++){
                    transchar = transinput[i];
                    if(transchar != '\n' && transchar != '\0'){
                        transtemp[j] = transchar;
                        ++i;
                    } else {
                        transtemp[j] = ' ';
                    }
                }
                for(ii=0;ii<npos;ii++){
                    fprintf(out,"%c", transtemp[decrypt[ii]]);
                }
        }
        while(transchar != '\n' && transchar != '\0');
    
        fprintf(out,"\n");
    }
    ...
    My current attempt to change it

    Code:
    i=0;
    while(fgets(input3,sizeof(input3),file1)!=NULL){
        ch = input3[i];
    
    
        while(ch != '\0' && ch!='\n'){
            for(j=0;j<npos;j++){
                //ch = input3[i];
                if(ch != '\0' && ch!='\n'){
                    temp[j] = ch;
                    ++i;
                } else {
                    temp[j] = ' ';
                }
    
    
             }
             for(k=0;k<npos;k++){
                 fprintf(file2,"%c", temp[transposition[k]]);
             }
        }
    
    
        fprintf(file2,"\n");
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > My current attempt to change it
    > i=0;
    Well for a start, you initialise i outside the loop, and not inside it.

    So the 2nd time around, you're using unknown values for i.


    > //ch = input3[i];
    You need this, otherwise your loop will never end.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You might want to start simpler. Given a simple while() loop of the form
    Code:
    while (condition())
    {
         stuff();
    }
    what would be an equivalent for loop, and what would be an equivalent do/while loop?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  2. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  3. Changing ifstream's source file in a while loop
    By bengreenwood in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2009, 12:45 PM
  4. Changing windows without changing?
    By Lionmane in forum Windows Programming
    Replies: 7
    Last Post: 10-19-2005, 11:41 AM
  5. changing a for loop to a while loop
    By belfour in forum C++ Programming
    Replies: 3
    Last Post: 11-11-2002, 11:48 PM

Tags for this Thread