Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   The silly syntax errors that won't go away! (http://www.chiefdelphi.com/forums/showthread.php?t=44181)

xrabohrok 02-17-2006 11:21 AM

The silly syntax errors that won't go away!
 
We've all been there. You spend hours upon hours working and working on your precious , but when you tell the compiler to finally compile your work, it spits out a error log the size of a Tom Clancy Novel! So you work and work, until the program comes down to one fatal error that you just, cant Find. :ahh:

So that's what this forum is for. Because by the time you actually finish the code, you have lost your ability to spot your own mistakes. Let us help you! :)

I'd like the forum to also be open to beginners C programming exploits (aka: not totally robotics related). You learn by doing. :o
Errors of any type are welcome!

xrabohrok 02-17-2006 11:31 AM

Re: The silly syntax errors that won't go away!
 
Alrighty. I am pretty new to C, though not that new, and I have decided to attempt to try making my own libraries and attached .c files to see how they work. Through much thinking, I have decided to make a program that would simulate a blackjack deck. What I have here isn't very flexible, and it just draws five cards. But when I compile, it says everything in the .h file has been declared twice! I don't know what to do! Here's the code:

main.c
Quote:

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

#include "deck.c"



int main(int argc, char *argv[])
{
int count=1;

draw(5);

while(count<=4)
{
if (hand[count]==0)
{break;}
printf("%s \n" ,handdis[count]);
count++;
}
system("PAUSE");
return 0;
}
deck.c

Quote:





//makes a random number between 1 and n
int randn(int n)
{
num=rand()%n + 1;
return num;
}

//the function that will give the value to the face cards
int facetovalue(int check)
{
//just use what you got in check for everything
//the ace checker
if (facev[check]==4)
{
printf("1 or 11?\n");
scanf("%i" ,&ace);
if (ace==1)
{
nface[check]--;
hand[handcount]=1;
handdis[handcount]= 1;
}
else if(ace==11)
{
nface[check]--;
hand[handcount]=11;
//handdis[handcount]= "11";
}
}

else
{
nface[check]--;
hand[handcount]=facev[check];
handdis[handcount]=face[check];
}

handcount++;
numface--;
return 0;
}

int draw(int drawn)
{

// draw this many cards
while (drawnn<=drawn)
{
//while codeing, I thought, what if there are no face cards left? Then we're screwed! so that's
//what's this is for
decision=1;
while (decision)
{
//what do we draw? face, or number card
randn(52);
if ((numdeckn>0)&&(num<36))
{

decision=0;
}
else if((numface>0)&&(num>37))
{

decision=0;
}
else
{
printf("Your out of cards!\n");
return 0;
}
}


if (num<=36)
{
//it's a number!
//caroline brought up something important, can one char store something like 10?
//We'll have to wait and find out.
randn(9);
hand[handcount]=deckn[num];
handdis[handcount]=deckn[num];
numdeckn--;
decknn[num]--;
handcount++;
return 0;

}

if (num>=37)
{

//it's a girl!
randn(4);
facetovalue(num);
return 0;
}
}
drawnn++;
}
deck.h

Quote:






//display face array
char face[5] = "0JKQA";

//number of face cards
int nface[5]= {0, 4, 4, 4, 4};

//default number of face cards
int dnface[5] = {0, 4, 4, 4, 4};

//values of face cards, where 4 is an ace
int facev[5] = { 0, 10, 10, 10, 4};

//is the ace one or 11?
int ace=1;

//number of cards in hand in array speak.
int handcount=0;

//what's in your hand, value wise
int hand[10]= {0,0,0,0,0,0,0,0,0,0};

//what's displayed
char handdis[10] ="0000000000";

//the temporary random number
int num=0;

//the values of the number
int deckn[10] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 10};

//the number of cards per type
int decknn[10] = { 0, 4, 4, 4, 4, 4, 4, 4, 4};

//the number of cards per type default
int decknnd[10] = { 0, 4, 4, 4, 4, 4, 4, 4, 4};

//the number of number cards left
int numdeckn=36;

//the number of face cards left
int numface=16;

//the deciding factor for figuring out if we are drawing from a viable pile
int decision=1;

//how many cards do you draw?
int cardn=0;

//how many cards we have drawn so far.
int drawnn=0;


the board didn't keep any of the formatting. Sorry!

kevlarman 02-17-2006 12:14 PM

Re: The silly syntax errors that won't go away!
 
Quote:

Originally Posted by xrabohrok
Alrighty. I am pretty new to C, though not that new, and I have decided to attempt to try making my own libraries and attached .c files to see how they work. Through much thinking, I have decided to make a program that would simulate a blackjack deck. What I have here isn't very flexible, and it just draws five cards. But when I compile, it says everything in the .h file has been declared twice! I don't know what to do! Here's the code:

main.c


deck.c



deck.h



the board didn't keep any of the formatting. Sorry!

uuuuhm... why did you #include a .c file?! :confused:

Matt Krass 02-17-2006 02:07 PM

Re: The silly syntax errors that won't go away!
 
Quote:

Originally Posted by xrabohrok
Alrighty. I am pretty new to C, though not that new, and I have decided to attempt to try making my own libraries and attached .c files to see how they work. Through much thinking, I have decided to make a program that would simulate a blackjack deck. What I have here isn't very flexible, and it just draws five cards. But when I compile, it says everything in the .h file has been declared twice! I don't know what to do! Here's the code:

main.c


deck.c



deck.h



the board didn't keep any of the formatting. Sorry!

This is a common problem. Your variables are being declared in every file that includes them. So use it more than once....it defines them in multiple files.

In the file that needs to own them, in this case deck.c, flank the include statement with
#define VAR_DECLARE
the include
#undef VAR_DECLARE

Then in your header file, wrap all the code declaring the variables in
#ifdef VAR_DECLARE
code goes here
#else
extern code goes here
#endif

After the else copy your code defining the variables, but put extern in front of each, that means to the compiler "This is already declared elsewhere, use that version."

Example:
C file that owns header:
Code:

#define VAR_DECLARE
  #include "header.h"
#undef VAR_DECLARE

C file that uses header, but does not own it:
Code:

#include "header.h"
Header file:
Code:

#ifdef VAR_DECLARE
  char myChar1;
  int myInt1;
#else
  extern char myChar1;
  extern int myInt1;
#endif


kevlarman 02-18-2006 11:11 AM

Re: The silly syntax errors that won't go away!
 
actually the more traditional way to do that is
Code:

#ifndef _MY_HEADER_FILE_H_
#define _MY_HEADER_FILE_H_
//other stuff in the header goes here
#endif

and make sure that all variables are declared extern (and initialized in the .c file that owns them), otherwise each file will get its own copy of the variable.
EDIT:
forgot to mention that _MY_HEADER_FILE_H_ should be the name of the file it is in (in this case it would be myheaderfile.h), if two files have the same #define at the top, one will get ignored

TuaMater 02-18-2006 03:57 PM

Re: The silly syntax errors that won't go away!
 
Why won't the printf's output anything! It prints temp1: temp2: ground Vector:

aka what's the syntax for outputting doubles @($&^

double calcDistance(void)
{
double temp1 = 0.0;
double temp2 = 0.0;
double groundVector = 0.0;

printf("temp1: %4.0f ", temp1);

if (getTargetFound() == 1){

temp1 = (targetHeight - cameraHeight);

printf("temp1: %4.0f ", temp1);

temp2 = (getTiltAngle() * 3.14 / 180.0);

printf("temp2: %4.5f ", temp2);

groundVector = (temp1 / tan(temp2)) * ((temp1) / tan(temp2));

printf("Ground Vector: %f ", groundVector);

return groundVector;
}
else return 0.0;
}

koenig3456 02-18-2006 04:37 PM

Re: The silly syntax errors that won't go away!
 
Quote:

Originally Posted by TuaMater
Why won't the printf's output anything! It prints temp1: temp2: ground Vector:

aka what's the syntax for outputting doubles @($&^

double calcDistance(void)
{
double temp1 = 0.0;
double temp2 = 0.0;
double groundVector = 0.0;

printf("temp1: %4.0f ", temp1);

if (getTargetFound() == 1){

temp1 = (targetHeight - cameraHeight);

printf("temp1: %4.0f ", temp1);

temp2 = (getTiltAngle() * 3.14 / 180.0);

printf("temp2: %4.5f ", temp2);

groundVector = (temp1 / tan(temp2)) * ((temp1) / tan(temp2));

printf("Ground Vector: %f ", groundVector);

return groundVector;
}
else return 0.0;
}

printf on the RC doesn't support floating point numbers.


All times are GMT -5. The time now is 11:46 AM.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi