C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-04-2005, 08:00 AM   #1
Registered User
 
Join Date: Nov 2005
Posts: 2
Newbie in problem with looping

hii all,

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   Reply With Quote
Old 11-04-2005, 09:46 AM   #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   Reply With Quote
Old 11-04-2005, 03:49 PM   #3
Frequently Quite Prolix
 
dwks's Avatar
 
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   Reply With Quote
Old 11-05-2005, 05:25 AM   #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   Reply With Quote
Old 11-05-2005, 06:08 AM   #5
ZuK
Registered User
 
Join Date: Aug 2005
Posts: 1,330
The statement
Code:
line = getchar();
inside the for-loop overwrites the value of line in the for-loop. Use different variables.
Kurt
ZuK is offline   Reply With Quote
Old 11-05-2005, 10:05 AM   #6
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
And main() should return int!

Change this
Code:
void main() {
    /* ... */
}
to
Code:
int main() {
    /* ... */
    return 0;
}
Your default case doesn't do what you think it does. And you don't need continue at the end of the for loop. Nor <conio.h>.
__________________
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   Reply With Quote
Old 11-05-2005, 12:53 PM   #7
Registered User
 
cbastard's Avatar
 
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:02 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22