Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 05-16-2005, 03:45 PM   #16 (permalink)
cheawick
Regular Contributor
 
cheawick's Avatar
 
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
cheawick is on a distinguished road
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..."
cheawick is offline   Reply With Quote
Old 05-18-2005, 06:17 PM   #17 (permalink)
cheawick
Regular Contributor
 
cheawick's Avatar
 
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
cheawick is on a distinguished road
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?
cheawick is offline   Reply With Quote
Old 05-18-2005, 09:16 PM   #18 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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.
__________________
Valmont is offline   Reply With Quote
Old 05-19-2005, 12:23 AM   #19 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 05-19-2005, 04:19 PM   #20 (permalink)
cheawick
Regular Contributor
 
cheawick's Avatar
 
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
cheawick is on a distinguished road
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.
cheawick is offline   Reply With Quote
Old 05-19-2005, 08:33 PM   #21 (permalink)
iccaros
Registered User
 
Join Date: Apr 2005
Posts: 24
iccaros is on a distinguished road
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+
iccaros is offline   Reply With Quote
Old 05-20-2005, 01:42 AM   #22 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 05-20-2005, 04:06 AM   #23 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Just mind that the switch statement accepts only intrinsic types types.
__________________
Valmont is offline   Reply With Quote
Old 05-21-2005, 06:30 PM   #24 (permalink)
cheawick
Regular Contributor
 
cheawick's Avatar
 
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
cheawick is on a distinguished road
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!!!
cheawick is offline   Reply With Quote
Old 05-22-2005, 04:00 AM   #25 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 05-22-2005, 04:31 AM   #26 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
...but why the heck can't I use it.
Because you don't code enough. Experiance is the real teacher.
__________________
Valmont is offline   Reply With Quote
Old 05-23-2005, 05:34 PM   #27 (permalink)
cheawick
Regular Contributor
 
cheawick's Avatar
 
Join Date: Dec 2003
Location: Mary Esther, FL
Posts: 189
cheawick is on a distinguished road
Can't argue that. I think I have a good direction now and some more sense of the mechanics. Thanks ya'll!
cheawick is offline   Reply With Quote
Old 05-23-2005, 10:57 PM   #28 (permalink)
iccaros
Registered User
 
Join Date: Apr 2005
Posts: 24
iccaros is on a distinguished road
sorry I have to ask.. RedHead .. why will this
Code:
case 'a':
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..
iccaros is offline   Reply With Quote
Old 05-23-2005, 11:08 PM   #29 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Yes it will work. char is an intrinsic type. Don't try it with std::string though. Or char* That's a container.
__________________
Valmont is offline   Reply With Quote
Old 05-24-2005, 06:32 AM   #30 (permalink)
iccaros
Registered User
 
Join Date: Apr 2005
Posts: 24
iccaros is on a distinguished road
thanks .,.. I believe I understand now..
iccaros is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
java/jsp test question ideas sde Java 2 03-25-2005 07:38 PM
How to test for empty date from MySql call? Wysocki PHP 3 02-06-2005 02:17 PM
how to test PHP R0B PHP 3 10-27-2004 03:28 PM


All times are GMT -8. The time now is 05:50 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting