C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 14th, 2012, 11:57 AM
buceto buceto is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 3 buceto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 10 m 58 sec
Reputation Power: 0
Simple c code

Hey guys.. i‘m new into this stuff so help me out please!

i need a simple code to do this:
While we have condition 1, the program should do the following. if x is happening, do y. if x not happening, do w.


But this must be a loop, it has to do y everytime x is happening and it has to do w if x not happening. but must happen again if x start again.


understand?
thanks in advance and sry for my monkey english.

Reply With Quote
  #2  
Old September 14th, 2012, 12:46 PM
ptr2void ptr2void is offline
I haz teh codez!
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Dec 2003
Posts: 2,476 ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 4 h 17 m 41 sec
Reputation Power: 2194
We help people that actually put in an effort themselves. So far all you've done is tell us your assignment. You need to post *your* attempt, in code tags, as well as any errors you might be getting, and then we can tell you what you've done wrong.
__________________
I ♥ ManiacDan & requinix

This is a sig, and not necessarily a comment on the OP:
Please don't be a help vampire!

Reply With Quote
  #3  
Old September 14th, 2012, 01:31 PM
buceto buceto is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 3 buceto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 10 m 58 sec
Reputation Power: 0
Sorry. Ok here it goes.
I'm trying to give commands to a robot. Basically, it will start to walk. When it hits something (a button will be pressed), it has to walk backwards. When it starts walking backwards, a light sensor will be turned on. The robot has to walk backwards until it detects a black line, then, it shall stop moving.

I'm very noob at codes, so here's what I've done so far, didn't work properly.
(I can't make tests, I only have access to the software and robot by friday mornings)
I can get the robot to walk, and to start walking backwards when the button is pressed.

Code:
#include "API.h"
#include "BuiltIns.h"

void main(void)
{
int light; //this is the variable of the light sensor

SetPWM (1,0);   // Motor 1, full rotation
SetPWM (2,255); // Motor 2, full rotation (making it walk)

{
StartInterruptWatcher (1, FALLING_EDGE); 
while (GetInterruptWatcher(1) !=1) //in case button is pressed
Wait (10);

SetPWM(1,255);
SetPWM(2,0); //making it walk backwards

{
light=GetAnalogInput(1); //turning light sensor on
if (light>=100) //light>=100 = sees a black line on floor
{
SetPWM (1,127);
SetPWM (2,127); // motors off

}
}
}
}


It will start walking backwards but won't advance to the new condition. I can't go any further with my little knowledge.

Reply With Quote
  #4  
Old September 14th, 2012, 01:47 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,350 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 38 m 45 sec
Reputation Power: 383
Code:
#include<stdio.h>

#define FOREVER for(;;)
#define BIND(A,L,H) ((L)<(A)?(A)<(H)?(A):(H):(L))

void y(void) {
  puts("y action");
}

void w(void) {
  puts("w action");
}

void(*function_table[])(void) = {y,w};

int main(int ac,char*av[]) {
  extern volatile int x;
  int y;
  FOREVER {			/* While we have condition 1 */
    y = x;			/* the condition x */
    y = BIND(y,0,1);		/* x must be 0 or 1 */
    (*function_table[y])();	/* use y or w, depending */
  }
  return 0;			/* never happens */
}

Here's the program, simply dress the incomplete specifications and you're done.
Comments on this post
bdb disagrees: Just say no (FOREVER sucks): #define BEGIN {
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #5  
Old September 14th, 2012, 04:06 PM
ptr2void ptr2void is offline
I haz teh codez!
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Dec 2003
Posts: 2,476 ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 4 h 17 m 41 sec
Reputation Power: 2194

Reply With Quote
  #6  
Old September 14th, 2012, 04:19 PM
buceto buceto is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 3 buceto User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 10 m 58 sec
Reputation Power: 0
Quote:
Originally Posted by b49P23TIvg
Code:
#include<stdio.h>

#define FOREVER for(;;)
#define BIND(A,L,H) ((L)<(A)?(A)<(H)?(A):(H):(L))

void y(void) {
  puts("y action");
}

void w(void) {
  puts("w action");
}

void(*function_table[])(void) = {y,w};

int main(int ac,char*av[]) {
  extern volatile int x;
  int y;
  FOREVER {			/* While we have condition 1 */
    y = x;			/* the condition x */
    y = BIND(y,0,1);		/* x must be 0 or 1 */
    (*function_table[y])();	/* use y or w, depending */
  }
  return 0;			/* never happens */
}

Here's the program, simply dress the incomplete specifications and you're done.


Thanks!! Gonna try it

Reply With Quote
  #7  
Old September 14th, 2012, 05:06 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,350 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 38 m 45 sec
Reputation Power: 383
Anyway, the c idiom for "repeat forever" is

for(;

even if BDB disapproves of a specific macro.

while(1)

works, as would

do
while (1);

or

label:
goto label; /* if c has such a thing. */

Reply With Quote
  #8  
Old September 15th, 2012, 12:10 AM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
Quote:
Originally Posted by b49P23TIvg
label:
goto label; /* if c has such a thing. */
C does have the infamous goto statement, but the others in your post are definitely more idiomatic. while (1) (in C) and while (true) (in C++) are what I always use.

Reply With Quote
  #9  
Old September 15th, 2012, 07:55 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,806 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 19 m 38 sec
Reputation Power: 1800
Quote:
Originally Posted by Lux Perpetua
while (1) (in C) and while (true) (in C++) are what I always use.
GCC used to (and may still do so) generate a "condition is always true" warning for that but not
Code:
for(;;)
so for that reason alone I'd use the latter.

Reply With Quote
  #10  
Old September 15th, 2012, 10:58 AM
djdigitalgirl99 djdigitalgirl99 is offline
Permanently Banned
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 3 djdigitalgirl99 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 19 m 33 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
Program is to append the contents of string str1 to the end of string str.
My Code is as follows-
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <process.h>
void main()
{
int a;
char *str, *str1;
printf("Enter any string:");
gets(str);
fflush(stdin);
printf("Enter any string:");
gets(str1);
fflush(stdin);
for(a=strlen(str);a<strlen(str)+strlen…
{
str[a]=str1[a-strlen(str)];
fflush(stdin);
}
printf("After performing strcat(str,str1), str is now:");
puts(str);
getch();
}


[edited] Removed signature.[/edited]

Last edited by Scorpions4ever : September 15th, 2012 at 11:25 AM.

Reply With Quote
  #11  
Old September 15th, 2012, 11:28 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,382 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 20 h 31 m 48 sec
Reputation Power: 4080
^^^^
Only reason I'm leaving the above post (after I removed the signature) is in case people want to point out how many things are wrong with it. Beginners, this is how NOT to write code.

Oh dear. As I suspected, djdigitalgirl is an SEO wannabe and is cutting/pasting code from elsewhere, trying to look like an expert:
http://in.answers.yahoo.com/questio...07121736AA7aLHz
Comments on this post
Lux Perpetua agrees: No joke - almost every line is incorrect! On top of that: no code tags and attempting to hijack the
thread.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Last edited by Scorpions4ever : September 15th, 2012 at 11:31 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Simple c code

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap