Wrong print with counting letters of words

I get an enormus number as answer while counting words' letters

Page 1 of 1

2 Replies - 770 Views - Last Post: 10 November 2009 - 03:28 PM Rate Topic: -----

#1 Auron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-November 09

Wrong print with counting letters of words

Post icon  Posted 09 November 2009 - 05:08 AM

I read some topics but i couln't find something helpfull.

I want to create a code that: i create a "child" procedure that will comunicate with the "parent" procedure with pipes and they will do the following:

1)the "parent" procedure asks the user for a word.
2)the "parent" writes the word on a pipe.
3)the "child" reads the word and finds its length(number of characters).
4)the "child" writes the number on a pipe
5)the "parent" reads the number and sum the number of characters (of all words) that have been typed since now.
6)the program ends when the user types END.

My code till now is:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>

main(){
int fd1[2],fd2[2],pid,meg,sum,a;
char lexi[100];

pipe(fd1);
pipe(fd2);
pid = fork();

if (pid == 0){
   close(fd1[1]);
   close(fd2[0]);
   while(1){
	  read(fd1[0], lexi, 100);

	  meg=strlen(lexi);
	 
	  write(fd2[1], meg, strlen(meg);
   }
}
else{
   close(fd1[0]);
   close(fd2[1]); 
   while(1){
	  printf("Please enter a word or press END to finish program. \n");
	  scanf("%s",lexi);

	  write(fd1[1], lexi, strlen(lexi);

	  if (!strcmp(lexi,"END")){
		 printf("The sum of the letters from the words you entered is %d. \n",sum);
		 exit(1); 
	  }

	  read(fd2[0], a, strlen(meg);

	  sum=sum+a;	
   }
}
}


I can type as many word as i want but the problem is that at the end when my program prints the number of letters i get an enormus number.Like:

Please enter a word or press END to finish program.
tralala
Please enter a word or press END to finish program.
lala
Please enter a word or press END to finish program.
END
The sum of the letters from the words you entered is -1208269200.

The problem is the number. :blink:

I'm working on ubuntu 8.1.I don't know if that changes anything.

This post has been edited by Auron: 09 November 2009 - 05:13 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Wrong print with counting letters of words

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2014
  • View blog
  • Posts: 5,395
  • Joined: 27-December 05

Re: Wrong print with counting letters of words

Posted 09 November 2009 - 09:11 PM

You must have posted the wrong version of your program, because the code posted here has a few errors that prevent it from compiling. But your problem with the value of sum is that you didn't initialize it to 0.
Was This Post Helpful? 0
  • +
  • -

#3 Auron  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-November 09

Re: Wrong print with counting letters of words

Posted 10 November 2009 - 03:28 PM

Thanks a lot.I've fixed some other errors and i put sum=0; as you told me and it works fine.
:D B)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1