I have to sum two number while first number is not equal to -1, and the numbers have just one digit. I have to use read() and write().
#include <unistd.h>
#include <errno.h>
int main()
{ int Somma;
int One;
int Two;
do
{ write(1, "\nFirst Number: ", 15);
if(read(0, &One, sizeof(int)) == -1)
perror("Error First Read");
if(One != -1)
{ write(1, "Second Number: ", 15);
if(read(0, &Two, sizeof(int)) == -1)
perror("Error Second Read");
Somma = One + Two;
Somma -= 48;
write(1, "Sum: ", 5);
if(write(1, &Somma, sizeof(int)) == -1)
perror("Error Write");
}
}while(One != -1);
return 0;
}
Now, I have some problems. First of all, when One is equal to -1, the program continues into if statement... The second one is that the last write(), print the number and a strange characters (a square with 0014 code into...). What's wrong?
Thank you in advance