0

A book exercise prompts me to create a program that simulates coin tossing. My friend says he ran my code in a native GNU compiler, and it worked, but I'm receiving the following errors when I try to run it in Visual Studio 2010:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int result;

int flip();

int main(void)
{
    srand(time(NULL));

    int heads = 0;
    int tails = 0;
    unsigned counter;

    for(counter = 1; counter <= 100; counter++)
    {
        result = flip();

        if(result == 1)
        {
            printf("Heads\n");
            heads++;
        }

        else
        {
            printf("Tails\n");
            tails++;
        }
    }

        printf("Heads: %d\tTails: %d\n", heads, tails);
}

int flip()
{
    result = 1 + rand() % 2;

    if (result == 1)
        return 1;

    if (result == 2)
        return 0;

    return NULL;
}

syntax error: ')' (line 10)

'counter': undeclared identifier (15, 23)

'heads': undeclared identifier (19, 23)

't': undeclared identifier (10, 10)

syntax error: missing ')' before 'type' (line 10)

syntax error: missing ';' before '{' (line 11)

syntax error: missing ';' before 'type' (9, 10, 10, 10)

Thanks for any response.

Sergio
  • 28,539
  • 11
  • 85
  • 132
  • If you are compiling a .c file, maybe [this link][1] can help with your problem . [1]: http://stackoverflow.com/questions/2706453/what-is-the-cause-of-these-visual-studio-2010-errors-warnings – Neoh Jun 27 '13 at 05:49
  • pull that srand statement below the type declaration statements. C does not allow type declaration statements anywhere. – Ram Jun 27 '13 at 06:29
  • @ram what do you mean by C *does not allow type declaration anywhere*? By the way declaration is correct otherwise it would not have worked in gcc. – 0decimal0 Jun 27 '13 at 06:38
  • Is this the complete code? Because,I can't see the "t" referred in the error message anywhere in the program,try to use debugger. – 0decimal0 Jun 27 '13 at 06:41
  • try compiling the code with option -pedantic. This will give you a warning in GCC. "warning: ISO C90 forbids mixed declarations and code". VC does not allow this warning to pass. – Ram Jun 27 '13 at 06:44

3 Answers3

0

Here is a modified code which runs fine with me in eclipse :

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int result;

int flip();

int main(void)
{
    srand(time(NULL));

    int heads = 0;
    int tails = 0;
    unsigned counter;

    for(counter = 1; counter <= 100; counter++)
    {
        result = flip();

        if(result == 1)
        {
            printf("Heads\n");
            heads++;
        }

        else
        {
            printf("Tails\n");
            tails++;
        }
    }

        printf("Heads: %d\tTails: %d\n", heads, tails);

        return 0;
}

int flip()
{
    result = 1 + rand() % 2;

    if (result == 1)
        return 1;

    if (result == 2)
        return 0;
}

Mistakes: In main , you forgot 'return 0 ' statement. In flip function you wrote 'return NULL' which should not be there. Try this code in vs.

mvv1277
  • 246
  • 2
  • 4
  • you will get a warning when you compile the code with option -pedantic. GCC might over look this warning. But not VC. – Ram Jun 27 '13 at 06:42
  • In `flip()` there must be an `else` case after `if` instead of two `if`.Because function will only return if any one of the `if` condition is true,it won't return anything for any other case. – 0decimal0 Jun 27 '13 at 06:56
  • Check this link : http://stackoverflow.com/questions/11368561/c-program-wont-compile-in-visual-studio-2010?rq=1 – mvv1277 Jun 27 '13 at 09:55
0
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void){
int heads = 0,tails = 0;
unsigned int counter = 1;
int result = 0;

srand(time(NULL));

for(counter = 1; counter <= 100; counter++){
    result = rand()%2;

    if(result == 1){         
        heads++;
    }
    else{
        tails++;
    }
}
printf("Heads: %d\tTails: %d\n", heads, tails);     
return 0;
}
Ram
  • 437
  • 3
  • 13
  • The code contains some more logical errors too.You should write the whole code. – 0decimal0 Jun 27 '13 at 06:59
  • The fundamental problem i felt was srand() function being called above the type declaration statements. All type declaration statements have to be made at the beginning of a block. – Ram Jun 27 '13 at 07:24
0
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int result;

int flip();

int main(void){
    int heads = 0;
    int tails = 0;
    unsigned counter;

    srand(time(NULL));//Executable statement after the declaration

    for(counter = 1; counter <= 100; counter++){
        result = flip();

        if(result == 1){
            printf("Heads\n");
            heads++;
        } else {
            printf("Tails\n");
            tails++;
        }
    }

    printf("Heads: %d\tTails: %d\n", heads, tails);
    return 0;
}

int flip() {
    result = 1 + rand() % 2;

    return result == 1;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70