-1

The source code is given bellow, What does the line of if(getch()==13) means.. int main()
{

int a[4],approx[4],b[4],i=0;  
int arr[4][6],sarr[4][6];  
initialize(a);  
encoding(a,arr);  

while(i<5)  

{  

            if(i==0)  
    {  
        while((selection(a,approx,sarr,arr,b))==0)  
        {  

            initialize(a);  
            encoding(a,arr);  
        }  
    }  
    else  
        selection(a,approx,sarr,arr,b);  
    crossover(sarr,a,b,arr);  
    if(getch()==13)  

    mutation(arr);  
    evaluate(arr,a);  
    getch();  
    i++;  
}  

getch();  

}
What does the line of code if(getch()==13) means..

NAT
  • 9
  • 3
  • 4
    ASCII 13 == carriage return. getch() is get a single character of input from the user. Basically checking if the user hit Enter. – jeff carey Feb 18 '17 at 06:15
  • 3
    Ooo, wouldn't that only work on DOS systems with `\r\n` newlines? – qxz Feb 18 '17 at 06:16
  • Yes, and getch() is also non-standard. If this doesn't currently work on your system, I'd recommend trying to replace the line with getchar()==10 – jeff carey Feb 18 '17 at 06:23
  • 2
    Or even better, with `getchar()=='\n'` for readability's sake – qxz Feb 18 '17 at 06:45

3 Answers3

0

The line

if (getch() == 13)

means whether the value taken as input from getch() is equal to 13 or not.

Note -
getch() is a non-standard functions defined in conio.h header library, mostly used in turbo C/dev C++ environement. getchar() is standard functions defined in C standard and they can be used in all environments

Cool Breeze
  • 738
  • 2
  • 10
  • 26
  • 1
    True, but what does _that_ mean? – qxz Feb 18 '17 at 06:47
  • getch() is a non-standard functions defined in conio.h header library, mostly used in turbo C/dev C++ environement. getchar() is standard functions defined in C standard and they can be used in all environments. – Cool Breeze Feb 18 '17 at 06:49
0

The C library function int getchar() gets a character (an unsigned char) from stdin. Here the line if (getch() == 13) checks if the user has entered a carriage return character (13 is the ascii value of CR).

VHS
  • 9,534
  • 3
  • 19
  • 43
  • the code is like- **if(getch()==13) ** **mutation(arr);** **getch(); ** can you plz elaborate how its working? @VHS – NAT Feb 18 '17 at 06:39
  • I just saw your edits. All the `if` condition does is call a function call `mutation()` if the user has entered a carriage return. There's nothing special. – VHS Feb 18 '17 at 16:26
-1

the return value of getchar() is equal ASCII

4fun
  • 1
  • 1