Thread: can (switch) be apart of a loop?

  1. #1
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279

    Post can (switch) be apart of a loop?

    i using a switch statment inside of a loop
    for some reason first switch case executes but when the loop starts over seams like the switch statments isn't even touched
    here's an example:


    const int MaxMonths = 12;

    for(int k = 0; k < MaxMonths; ++k) // Iterates 12 times

    {
    switch (k)
    {
    case 0: {cout << "Janurary "; break;}
    case 1: {cout << "Febrary "; break;}
    case 2: {cout << "March "; break;}
    case 3: {cout << "April "; break;}
    case 4: {cout << "May "; break;}
    case 5: {cout << "June "; break;}
    case 6: {cout << "July "; break;}
    case 7: {cout << "August "; break;}
    case 8: {cout << "September "; break;}
    case 9: {cout << "Octoter "; break;}
    case 10: {cout << "November "; break;}
    case 11: {cout << "December "; break;}
    }

    }



    first line prints January and then no other months are printed..

    hmmmm...
    what do you guys think???

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    The buffers may not be getting flushed.

    cout << "This is a string with no cr/lf" << flush; //flushes with no cr/lf
    cout << "This is a string with a cr/lf" << endl; //flushes with cr/lf

    I just put endl in your code below and all is well.

    Code:
    #include "stdafx.h"
    #include <iostream> 
    using namespace std; 
    
    
    int main () 
    { 
    	
    	const int MaxMonths = 12; 
    
    	for(int k = 0; k < MaxMonths; ++k) // Iterates 12 times 
    
    	{ 
    		switch (k) 
    		{ 
    			case 0: {cout << "Janurary " << endl; break;} 
    			case 1: {cout << "Febrary " << endl; break;} 
    			case 2: {cout << "March " << endl; break;} 
    			case 3: {cout << "April " << endl; break;} 
    			case 4: {cout << "May " << endl; break;} 
    			case 5: {cout << "June " << endl; break;} 
    			case 6: {cout << "July " << endl; break;} 
    			case 7: {cout << "August " << endl; break;} 
    			case 8: {cout << "September " << endl; break;} 
    			case 9: {cout << "Octoter " << endl; break;} 
    			case 10: {cout << "November " << endl; break;} 
    			case 11: {cout << "December " << endl; break;} 
    		} 
    
    	} 
    
    	return 0; 
    }

  3. #3
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279

    thanx

    thanx, actually after i posted this question i found the problem

    Regards,
    matheo917

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to loop in switch??
    By pczafer in forum C++ Programming
    Replies: 6
    Last Post: 05-04-2009, 01:53 AM
  2. break statement not within loop or switch
    By Arruba in forum C Programming
    Replies: 3
    Last Post: 11-04-2006, 01:36 AM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM