Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
April 10th, 2013, 02:40 PM
 LivingL
Registered User

Join Date: Apr 2013
Posts: 2
Time spent in forums: 29 m 32 sec
Reputation Power: 0
C Simple child and parent back and forth

Im creating a small guess game between a child and a parent: The child takes in the input from user and makes that the number to guess, the parent simply goes into a loop trying to figure out which is the right number.
I can't get it to work fully. Also you can see I'm keeping track of how many times it takes, once its found, make a new one and then exits after 4 rounds.

Code:
```#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>

int main() {
int pid, nbytes, i, input;
int fd[2] = {} ;
int guess = 0;
pid = fork();
int x;

srand((unsigned) time(NULL));
x = (rand() % 10 + 1);

if (pid == 0)
{
close(fd[0]);
printf("enter a number ");
scanf("%d", &input);

write(fd[1], &input, (sizeof(input)));

if (x == input)
{
exit(0);
}

close(fd[1]);
}

else
{
//PARENT
close(fd[1]);

while (x != input)
{
if (x != input)
{
guess++;
}
}

if (x == input)
{
printf(" correct");
printf("Number of Guesses: %d\n", guess);
exit(0);
}

//to reset random

x = (rand() % 10 + 1);
close(fd[0]);   } }```

#2
April 10th, 2013, 02:59 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,858
Time spent in forums: 3 Months 1 Day 5 h 50 m 16 sec
Reputation Power: 2199
For one thing, format your code! The reason to use code tags is so that your code will not lose its formatting. That jumbled mess that you posted is worse than not using code tags. Whatever demon possessed you to hide all your braces at the ends of lines like that?

Code:
```#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>

int main()
{
int pid, nbytes, i, input;
int fd[2] = {} ;
int guess = 0;
pid = fork();
int x;
srand((unsigned) time(NULL));
x = (rand() % 10 + 1);
if (pid == 0)
{
close(fd[0]);
printf("enter a number ");
scanf("%d", &input);
write(fd[1], &input, (sizeof(input)));
if (x == input)
{
exit(0);
}
close(fd[1]);
}
else
{
//PARENT
close(fd[1]);
while (x != input)
{
if (x != input)
{
guess++;
}
}
if (x == input)
{
printf(" correct");
printf("Number of Guesses: %d\n", guess);
exit(0);
}
//to reset random
x = (rand() % 10 + 1);
close(fd[0]);
}
}```

Now that it's in a readable form, it will be more likely for somebody to read it. Nobody would have wanted to bother with the original mess.

Also, that is not C. It will not compile as C.
Quote:
 C:TEST>gcc -Wall guess.c guess.c: In function `main': guess.c:13: warning: implicit declaration of function `fork' guess.c:14: parse error before `int' guess.c:16: `x' undeclared (first use in this function) guess.c:16: (Each undeclared identifier is reported only once guess.c:16: for each function it appears in.) guess.c:41: parse error before `char' guess.c:10: warning: unused variable `i' C:TEST>

If you are using an odd variant like C99, then inform us of that fact. It will save this forum the bandwidth of us having to come back to you to inform you that your program won't even compile.

And you forgot main's return statement.

Last edited by dwise1_aol : April 10th, 2013 at 03:04 PM.

#3
April 10th, 2013, 03:47 PM
 LivingL
Registered User

Join Date: Apr 2013
Posts: 2
Time spent in forums: 29 m 32 sec
Reputation Power: 0
Quote:
 Originally Posted by dwise1_aol For one thing, format your code! The reason to use code tags is so that your code will not lose its formatting. That jumbled mess that you posted is worse than not using code tags. Whatever demon possessed you to hide all your braces at the ends of lines like that? Code: ```#include #include #include #include char readbuffer[100]; int main() { int pid, nbytes, i, input; int fd[2] = {} ; int guess = 0; pid = fork(); int x; srand((unsigned) time(NULL)); x = (rand() % 10 + 1); if (pid == 0) { close(fd[0]); printf("enter a number "); scanf("%d", &input); write(fd[1], &input, (sizeof(input))); if (x == input) { exit(0); } close(fd[1]); } else { //PARENT close(fd[1]); nbytes = read(fd[0], &input, sizeof(input)); while (x != input) { if (x != input) { guess++; } } char answer; if (x == input) { printf(" correct"); printf("Number of Guesses: %d\n", guess); exit(0); } //to reset random x = (rand() % 10 + 1); close(fd[0]); } }``` Now that it's in a readable form, it will be more likely for somebody to read it. Nobody would have wanted to bother with the original mess. Also, that is not C. It will not compile as C. If you are using an odd variant like C99, then inform us of that fact. It will save this forum the bandwidth of us having to come back to you to inform you that your program won't even compile. And you forgot main's return statement.

So sorry its my first time here :
i use gcc -std=c99 g.c
and sorry for the indent i was having problems with the tagging and it reformated my code

#4
April 10th, 2013, 04:12 PM
 jimblumberg
Contributing User

Join Date: Jan 2013
Posts: 152
Time spent in forums: 1 Week 7 h 24 m 27 sec
Reputation Power: 18
Quote:
 If you are using an odd variant like C99, then inform us of that fact.

Why do you consider C11 or C99 odd variants. In my opinion these are the current standards and your C90 variant should really be considered the odd variant if any.

Jim

#5
April 11th, 2013, 01:59 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,258
Time spent in forums: 2 Months 4 Weeks 1 Day 14 h 22 m 54 sec
Reputation Power: 1827
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

#6
April 11th, 2013, 02:02 AM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,858
Time spent in forums: 3 Months 1 Day 5 h 50 m 16 sec
Reputation Power: 2199
I call C99 the variant because it is. When C is mentioned, it is C90, not C99. To mention C99 requires that we say "C99". When you pick up a C book, how often is it instead C99? Not very often. If the book is about C99, it will say "C99".

As far as I can tell, C90 is universally supported in current compilers, but C99 is not. Some current compilers support C99 fully, some only partially, and some not at all nor do they have any plans to support it. Microsoft is the best known example of not supporting C99, because C++ and the STL already do what C99 does so why bother?

Even though gcc now supports C99 partially, when you run gcc what does it default to? C90, not C99. We both saw that the OP had to tell gcc to not do what it normally would and instead to compile according to the variant standard, C99. And for anyone else to try to compile his code successfully, they would have to either correct it to comply with C90 or else get a compiler that supports C99.

And just who uses C99? How widespread is its use and support in industry? I've never encountered any programmer in industry, AKA "the real world", who uses C99, nor any company that specifies using C99. Doesn't mean that they don't exist, just that I've never heard of any. So far, the only people I've seen using C99 have been students and hobbyists.

I'm not saying that nobody should use C99. All I'm saying is that when you do use C99 and want to ask others to compile and test your code, you damned well better tell us up front that you are using something different from what everybody else knows as "C".

C is C90. C99 is a variant that needs to be identified as C99.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > C Simple child and parent back and forth