I made a number guessing game in C that compiles fine with GCC 4.7.1. But it gives a lot of error with VC2010. Mostly syntax error issues. I don't know why it gives these errors as I've compiled many other C sources with VC2010 without these errors.
I compile it with VC (Command Line) with the command: cl guessgame.c
VC gives the following errors:
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for
Copyright (C) Microsoft Corporation. All rights reserved.
guessgame.c
guessgame.c(8) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(9) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(10) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(11) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(12) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(13) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(14) : error C2143: syntax error : missing ';' before 'type'
guessgame.c(20) : error C2065: 'chances' : undeclared identifier
guessgame.c(21) : error C2065: 'randRangeX' : undeclared identifier
guessgame.c(21) : error C2065: 'randRangeY' : undeclared identifier
guessgame.c(23) : error C2065: 'i' : undeclared identifier
guessgame.c(23) : error C2065: 'chances' : undeclared identifier
guessgame.c(23) : error C2065: 'i' : undeclared identifier
guessgame.c(23) : error C2065: 'i' : undeclared identifier
guessgame.c(25) : error C2065: 'chances' : undeclared identifier
guessgame.c(25) : error C2065: 'i' : undeclared identifier
guessgame.c(26) : error C2065: 'userAns' : undeclared identifier
guessgame.c(28) : error C2065: 'userAns' : undeclared identifier
guessgame.c(28) : error C2065: 'randomNumber' : undeclared identifier
guessgame.c(30) : error C2065: 'chances' : undeclared identifier
guessgame.c(30) : error C2065: 'i' : undeclared identifier
guessgame.c(36) : error C2065: 'wrong_guesses' : undeclared identifier
guessgame.c(39) : error C2065: 'wrong_guesses' : undeclared identifier
guessgame.c(41) : error C2065: 'randomNumber' : undeclared identifier
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));
int randomNumber = (rand() % 245)+5;
int userAns;
int randRangeX = randomNumber-((rand() % 3)+5);
int randRangeY = randomNumber+((rand() % 3)+5);
int chances = 3;
int wrong_guesses = 0;
int i;
printf("Number guessing game v1.1.\n");
printf("Copyright(c) 2013 - Ahnaf Tahmid.\n");
printf("-----------------------------------\n\n");
printf("Rules: 1) Guess the number.\n");
printf(" 2) You have %d chances.\n\n", chances);
printf("The number is between %d and %d.", randRangeX, randRangeY);
for(i = chances; i >= 1; i--)
{
printf("\nGuess %d: ", (chances-i)+1);
scanf("%d", &userAns);
while(getchar() != '\n');
if(userAns == randomNumber)
{
printf("\nGood guess! You guessed it in %d turn(s).\n", (chances-i)+1);
break;
}
else
{
printf("Aww, bad guess.\n");
wrong_guesses += 1;
}
}
if(wrong_guesses == 3)
{
printf("\nGame Over!\n\nThe answer was %d.\n", randomNumber);
}
getch();
return 0;
}
VC2010 supports C89 which requires that variables be declared at the start of their scope block. You can build with both compilers if you move the srand call after your variable declarations and delay initialising the variables which depend on rand().
int main()
{
int randomNumber;
int userAns;
int randRangeX;
int randRangeY;
int chances = 3;
int wrong_guesses = 0;
int i;
srand(time(NULL));
randomNumber = (rand() % 245)+5;
randRangeX = randomNumber-((rand() % 3)+5);
randRangeY = randomNumber+((rand() % 3)+5);
In C89, all variables have to be declared in the beginning of a block. This has been removed in C99.
But VC2010 doesn't support C99. So you have to put
srand(time(NULL));
after all the declaration of variables.
Reference:
C89 §3.6.2 Compound statement, or block
Syntax
compound-statement:
{ declaration-list opt statement-list opt }
declaration-list:
declaration
declaration-list declaration
statement-list:
statement
statement-list statement
Note that in C89, declaration-list has to be before statement -list in a block.
C99 §6.8.2 Compound statement
Syntax
compound-statement:
{ block-item-list_opt }
block-item-list:
block-item
block-item-list block-item
block-item:
declaration
statement
In C99, declaration and statement can be mixed.
An alternative approach to solve the issue pointed out by the other answers would be to open a new scope:
int main()
{
srand(time(NULL));
{
int randomNumber = (rand() % 245)+5;
int userAns;
int randRangeX = randomNumber-((rand() % 3)+5);
int randRangeY = randomNumber+((rand() % 3)+5);
int chances = 3;
int wrong_guesses = 0;
int i;
...
Related
I am making a program to simulate a game show, randomly. The program picks 1 of three doors for the prize to be hidden behind randomly. The computer picks a door to see if it won or not. This is looped over 10000 times to see how many times I win if I change my pick versus not changing it.
I am getting a bunch of syntax
#include <path-spec>
#include <stdio.h>
#include <time.h>
void count(Status result, Door* pLast_pick, Door* pPick, int* pWin_unchanged, int* pWin_changed);
void randomize_door(Door* pJackpot);
void pick_door(Door* pPick);
Status check(Door* pPick, Door* pJackpot);
enum status {WIN=1,LOSE};
enum door { FIRST=1, SECOND, THIRD };
typedef enum door Door;
typedef enum status Status;
int main(int argc, char* argv[]){
int i = 0;
srand(time);
Status result;
Status* pResult = &result ;
Door jackpot, pick, last_pick=NULL;
Door* pJackpot = &jackpot, * pPick=&pick, *pLast_pick;
int win_unchanged = 0, win_changed=0;
int* pWin_unchanged = &win_unchanged, *pWin_changed=&win_changed;
while (i < 10000){
last_pick = NULL;
randomize_door(pJackpot);
pick_door(pPick);
result = check(pPick, pJackpot);
count(result, pLast_pick, pPick, pWin_unchanged, pWin_changed);
i++;
}
printf("Wins when changed choice: %d , wins when choice is unchanged: %d", win_changed, win_unchanged);
return 0;
}
void randomize_door(Door* pJackpot){
*pJackpot = rand() % 3 + 1;
}
void pick_door(Door* pPick){
*pPick = rand() % 3 + 1;
}
Status check(Door* pPick, Door* pJackpot){
if (*pPick == *pJackpot){
return WIN;
}
else{
return LOSE;
}
}
void count(Status result, Door* pLast_pick, Door* pPick, int* pWin_unchanged, int* pWin_changed) {
if (*pLast_pick == *pPick){
if (result == WIN){
*pWin_unchanged++;
}
}
else{
if (result == WIN){
*pWin_changed++;
}
}
*pLast_pick = *pPick;
}
Below are the errors and which lines its happening at. most of them are forgot } or ; which doesn't make any sense in a function header.
1>------ Build started: Project: Program4.1, Configuration: Debug Win32 ------
1> daily4.c
daily4.c(4): error C2146: syntax error : missing ')' before identifier 'result'
daily4.c(4): error C2061: syntax error : identifier 'result'
daily4.c(4): error C2059: syntax error : ';'
daily4.c(4): error C2059: syntax error : ','
daily4.c(4): error C2059: syntax error : ')'
daily4.c(6): error C2143: syntax error : missing ')' before '*'
daily4.c(6): error C2143: syntax error : missing '{' before '*'
daily4.c(6): error C2059: syntax error : ')'
daily4.c(8): error C2143: syntax error : missing ')' before '*'
daily4.c(8): error C2143: syntax error : missing '{' before '*'
daily4.c(8): error C2059: syntax error : ')'
daily4.c(10): error C2061: syntax error : identifier 'check'
daily4.c(10): error C2059: syntax error : ';'
daily4.c(10): error C2143: syntax error : missing ')' before '*'
daily4.c(10): error C2143: syntax error : missing '{' before '*'
daily4.c(10): error C2143: syntax error : missing ';' before '*'
daily4.c(10): error C2059: syntax error : ')'
daily4.c(16): error C2370: 'Door' : redefinition; different storage class
daily4.c(10) : see declaration of 'Door'
daily4.c(21): warning C4013: 'srand' undefined; assuming extern returning int
daily4.c(24): error C2146: syntax error : missing ';' before identifier 'jackpot'
daily4.c(24): error C2065: 'jackpot' : undeclared identifier
daily4.c(24): error C2065: 'pick' : undeclared identifier
daily4.c(24): error C2065: 'last_pick' : undeclared identifier
daily4.c(24): warning C4047: '=' : 'int' differs in levels of indirection from 'void *'
daily4.c(25): error C2297: '*' : illegal, right operand has type 'int *'
daily4.c(25): error C2065: 'jackpot' : undeclared identifier
daily4.c(25): error C2065: 'pick' : undeclared identifier
daily4.c(25): warning C4047: '=' : 'int' differs in levels of indirection from 'int *'
daily4.c(25): error C2065: 'pLast_pick' : undeclared identifier
daily4.c(25): error C2100: illegal indirection
daily4.c(30): error C2065: 'last_pick' : undeclared identifier
daily4.c(30): warning C4047: '=' : 'int' differs in levels of indirection from 'void *'
daily4.c(31): warning C4013: 'randomize_door' undefined; assuming extern returning int
daily4.c(32): warning C4013: 'pick_door' undefined; assuming extern returning int
daily4.c(33): warning C4013: 'check' undefined; assuming extern returning int
daily4.c(34): warning C4013: 'count' undefined; assuming extern returning int
daily4.c(34): error C2065: 'pLast_pick' : undeclared identifier
daily4.c(43): error C2143: syntax error : missing ')' before '*'
daily4.c(43): error C2143: syntax error : missing '{' before '*'
daily4.c(43): error C2059: syntax error : ')'
daily4.c(43): error C2054: expected '(' to follow 'pJackpot'
daily4.c(48): error C2143: syntax error : missing ')' before '*'
daily4.c(48): error C2143: syntax error : missing '{' before '*'
daily4.c(48): error C2059: syntax error : ')'
daily4.c(48): error C2054: expected '(' to follow 'pPick'
daily4.c(53): error C2143: syntax error : missing ')' before '*'
daily4.c(53): error C2143: syntax error : missing '{' before '*'
daily4.c(53): error C2143: syntax error : missing ';' before '*'
daily4.c(53): error C2059: syntax error : ')'
daily4.c(53): error C2054: expected '(' to follow 'pJackpot'
daily4.c(62): error C2143: syntax error : missing ')' before '*'
daily4.c(62): error C2081: 'Door' : name in formal parameter list illegal
daily4.c(62): error C2143: syntax error : missing '{' before '*'
daily4.c(62): error C2143: syntax error : missing ';' before '*'
daily4.c(62): error C2059: syntax error : 'type'
daily4.c(62): error C2059: syntax error : ')'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
sample to fix
#include <stdio.h>
#include <time.h>
#include <stdlib.h> //for srand
//enum and typedef defined prior to use
enum status {WIN=1,LOSE};
enum door { NONE, FIRST=1, SECOND, THIRD };//Define NONE instead of NULL
typedef enum door Door;
typedef enum status Status;
void count(Status result, Door* pLast_pick, Door* pPick, int* pWin_unchanged, int* pWin_changed);
void randomize_door(Door* pJackpot);
void pick_door(Door* pPick);
Status check(Door* pPick, Door* pJackpot);
int main(int argc, char* argv[]){
int i = 0;
Status result;
Status* pResult = &result ;
Door jackpot, pick, last_pick=NONE;
Door* pJackpot = &jackpot, * pPick=&pick, *pLast_pick;
int win_unchanged = 0, win_changed=0;
int* pWin_unchanged = &win_unchanged, *pWin_changed=&win_changed;
srand(time(NULL));//If you use the equivalent of the C89 compiler can not be mixed the declaration statement and execution statements.
...
nb: This question has been reduced many times, due to comments.
Below now is presented minimum amount of code which generated the error. inttypes.h file was downloaded from here: ffMPEG "inttypes.h not found" error), which was thought to be the issue in the beginning.
//tlvlist.c
static int32_t test(somestruct *a);
/* Private method, adds tlv object to the list which contains raw binary data. */
int32_t int32_t test(somestruct *a)
{
/* Some checks */
if(a == NULL || bytes == NULL)
return -1;
/* Check if list is full */
if(a->used == MAX_LIST_SIZE)
return -1;
/* Index to first free element in the list */
int iIndex = a->used;
// ...
return 0;
}
errors:
tlvlist.c
c:\users\documents\visual studio 2012\projects\tlv list\tlv list\tlvlist.c(21): error C2143: syntax error : missing ';' before 'type'
c:\users\documents\visual studio 2012\projects\tlv list\tlv list\tlvlist.c(23): error C2065: 'iIndex' : undeclared identifier
c:\users\documents\visual studio 2012\projects\tlv list\tlv list\tlvlist.c(24): error C2065: 'iIndex' : undeclared identifier
c:\users\documents\visual studio 2012\projects\tlv list\tlv list\tlvlist.c(28): error C2065: 'iIndex' : undeclared identifier
c:\users\documents\visual studio 2012\projects\tlv list\tlv list\tlvlist.c(29): error C2065: 'iIndex' : undeclared identifier
c:\users\documents\visual studio 2012\projects\tlv list\tlv list\tlvlist.c(32): error C2065: 'iIndex' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Evidently, when compiling a C file in MSVS you need to have all variable declarations at the start of the function before any statement. For example:
int32_t Tlvlist_AddRawt(Tlvlist *a, uint8_t type, uint16_t size, const void *bytes)
{
/* Index to first free element in the list */
int iIndex;
/* Some checks */
if(a == NULL || bytes == NULL)
return -1;
iIndex = a->used;
...
}
I believe this is old C89 format and most C compilers now use C99 (or greater) which would permit variable declarations anywhere in the function. Renaming the file as CPP is another option for MSVS without moving the variable declarations to the top of a function, though it may raise other issues in the code.
http://blogs.msdn.com/b/vcblog/archive/2013/07/19/c99-library-support-in-visual-studio-2013.aspx
C99 support added visual studio 2013, but I cant use complex.h in my "C" code.
#include <stdio.h>
#include <complex.h>
int main(void)
{
double complex dc1 = 3 + 2 * I;
double complex dc2 = 4 + 5 * I;
double complex result;
result = dc1 + dc2;
printf(" ??? \n", result);
return 0;
}
I get syntax errors.
Edit: Sorry for the missing part.
error C2146: syntax error : missing ';' before identifier 'dc1'
error C2065: 'dc1' : undeclared identifier
error C2088: '*' : illegal for struct
error C2086: 'double complex' : redefinition
error C2146: syntax error : missing ';' before identifier 'dc2'
error C2065: 'dc2' : undeclared identifier
error C2088: '*' : illegal for struct
error C2086: 'double complex' : redefinition
error C2146: syntax error : missing ';' before identifier 'result'
error C2065: 'result' : undeclared identifier
error C2065: 'result' : undeclared identifier
error C2065: 'dc1' : undeclared identifier
error C2065: 'dc2' : undeclared identifier
error C2065: 'result' : undeclared identifier
IntelliSense: expected a ';'
IntelliSense: expected a ';'
IntelliSense: expected a ';'
IntelliSense: identifier "result" is undefined
IntelliSense: identifier "dc1" is undefined
IntelliSense: identifier "dc2" is undefined
In case anyone is searching a year later, try
_Dcomplex dc1 = {3.0, 2.0};
for the variable declaration.
From looking inside VS2013's "complex.h" header, it seems that Microsoft decided on their own implementation for C complex numbers. You'll have to implement your own arithmetical operators using the real() and imag() functions, i.e.:
double real_part = real(dc1) + real(dc2);
double imag_part = imag(dc1) + imag(dc2);
_Dcomplex result = {real_part, imag_part};
Another way is to define like:
/*_Fcomplex */ _C_float_complex a = _FCbuild(5.0F, 1.0F);
printf( "z = %.1f% + .1fi\n", crealf(a), cimagf(a));
/*_Dcomplex*/ _C_double_complex b = _Cbuild(3.0, 2.0);
printf("z = %.1f% + .1fi\n",creal(b), cimag(b));
I am writing a program in C that uses winsock and I am using the command fcntl to make the receive call nonblocking and I am getting the following errors.
warning C4013: 'fcntl' undefined; assuming extern returning int
error C2065: 'F_SETFL' : undeclared identifier
error C2065: 'F_GETFL' : undeclared identifier
error C2065: 'F_SETFL' : undeclared identifier
error C2065: 'O_NDELAY' : undeclared identifier
error C2065: 'EWOULDBLOCK' : undeclared identifierenter code here
I am including the winsock2.h header file in my code as follows
#pragma comment(lib,"ws2_32.lib")
#include <winsock2.h>
Please help me out.
Thanks in advance.
I think on Windows you need to use ioctlsocket rather than fcntl().
To make non-blocking:
unsigned long on = 1;
if (0 != ioctlsocket(socket_fd, FIONBIO, &on))
{
/* Handle failure. */
}
To make blocking:
unsigned long off = 0;
if (0 != ioctlsocket(socket_fd, FIONBIO, &off))
{
/* Handle failure. */
}
Instead of EWOULDBLOCK use WSAEWOULDBLOCK.
This code is working fine in c++.I need the solution for C.
This is my sample.
FileName:Mail.c
This is the declaration I made in the file mail.c
#ifdef __cplusplus
typedef int (__cdecl *SetIpAddressFun)(char* , int , int );
typedef bool (__cdecl *SendMailFun)(char * , char *);
#endif
I used this function pointers in the function called func() in the same file mail.c
void func()
{
SendMailFun sendMailFuncPtr;
SetIpAddressFun setIpAddressFuncPtr;
}
EDIT
If i remove the #ifdef lines in the file mail.c then it is showing the following error.
error C2143: syntax error : missing ')' before '__cdecl'
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(138) : error C2143: syntax error : missing '{' before '__cdecl'
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(138) : error C2059: syntax error : ')'
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(138) : error C2165: 'left-side modifier' : cannot modify pointers to data
ERROR
List of errors happened while compiling in c.
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1615) : error C2065: 'SendMailFun' : undeclared identifier
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1615) : error C2146: syntax error : missing ';' before identifier 'sendMailFuncPtr'
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1615) : error C2065: 'sendMailFuncPtr' : undeclared identifier
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1616) : error C2065: 'SetIpAddressFun' : undeclared identifier
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1616) : error C2146: syntax error : missing ';' before identifier 'setIpAddressFuncPtr'
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1616) : error C2065: 'setIpAddressFuncPtr' : undeclared identifier
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1625) : error C2065: 'setIpAddressFuncPtr' : undeclared identifier
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1625) : error C2065: 'SetIpAddressFun' : undeclared identifier
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1625) : error C2146: syntax error : missing ';' before identifier 'GetProcAddress'
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1626) : error C2065: 'setIpAddressFuncPtr' : undeclared identifier
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1626) : warning C4047: '!=' : 'void *' differs in levels of indirection from 'int'
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1628) : warning C4013: 'setIpAddressFuncPtr' undefined; assuming extern returning int
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1630) : error C2065: 'sendMailFuncPtr' : undeclared identifier
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1630) : error C2065: 'SendMailFun' : undeclared identifier
e:\projects\avg\apps\ezcepanel i.7 - pcpanel from vss\ccode\alarms.c(1630) : error C2146: syntax error : missing ';' before identifier
This is because when you declared the function
#ifdef __cplusplus
typedef int (__cdecl *SetIpAddressFun)(char* , int , int );
typedef bool (__cdecl *SendMailFun)(char * , char *);
#endif
you do it conditionally only for __cplusplus which is typically not defined when compiling C files
EDIT
I think your second problem is to do with the presence of bool which to my knowledge is not C keyword. Considering replacing it with an int
The #ifdef __cplusplus means that bit of code will only be complied when using a C++ compiler.
It's the bit that defines the function pointer type SendMailFun, so the compiler errors are complaining that SendMailFun and SendMailFun don’t exist.
You could start by removing the lines
#ifdef __cplusplus
and
#endif