473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

help with loop!!!!!! plz

38
I am trying to write a Traffic-Light control program using C which will ask the user to chose an initial state,then the program program has to continue with the correct state for both lights, which follows the initial state indicated by the user.
eg: if I chose initial state=4 the program has to go trought 4,5,6,1,2,and then stop at 3.

this is the code i have written so far:
------------------------------------------------------------------------------------
#include<stdio.h>

void main(void)

{

int state;
int counter;
int state2;


printf("Please give a value to start\n");
scanf("%d",&state2);
fflush(stdin);


printf("LIGHTS 1 LIGHTS 2 \n");

while(state<=6)
{



for(state=state2;state<=6;state++)
{




switch(state)
{
case 1:

printf(" 1 RED GREEN \n");
break;

case 2:

printf(" 2 RED AMBER \n");
break;

case 3:

printf(" 3 RED+AMBER RED \n");
break;

case 4:
printf(" 4 GREEN RED \n" );
break;

case 5:
printf(" 5 AMBER RED \n");
break;

case 6:
printf(" 6 RED RED+ AMBER \n ");
break;

default:

printf("You have entered a wrong number\n");
break;




} getchar();


}
if(state>6)







{

state2= state-6;


}


}


}
Dec 5 '06 #1
1 1334
DeMan
1,806 1GB
You could use a while loop instead of a for loop, because you always want your state to loop (and always be <=6, which could make the for loop loop forever).

A simpler alternative (which should work) is to change your switch statement to
Expand|Select|Wrap|Line Numbers
  1. switch(state%6) //This gives the remainder when divided by 6, so 7 ==1 (mod 6) as req'd
  2. {
  3.   case 1:
  4.     printf(" 1 RED GREEN \n");
  5.   break;
  6.  
  7.   case 2:
  8.     printf(" 2 RED AMBER \n");
  9.   break;
  10.  
  11.   case 3: 
  12.     printf(" 3 RED+AMBER RED \n");
  13.   break;
  14.  
  15.   case 4:
  16.     printf(" 4 GREEN RED \n" );
  17.   break;
  18.  
  19.   case 5:
  20.     printf(" 5 AMBER RED \n");
  21.   break;
  22.  
  23.   case 0:  //This now has to be 0 since 6 % 6 ==0, you may need to move this up the top, 
  24.             //although I think case statements can be in any order
  25.     printf(" 6 RED RED+ AMBER \n ");
  26.   break;
  27.  
  28.   default:
  29.     printf("It should now be impossible to get here even with ridiculous input");
  30.   break;
  31. }
  32.  
Dec 5 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: addi | last post by:
All, I will be eternally greatful if someone can provide snippet of code, URL or reference material that shows how to display data in a "n colums * n rows" format. I am new to ASP and have...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
0
by: pargat.singh | last post by:
Hi : I am using Master/Child page in VS2005 and i wrote generic function to show/hide controls which works fine if i don't use master/child .Below is my function which work fine for single page...
1
by: al2004 | last post by:
Write a program that reads information about youth soccer teams from a file, calculates the average score for each team and prints the averages in a neatly formatted table along with the team name....
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
9
by: TF | last post by:
Hello all, I made a ASP.NET 2.0 site that shows possible "recipes" for paint colors stored in an access dbase. Basically, 1000 colors are stored with specific RGB values in separate columns. A...
6
by: jenipriya | last post by:
Hi all... its very urgent.. please........i m a beginner in oracle.... Anyone please help me wit dese codes i hv tried... and correct the errors... The table structures i hav Employee (EmpID,...
0
by: shrik | last post by:
I have following error : Total giant files in replay configuration file are : File name : /new_file/prob1.rec Given file /new_file/prob1.rec is successfully verified. Splitting for giant file...
2
by: giulyteh8 | last post by:
An integer number is said to be prefect number if its factors, including 1(but not the number itself), sum to the number. For example, 6 is a prefect number bcoz 6=1+2+3. Write a function called...
1
by: mshroom12 | last post by:
Hello to all. I am having difficulty trying to do this Java project using Eclipse. The following is what I have to do. Election Day It's almost election day and the election officials need a...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.