![]() |
| | #1 |
| Registered User Join Date: Nov 2005
Posts: 2
| Newbie in problem with looping i am a total newbie here and also a BRAND NEW learner of C as a part of my first assignment in class i have done the following coding. but having problem wth this. is there any one who wishes to help me?? Code:
#include <stdio.h>
void main()
{
char line;
clrscr();
printf("Choose your initial state:");
line=getchar();
switch(line) {
case '1':
printf("Step 1:\t L==Red\t\t\t\tGreen\n");
case '2':
printf("Step 2:\t L==Red\t\t\t\tAmber\n");
case '3':
printf("Step 3:\t L==Red+Amber\t\t\tRed\n");
case '4':
printf("Step 4:\t L==Green\t\t\tRed\n");
case '5':
printf("Step 5:\t L==Amber\t\t\tRed\n");
case '6':
printf("Step 6:\t L==Red\t\t\t\tRed+Amber\n");
}
getch();
}
My problems are: 1. I want to use looping , but unfortunately both while and for loops are failed. 2. I want to run the loops depending on user demand. Is there any 1 to help this newbie??? JUST TELL ME IF I NEED TO PRACTICE OR ADD ANY SPECIFIC FUNCTION IF Any 1 can suggest any good pdf to learn switch & loops with example, I will be grateful Last edited by nrain; 11-04-2005 at 08:19 AM. Reason: addition |
| nrain is offline | |
| | #2 |
| Fear the Reaper... Join Date: Aug 2005 Location: Toronto, Ontario, Canada
Posts: 625
| I don't particularly see why you would want loops in there. That is, unless you want to cycle through the steps. What specifically is it that you're trying to do ? And as for ressources on learning about loops and switch statements : For loops : http://www.cprogramming.com/tutorial/c/lesson3.html For switch statements : http://www.cprogramming.com/tutorial/c/lesson5.html
__________________ Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction |
| Happy_Reaper is offline | |
| | #3 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| And main() should return int.
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, nort, etc. |
| dwks is offline | |
| | #4 |
| Registered User Join Date: Nov 2005
Posts: 2
| still prob would any1 plz find an error that is causing LOOP-error Code:
#include <stdio.h>
#include <conio.h>
void main()
{
char line;
for(line=0;line<7;line++){
printf("Choose your initial state:");
line=getchar();
switch(line)
{
case '1':
printf("Step 1:\t L==Red\t\t\t\tGreen\n");
printf("Step 2:\t L==Red\t\t\t\tAmber\n");
printf("Step 3:\t L==Red+Amber\t\t\tRed\n");
printf("Step 4:\t L==Green\t\t\tRed\n");
printf("Step 5:\t L==Amber\t\t\tRed\n");
printf("Step 6:\t L==Red\t\t\t\tRed+Amber\n");
printf("Choose your initial state:");
scanf("%d",&line);
break;
case '2':
printf("Step 2:\t L==Red\t\t\t\tAmber\n");
printf("Step 3:\t L==Red+Amber\t\t\tRed\n");
printf("Step 4:\t L==Green\t\t\tRed\n");
printf("Step 5:\t L==Amber\t\t\tRed\n");
printf("Step 6:\t L==Red\t\t\t\tRed+Amber\n");
printf("Step 1:\t L==Red\t\t\t\tGreen\n");
printf("Choose your initial state:");
scanf("%d",&line);
break;
case '3':
printf("Step 3:\t L==Red+Amber\t\t\tRed\n");
printf("Step 4:\t L==Green\t\t\tRed\n");
printf("Step 5:\t L==Amber\t\t\tRed\n");
printf("Step 6:\t L==Red\t\t\t\tRed+Amber\n");
printf("Step 1:\t L==Red\t\t\t\tGreen\n");
printf("Step 2:\t L==Red\t\t\t\tAmber\n");
break;
case '4':
printf("Step 4:\t L==Green\t\t\tRed\n");
printf("Step 5:\t L==Amber\t\t\tRed\n");
printf("Step 6:\t L==Red\t\t\t\tRed+Amber\n");
printf("Step 1:\t L==Red\t\t\t\tGreen\n");
printf("Step 2:\t L==Red\t\t\t\tAmber\n");
printf("Step 3:\t L==Red+Amber\t\t\tRed\n");
break;
case '5':
printf("Step 5:\t L==Amber\t\t\tRed\n");
printf("Step 6:\t L==Red\t\t\t\tRed+Amber\n");
printf("Step 1:\t L==Red\t\t\t\tGreen\n");
printf("Step 2:\t L==Red\t\t\t\tAmber\n");
printf("Step 3:\t L==Red+Amber\t\t\tRed\n");
printf("Step 4:\t L==Green\t\t\tRed\n");
break;
case '6':
printf("Step 6:\t L==Red\t\t\t\tRed+Amber\n");
printf("Step 1:\t L==Red\t\t\t\tGreen\n");
printf("Step 2:\t L==Red\t\t\t\tAmber\n");
printf("Step 3:\t L==Red+Amber\t\t\tRed\n");
printf("Step 4:\t L==Green\t\t\tRed\n");
printf("Step 5:\t L==Amber\t\t\tRed\n");
default:
printf("Choose your initial state:");
line=getchar();
break;
}
continue;
}
}
|
| nrain is offline | |
| | #5 |
| Registered User Join Date: Aug 2005
Posts: 1,330
| The statement Code: line = getchar(); Kurt |
| ZuK is offline | |
| | #6 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| And main() should return int! ![]() Change this Code: void main() {
/* ... */
}
Code: int main() {
/* ... */
return 0;
}
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, nort, etc. |
| dwks is offline | |
| | #7 |
| Registered User Join Date: Jul 2005 Location: India
Posts: 167
| Your case '6' does'nt contain a break statement but that will not give you a loop error. Take another int variable for for loop.as already told.
__________________ Long time no C. I need to learn the language again. Help a man when he is in trouble and he will remember you when he is in trouble again. You learn in life when you lose. Complex problems have simple, easy to understand wrong answers. "A ship in the harbour is safe, but that's not what ships are built for" |
| cbastard is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Structure Array Looping Problem | Vitamin_C | C++ Programming | 2 | 12-17-2005 03:22 PM |
| Problem with looping. | rina | C Programming | 9 | 10-05-2005 01:21 AM |
| newbie Windows problem... | gcn_zelda | Windows Programming | 9 | 06-07-2003 02:14 PM |
| Looping Problem | simly01 | Windows Programming | 1 | 06-28-2002 01:05 AM |
| newbie coding problem | rippascal | C++ Programming | 10 | 01-08-2002 11:45 PM |