C - Error while running .exe file that I compiled -


i used geany compile code , no errors found. when run .exe file program stops working, , i'm not programmer, work school.

my program consists of reading 2 words, in words going count how many letters each 1 has, , divides de number of letters in worda number of letters in wordb.

this code

#include <stdio.h>  int main(int argc, char *argv[]) {     int i, j;     float n;      printf ("insert first word:\n");      for(i=0; argv[1][i] != '\0'; i++);      printf ("insert second word:\n");      for(j=0; argv[2][j] != '\0'; j++);      n=i/j;      printf("%.4f", n);      return 0; } 

in line

n = i/j; 

you performing integer division. so, example, let's i 3 , j 5, perform 3/5 equals 0.

but think looking perform 3.0/5.0 , hoping answer 0.6. need perform floating point division. can force casting 1 of operands float.

n = (float)i/j; 

in question wrote int rather int. assumed transcription error when asking question. perhaps real code looks that. in case, you'll need change int compile.

the other possible problem have program expects arguments passed on command line. passing 2 arguments program? in other words need execute program this:

program.exe firstword secondword 

if not passing arguments encounter runtime errors when attempting access non-existent arguments in argv[]. @ least should add check program ensure argc==3.

if want read input stdin, rather passing command line arguments, use scanf.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -