|
 |
|
 |
05-16-2005, 03:45 PM
|
#16 (permalink)
|
|
Regular Contributor
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
|
Ya'll are gonna love me for this, but I forgot how to implement the old "if then" statements. The book I have just refers to "if else" statements. I'm begging someone to digitally knock some sense back in me. Ya know, "if X=a then flush or if x=b then use toilet paper... etc..."
|
|
|
05-18-2005, 06:17 PM
|
#17 (permalink)
|
|
Regular Contributor
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
|
Alright, I think I may have figured out the "if then" problem. I looked at some other programs I downloaded and it seems that "if x==a (execute this)".
But they are usually followed by only an else statement, which leads to no multiplicity. Can I list the following:
if x==a (execute test menu)
if x==b (execute edit menu)
if x==c (execute subject menu)
else (display "this is not a valid choice")
Does this make sense?
|
|
|
05-18-2005, 09:16 PM
|
#18 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Cheawick, first use proper syntax if you can, otherwise I may doubt the real underlying problem.
And secondly, why not checking out code on this forum?
Ok, first try to use proper C++ syntax before I can give any answer.
__________________
|
|
|
05-19-2005, 12:23 AM
|
#19 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
|
Quote:
|
Originally Posted by cheawick
if x==a (execute test menu)
if x==b (execute edit menu)
if x==c (execute subject menu)
else (display "this is not a valid choice")
|
A nice swithc() statement comes to mind..
Code:
switch (x)
{
case a:
/* test menu */;
break;
case b:
/* edit menu */;
break;
case c:
/* subject menu */;
break;
default:
/* this is not a valid choice */;
}
if you look through other examples here in the C/C++ section, you'll find varius usage of it.
|
|
|
05-19-2005, 04:19 PM
|
#20 (permalink)
|
|
Regular Contributor
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
|
Sorry ya'll, I don't know why I'm drawing nothing but blanks lately. Perhaps the ten hour days these past three weeks. Sorry about the syntax Valmont, I was in psuedo code mode at the time.
Redhead, can you explain the "switch case" you posted there, or point to a link tutorial. It truely looks more professional and seems rather flexible.
Thanks ya'll.
|
|
|
05-19-2005, 08:33 PM
|
#21 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 24
|
case statements are like a menu.. the old case /esac is still used in BASH and other unix type shells.
in this case...
you need a input of some type.. you have in yoru example x = something
so let assume that X is value entered from a user
so we have something like
Code:
cout << "please enter your input " ;
cin >> x
so this takes care of our input.. so we must check to see what that input means. so we use a switch (x) which starts the loop and break ends the loop
example
Code:
switch (x) {
case 1:
function1();
break;
case 2:
function2();
break;
case 3:
function3();
break;
default:
cout << "you have not entered a correct responce";
break; //this is redundent as it the end but its habbit
}
this would run once through
its that simple..
another example
from http://www.hello-world.com/cpp/switch/switch.php
to see what season you are in.
Code:
//Season
#include <iostream>
using namespace std;
void main(void)
{ int month;
cout<<"Enter the number of the month:";
cin>>month;
switch (month)
{
case 3: case 4: case 5: cout<<"Spring\n"; break;
case 6: case 7: case 8: cout<<"Summer\n"; break;
case 9: case 10: case 11: cout<<"Fall\n"; break;
case 12: case 1: case 2: cout<<"Winter\n"; break;
default: cout<<"Not a valid month\n";
} //switch month
} //main
more sites..
http://www.intap.net/~drw/cpp/cpp04_02.htm
you can also try and google C++ examples switch+
|
|
|
05-20-2005, 01:42 AM
|
#22 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
|
Just what Iccaros said,
In short, switch() is a way to shorten a whole bunch of if()/else statements, it provides a nicer look to the code, as well as an easier understanding.
|
|
|
05-20-2005, 04:06 AM
|
#23 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Just mind that the switch statement accepts only intrinsic types types.
__________________
|
|
|
05-21-2005, 06:30 PM
|
#24 (permalink)
|
|
Regular Contributor
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
|
Awesome, gang! But I'm not sure what "intrinsic types" are. Also, I'm curious about the "break;" at the end of the case code. What it the function of this? Finally, what file must be included for the use of case?
Hopefully I'm not frustrating ya'll as much as I am at myself for forgetting. Funny thing is that I can read and follow most non-graphical code, but why the heck can't I use it.
GAH!!!
|
|
|
05-22-2005, 04:00 AM
|
#25 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
|
Quote:
|
I'm not sure what "intrinsic types" are.
|
Those are types which always have either true/false values or integer values a small example:
Code:
char a_char;
int an_int;
cin >> a_char;
switch(a_char){
case 'a':
/* this will fail, since it's not true/false or integer we're comparing */
cout << "It's an a" << endl;
break;
case (a_char == 'a'):
/* this will work, since the case statement is a true/false statement */
cout << "It's an a" << endl;
break;
}
cin >> an_int;
switch(an_int){
case 1:
/* this will be accepted, since an_int is type integer */
cout << "It's one, numero uno.." << endl;
}
Quote:
|
I'm curious about the "break;" at the end of the case code.
|
It means you break out of the switch, when you reach the break command.
An example:
Code:
switch(some_condition){
case 2:
cout << "we start at two" << endl;
case 3:
cout << "we break at three" << endl;
break;
case 4:
cout << "Given 2 or 3, this is never reached" << endl;
}
If the condition is 2, it will print everything for the two case and three case, if the contidion equals 3, it will only print three, if the condition is 4, it will only print the "never reached"
Theres also a continue keyword, this is commonly used in while loops, where it makes the code skip the rest of the loop, since a switch statement isn't a loop, it will have the same effect here as a break would.
In switch statements you can combine several code segments, if you have more conditions where the same code portions should be used, then just place the break befor the condition, where the code portions shouldn't be reused like:
Code:
switch(condition){
case 1:
case 2:
case 3:
cout << "No matter if one, two or three, this is used" << endl;
break;
case 4:
cout << "This is only used if we have four" << endl;
}
Quote:
|
Finally, what file must be included for the use of case?
|
None, it is of the same type as if/else they don't need any includes.
|
|
|
05-22-2005, 04:31 AM
|
#26 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Quote:
|
...but why the heck can't I use it.
|
Because you don't code enough. Experiance is the real teacher.
__________________
|
|
|
05-23-2005, 05:34 PM
|
#27 (permalink)
|
|
Regular Contributor
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
|
Can't argue that. I think I have a good direction now and some more sense of the mechanics. Thanks ya'll!
|
|
|
05-23-2005, 10:57 PM
|
#28 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 24
|
sorry I have to ask.. RedHead .. why will this fail..
its used all the time..
this is my output....
Code:
#include <iostream>
using namespace std;
int main ()
{
char c;
cout << "enter a/A or b/B" << endl;
cin >> c;
switch (c)
{
case 'a':
case 'A':
cout << "you entered a or A"<< endl;
break;
case 'b':
case 'B':
cout << "You entered b or B" << endl;
break;
default:
cout << "you did not enter a/A or b/B" << endl;
}
return 0;
}
bash-2.05b$ c++ test.cpp
bash-2.05b$ ./a.out
enter a/A or b/B
B
You entered b or B
or am I missunderstanding your answer?
thanks for your time..
|
|
|
05-23-2005, 11:08 PM
|
#29 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Yes it will work. char is an intrinsic type. Don't try it with std::string though. Or char* That's a container.
__________________
|
|
|
05-24-2005, 06:32 AM
|
#30 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 24
|
thanks .,.. I believe I understand now..
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 05:50 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|