Thread: Looping

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    12

    Question Looping

    i was wondering how i would loop a switchcase? the looping tutrorial uses veriables and does not help. in the last thread some one got into that but did not explain the parts and how they can be altered to work in diferent code.

    thx in advance B]

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Well if I am understanding you, this is probably what you are looking for
    Code:
    #include <iostream>
    
    int main()
    {
        bool done = false;
        int input;
        char exit;
        
        while(!done)
        {
         
          std::cout<<"Enter a number"<<std::endl;
          std::cin>>input;
          
          switch(input)
          {        
            case 1:
              std::cout<<"Number is 1"<<std::endl;
              break;
            //etc....
            default:
              std::cout<<"Not a valid number"<<std::endl;
          }  
              
          std::cout<<"Do it agian y or n"<<std::endl;
          std::cin>>exit;
          
          if(exit == 'n')
          {
            done = true;
          }
                
        }
        
    }
    Woop?

  3. #3
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    I'm not sure what you mean , but i think its something like this where typing 2 quits the loop :
    Code:
    int switchloop = 1;
    int choice = 0;
    
    while( switchloop )
    {
       cin >> choice;
       switch(choice)
       {
          case 1 :
             printf("you entered 1\n");
             break;
    
          case 2 :
             printf("Quitting loop\n");
             switchloop = 0;
             break;
       }
    }
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  4. #4
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    'nuther example for you...
    Code:
    	do {
    		int choice;
    		
    		cout << "Enter choice [0 to quit]\n?" >> endl;
    		cin >> choice;
    		
    		switch ( choice )
    			case 1:
    				// code
    				break;
    			case 2;
    				// code
    				break;
    			case 0;
    				// code
    				break;
    		} while ( choice );

  5. #5
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    You can also make a for loop that runs forever and in your switch-statement have a case that runs return 0, and closes the program.
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    12
    ya im pretty sure i did this wrong cus it does not work. but this is how i wrote it in. (i would post my code but i dont want to show how much of a n00b i am )

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        //many int's
       bool done = false
       while(!done)
       {
    
        switchcase:
          // ect...
    
          //the end of switchcase():
       std::cout<<"Do it again y or n"<<std::endl;
       std::cin>>exit;
    
       if(exit == 'n')
       {
         done = true;
       }
    }
    i get an error with the input

    on another note:
    explain std::cout for me plz.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    To answer your second question first, std is the name of a namespace, namely, namespace std. Kinda cool, huh. So what's a namespace? Well it's sort of a folder that can hold variables, functions, user defined types, files, etc. To tell the compiler or the linker where to look for anything declared in a namespace you need to use the scope operator associated with the name of the namespace followed by what it is you want to use from the namespace, like this;

    std::cout <<

    which means use the << operator associated with the object called cout declared in namespace std.

    There are lots of things declared in namespace std, like the iostream header file, the STD template classes, etc, etc.

    If you don't like prefixing anything you use from namespace std with the std:: identifier then you can use a using directive or a using declaration. But until you can read more about namespaces in your favorite textbook you can just add the line:

    using namespace std;

    after the list of files identified with the #include precompiler directives and before any other code is written, like you did in your posts. That way you can use anything found in namespace std in your program without prefixing it with std::. It isn't recommended you use this technique when your programs get more sophisticated, but it works fine when you are just starting out and the purpose of namespaces (to prevent name clashes) isn't that important to you yet. So continue to do so, don't worry about prefixing things in namespace std with std:: until you feel ready to do so, but beware that other people may use it when it is to their advantage.

    To possibly answer the first question in your last post I suspect the problem is this:

    switchcase:

    Note the other people who posted never used that syntax. They used:
    Code:
    switch(some_variable_name)
    {
       //some case(s) listed in here
    }
    You're only born perfect.

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    12
    thanks for the answer about std ya i know about the switchcase thing. still wont compile

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    12
    Quote Originally Posted by Brain Cell
    I'm not sure what you mean , but i think its something like this where typing 2 quits the loop :
    Code:
    int switchloop = 1;
    int choice = 0;
    
    while( switchloop )
    {
       cin >> choice;
       switch(choice)
       {
          case 1 :
             printf("you entered 1\n");
             break;
    
          case 2 :
             printf("Quitting loop\n");
             switchloop = 0;
             break;
       }
    }
    Thank you very much!!! read this one second but i whish it was the first i finished my program thanks to all you guys on the fourm that helped me including you!! im going to start work on a text based rpg soon this should be fun. thx again!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 01:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 01:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM