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]
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]
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?
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
'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 );
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...
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 )
i get an error with the inputCode:#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; } }
on another note:
explain std::cout for me plz.
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.
thanks for the answer about std ya i know about the switchcase thing. still wont compile
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!!Originally Posted by Brain Cell