Thread: Newbie in problem with looping

  1. #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

  2. #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

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.

  4. #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;
                    }
    		 
    
    
    }

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The statement
    Code:
    line = getchar();
    inside the for-loop overwrites the value of line in the for-loop. Use different variables.
    Kurt

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.

  7. #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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure Array Looping Problem
    By Vitamin_C in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2005, 03:22 PM
  2. Problem with looping.
    By rina in forum C Programming
    Replies: 9
    Last Post: 10-05-2005, 01:21 AM
  3. newbie Windows problem...
    By gcn_zelda in forum Windows Programming
    Replies: 9
    Last Post: 06-07-2003, 02:14 PM
  4. Looping Problem
    By simly01 in forum Windows Programming
    Replies: 1
    Last Post: 06-28-2002, 01:05 AM
  5. newbie coding problem
    By rippascal in forum C++ Programming
    Replies: 10
    Last Post: 01-08-2002, 11:45 PM