Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › converting do loop to for loop
New Posts  All Forums:Forum Nav:

converting do loop to for loop

post #1 of 4
Thread Starter 
writing a program using a for loop this is the code i have so far
Code:






#include<stdio.h>

int funny_man(char*, char*);

int main()

{
 int max = 10000;
 char satu[max];
 char dua[max];
 char c;
 char result;
 int i;


do
{


    printf("Please Enter First Sentence:",satu);
    fgets(satu , max , stdin );


    printf("\nPlease Enter Second Sentence:",dua);
    fgets(dua , max , stdin);

    result = funny_man(satu, dua);

    if ( result == 0 )
        {
       printf("Both strings are the same.\n");
        }
    else
        {
       printf("Entered strings are not equal.\n");
       printf( "The location of the error is at: %d\n", result);
        }

{


        printf( "Press 'y' to Continue or press 'n' to Exit.\n" );
        scanf(" %c%*c",&c);
        while( !( c == 'y' || c == 'n'))
            {

         printf( "Invalid Character Entered!\n" );
         printf( "Please press 'y' to Continue or press 'n' to Exit.\n" );
         scanf(" %c%*c",&c);
        }
}
}
             while (c != 'n');

    return 0;
}

int funny_man(char *satu, char *dua)
{
    int kira  = 1;

   while(*satu==*dua)
   {
      if ( *satu == '\0' || *dua == '\0' )
         break;

      satu++;
      dua++;
      kira++;
   }
   if( *satu == '\0' && *dua == '\0' )
      return 0;
   else
      return kira;


    return 0;
}

 i just finnish writing this code ...... i want it to do the same thing but with a for loop....  is it possible and how...pleas help me tq....

















post #2 of 4
It's possible, but it doesn't really make any sense given the way you've currently written the code as you've got an infinite loop which only exits when users choose 'n'. That menu wouldn't really be required and you'd be locking the users into a specified number of iterations.

Why do you want a for loop? Assignment question?
post #3 of 4
In your current code, i would do this

while(true)
{
... stuff

if(escape character pressed) break;

}



Edit: I misread the code, and yeah I am not sure why you would add a for loop. The purpose of the for loop is to iterate trough something. To define, X tries or X items, its to repeat for a reason, and i see no limiting factor. So, you could do a for loop like for(int i = 0; i < 10 ; i++) and run trough your code at max 10 times, then you instantly close, but you can also give the option on the escape character being pressed to "break" from the loop at any point.
Edited by Mrzev - 4/9/14 at 2:06pm
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
Zev's Comp
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5-2500K Sandy Bridge 3.3GHz GIGABYTE GA-Z68X-UD3H-B3 LGA 1155 Intel Z68 HDM... GeForce GTX 750 Ti G.SKILL Ripjaws X Series 8GB 
Hard DriveHard DriveHard DrivePower
1TB HDD 64GB SSD (Used for SRT) 500 GB. Antec BP550 Plus 550W Continuous Power ATX12V V... 
Case
COOLER MASTER ELITE 335 RC-335-KKN1-GP Black S... 
  hide details  
Reply
post #4 of 4
The point of a for loop is mechanical progression when there is an endpoint.

A while loop can be converted to a for loop (with or without a break) for example.

This pseudo-code may work, but you have to redo your entire logic. I am not saying this is the best way to do it, but here are couple of For loop ideas.
Code:
//For Loop Reads String One, stop at UserString1.Length, stores in Character Array1
//For Loop Reads String Two, stop at UserString2.Length, stores in Character Array2
//For Loop Compares Array1 to Array2, stops at Array1.Length, or Array2.Length, which ever is longer/shorter, with out of bounds index checking.
//Results are stored in a Boolean Array.
//BooleanArray CharIsTheSame, stores a T/F in every index matching the compares above.
//If all true, Strings the same, if not, False indicates Characters that are different and location.
Snowdevil
(16 items)
 
ASUS G750JM
(9 items)
 
 
CPUMotherboardGraphicsGraphics
[i7 4790K @ 4.4 GHz (1.186v)] [Asus Sabertooth Z97 Mark S] [nVidia Geforce GTX 1080] [nVidia Geforce GTX 1080] 
RAMHard DriveCoolingOS
[G.Skill 32GB DDR3 2133 MHz] [Crucial MX100 256GB] [Phanteks PH-TC12DX] [Win 10.1 Pro] 
MonitorMonitorKeyboardPower
[LG 29UM65 (2560x1080)] [QNIX Evo II LED (2560x1440)] [WASD v2 Tenkeyless] [NZXT Hale90 v2 ] 
CaseMouseMouse PadAudio
[ThermalTake GT10 Snow Edition] [Razer Mamba - Chroma] [Razer Kabuto] [Razer Man O' War] 
CPUMotherboardGraphicsRAM
i7 4770HQ Intel HM87 Express Chipset Geforce GTX 860M 8GB DDR3L 1600 MHz 
Hard DriveOptical DriveCoolingOS
Samsung SSD EVO DVD-RW Stock Windows 8.1 
Monitor
1920x1080 TN 
  hide details  
Reply
Snowdevil
(16 items)
 
ASUS G750JM
(9 items)
 
 
CPUMotherboardGraphicsGraphics
[i7 4790K @ 4.4 GHz (1.186v)] [Asus Sabertooth Z97 Mark S] [nVidia Geforce GTX 1080] [nVidia Geforce GTX 1080] 
RAMHard DriveCoolingOS
[G.Skill 32GB DDR3 2133 MHz] [Crucial MX100 256GB] [Phanteks PH-TC12DX] [Win 10.1 Pro] 
MonitorMonitorKeyboardPower
[LG 29UM65 (2560x1080)] [QNIX Evo II LED (2560x1440)] [WASD v2 Tenkeyless] [NZXT Hale90 v2 ] 
CaseMouseMouse PadAudio
[ThermalTake GT10 Snow Edition] [Razer Mamba - Chroma] [Razer Kabuto] [Razer Man O' War] 
CPUMotherboardGraphicsRAM
i7 4770HQ Intel HM87 Express Chipset Geforce GTX 860M 8GB DDR3L 1600 MHz 
Hard DriveOptical DriveCoolingOS
Samsung SSD EVO DVD-RW Stock Windows 8.1 
Monitor
1920x1080 TN 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › converting do loop to for loop