Ask your own question, for FREE!
Computer Science 13 Online
OpenStudy (anonymous):

How can i use exit code in a switch statement?

OpenStudy (anonymous):

this must be okay its given by STANFORD NSString * convertNum (int theNum) { NSString *numString; switch (theNum) { case 102: numString = @"Oh yea, string 102"; break; case 104: numString = @"Oh great, string 104"; break; /* ... */ default: numString = @"Don't feed me with something I don't know!"; break; } return numString; }

OpenStudy (anonymous):

Im new at c++.. can you explain it?

OpenStudy (anonymous):

k

OpenStudy (anonymous):

Linking error usually means something like a method, function or something similar has the prototype declare but it is not implemented anywhere. It can also mean you have not included a library or framework in you application but you are using the header files from that library or framework. Also your use of numString is fine, you are returning pointers to strings that are static, they generated at compile time.

OpenStudy (anonymous):

I have a sample program using switch stament.. but it didnt go well. and aslo want to apply exit in my prog.

OpenStudy (anonymous):

cout<<"MENU OF OPERATION"; cout<<endl; cout<<"1 - addition\n"; cout<<"2 - subtraction\n"; cout<<"3 - multiplication\n"; cout<<"4 - division\n"; cout<<"5 - Modulo Division\n";\ cout<<"6 - Average\n"; cout<<"7 - Quit\n"; cout<<endl; cout<<endl; cout<<"Enter your choice: "; cin>> choice; switch (choice) { case '1': { cout<<"***You Have Chosen Addition!***\n"; cout<<endl; cout<<"Please Enter the first value:"; cin>>a; cout<<"Please Enter the second value:"; cin>>b; cout<<"Please Enter the third value:"; cin>>c; cout<<endl; Add = a + b + c; cout<<"The sum of "<<a<<" and "<<b<< " and" <<c<< " is = "<<Add<<endl;break; cout<<endl; } case '2': { cout<<"***You Have Chosen Subtruction!***\n"; cout<<endl; cout<<"Please Enter the first value:"; cin>>a; cout<<"Please Enter the second value:"; cin>>b; cout<<"Please Enter the third value:"; cin>>c; cout<<endl; Sub = a - b - c; cout<<"The Difference of "<<a<<" and "<<b<<" and " <<c<< " is = "<<Sub<<endl;break; cout<<endl; } case '3': { cout<<"***You Have Chosen Multiplication!***\n"; cout<<endl; cout<<"Please Enter the first value:"; cin>>a; cout<<"Please Enter the second value:"; cin>>b; cout<<"Please Enter the third value:"; cin>>c; cout<<endl; Multi = a * b *c; cout<<"The product of "<<a<<" and "<<b<<" and " <<c<< " is = "<<Multi<<endl;break; cout<<endl; } case '4': { cout<<"***You Have Chosen Division!***\n"; cout<<endl; cout<<"Please Enter the first value:"; cin>>a; cout<<"Please Enter the second value:"; cin>>b; cout<<"Please Enter the third value:"; cin>>c; cout<<endl; if ( a == 0 || b == 0 || c == 0 ) cout<<"MATH ERROR! CANNOT DIVIDED BY ZERO\n"; else Divi = a / b / c; cout<<"The qoutient of "<<a<<" and "<<b<< " and " <<c<< " is = "<<Divi<<endl;break; cout<<endl; } case '5': { cout<<"***You Have Chosen Modulo Division!***\n"; cout<<endl; cout<<"Please Enter the first value:"; cin>>a; cout<<"Please Enter the second value:"; cin>>b; cout<<"Please Enter the third value:"; cin>>c; cout<<endl; if ( a == 0 || b == 0 || c == 0 ) cout<<"MATH ERROR! CANNOT DIVIDED BY ZERO\n"; else Mod = a % b % c; cout<<"The Remainder of " <<a<< " and " <<b<< " and " <<c<< " is = "<<Mod<<endl;break; cout<<endl; } case '6': { cout<<"***You Have Chosen Average!***\n"; cout<<endl; cout<<"Please Enter the first value:"; cin>>a; cout<<"Please Enter the second value:"; cin>>b; cout<<"Please Enter the third value:"; cin>>c; Ave = (a + b + c)/3; cout<<"The Average of " <<a<< " and " <<b<< " and " <<c<< " is = "<<Ave<<endl;break; cout<<endl; } case '7': { cout<<"***You Have Chosen Quit!***\n"; cout<<endl; cout<<"*****GOODBYE!!! PLEASE COME AGAIN!*****"<<endl;break; } default: cout<<"INVALID CHOICE!\n"; } system("pause"); return 0; }

OpenStudy (anonymous):

okay then apply it

OpenStudy (anonymous):

in the case 4 (division); when i divided it by 0, it break. how can i make a program that accept that problem

OpenStudy (anonymous):

ya then

OpenStudy (anonymous):

ny prob

OpenStudy (anonymous):

what?

OpenStudy (anonymous):

how can i apply the exit code in my case 7: (QUIT)? #confused.

OpenStudy (anonymous):

kris100 Master.RohanChakraborty is not a programmer I think, most of his answers are copy pastes and wrong. He did not even understand the simple problem of yours.I am providing the correct code of you program. I will correct the code ASAP and post. There after I will clarify your doubt.

OpenStudy (anonymous):

Please provide the entire code kris100

OpenStudy (anonymous):

Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply values that can be expressed as an integer, such as the value of a char). The basic format for using switch case is outlined below. The value of the variable given into switch is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point. switch ( <variable> ) { case this-value: Code to execute if <variable> == this-value break; case that-value: Code to execute if <variable> == that-value break; ... default: Code to execute if <variable> does not equal the value following any of the cases break; } The condition of a switch statement is a value. The case says that if it has the value of whatever is after that case then do whatever follows the colon. The break is used to break out of the case statements. Break is a keyword that breaks out of the code block, usually surrounded by braces, which it is in. In this case, break prevents the program from falling through and executing the code in all the other case statements. An important thing to note about the switch statement is that the case values may only be constant integral expressions. Sadly, it isn't legal to use case like this: int a = 10; int b = 10; int c = 20; switch ( a ) { case b: // Code break; case c: // Code break; default: // Code break; } The default case is optional, but it is wise to include it as it handles any unexpected cases. Switch statements serves as a simple way to write long if statements when the requirements are met. Often it can be used to process input from a user. Below is a sample program, in which not all of the proper functions are actually declared, but which shows how one would use switch in a program. #include <iostream> using namespace std; void playgame() { cout << "Play game called"; } void loadgame() { cout << "Load game called"; } void playmultiplayer() { cout << "Play multiplayer game called"; } int main() { int input; cout<<"1. Play game\n"; cout<<"2. Load game\n"; cout<<"3. Play multiplayer\n"; cout<<"4. Exit\n"; cout<<"Selection: "; cin>> input; switch ( input ) { case 1: // Note the colon, not a semicolon playgame(); break; case 2: // Note the colon, not a semicolon loadgame(); break; case 3: // Note the colon, not a semicolon playmultiplayer(); break; case 4: // Note the colon, not a semicolon cout<<"Thank you for playing!\n"; break; default: // Note the colon, not a semicolon cout<<"Error, bad input, quitting\n"; break; } cin.get(); } This program will compile, but cannot be run until the undefined functions are given bodies, but it serves as a model (albeit simple) for processing input. If you do not understand this then try mentally putting in if statements for the case statements. Default simply skips out of the switch case construction and allows the program to terminate naturally. If you do not like that, then you can make a loop around the whole thing to have it wait for valid input. You could easily make a few small functions if you wish to test the code.

OpenStudy (anonymous):

Hi, Jeet.in! I'm sorry for my late response.. i was busy doing my program. i haven't finish reading your comments yet. But i'll try to understand it as much as i can. Anyway, Thank you for your concern for answering my problem. Maybe Tomorrow i hope i could talk to you regarding my problem, i have lot of things to ask about this program. Thank you :) God bless.

OpenStudy (anonymous):

Sure kris100. Happy to help ! Actually I can't tolerate wrong misleading answers to learners. Get in touch anytime you need me.

OpenStudy (anonymous):

oh.. i thought you were off. I finished reading your comments..and im trying to apply it now.

OpenStudy (anonymous):

Sure thing !

OpenStudy (anonymous):

One more thing. is exit code can stop your program from debugging?

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!