Go to Post The possibilities for zip ties are endless. - Elgin Clock [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 17-02-2006, 11:21
xrabohrok's Avatar
xrabohrok xrabohrok is offline
hunter of errors
FRC #1208 (The Metool Brigade)
Team Role: Programmer
 
Join Date: Jan 2006
Location: O'Fallon
Posts: 62
xrabohrok is an unknown quantity at this point
Red face 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.

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.
Errors of any type are welcome!
__________________
"It's programming's fault" may be a viable excuse for just about everything, except the robot falling apart.

It will 'cause it can!

constants aren't. variables won't.
  #2   Spotlight this post!  
Unread 17-02-2006, 11:31
xrabohrok's Avatar
xrabohrok xrabohrok is offline
hunter of errors
FRC #1208 (The Metool Brigade)
Team Role: Programmer
 
Join Date: Jan 2006
Location: O'Fallon
Posts: 62
xrabohrok is an unknown quantity at this point
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!
__________________
"It's programming's fault" may be a viable excuse for just about everything, except the robot falling apart.

It will 'cause it can!

constants aren't. variables won't.
  #3   Spotlight this post!  
Unread 17-02-2006, 12:14
kevlarman kevlarman is offline
"why cant i install Linux?" guy
AKA: Roman Tetelman
None #0100
Team Role: Programmer
 
Join Date: Feb 2005
Rookie Year: 2005
Location: Belmont, CA
Posts: 11
kevlarman is an unknown quantity at this point
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?!
  #4   Spotlight this post!  
Unread 17-02-2006, 14:07
Matt Krass's Avatar
Matt Krass Matt Krass is offline
"Old" and Cranky. Get off my lawn!
AKA: Dark Ages
FRC #0263 (Sachem Aftershock)
Team Role: Mentor
 
Join Date: Oct 2002
Rookie Year: 2002
Location: Long Island, NY
Posts: 1,187
Matt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond repute
Send a message via AIM to Matt Krass
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
__________________
Matt Krass
If I suggest something to try and fix a problem, and you don't understand what I mean, please PM me!

I'm a FIRST relic of sorts, I remember when we used PBASIC and we got CH Flightsticks in the KoP. In my day we didn't have motorized carts, we pushed our robots uphill, both ways! (Houston 2003!)
  #5   Spotlight this post!  
Unread 18-02-2006, 11:11
kevlarman kevlarman is offline
"why cant i install Linux?" guy
AKA: Roman Tetelman
None #0100
Team Role: Programmer
 
Join Date: Feb 2005
Rookie Year: 2005
Location: Belmont, CA
Posts: 11
kevlarman is an unknown quantity at this point
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

Last edited by kevlarman : 18-02-2006 at 11:43.
  #6   Spotlight this post!  
Unread 18-02-2006, 15:57
TuaMater TuaMater is offline
Registered User
FRC #1895
 
Join Date: Jan 2006
Location: Manassas
Posts: 10
TuaMater is on a distinguished road
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;
}
  #7   Spotlight this post!  
Unread 18-02-2006, 16:37
koenig3456 koenig3456 is offline
Programming Coach
None #0681 (EARTH Squad)
Team Role: Coach
 
Join Date: Jan 2006
Location: Pennsylvania
Posts: 16
koenig3456 is an unknown quantity at this point
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.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C18 v2.4 -- Syntax error where there is no syntax error Joel J Programming 7 12-01-2007 17:27
Union Errors in AutoCAD indieFan Inventor 4 16-07-2004 12:54
PBASIC language syntax WizardOfAz Programming 14 30-04-2003 10:23
Digital inputs, bandwith, errors? Micah Brodsky Programming 7 20-01-2003 16:08
Specs full of errors??? Simon G Motors 1 20-01-2003 12:53


All times are GMT -5. The time now is 16:54.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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