switch case

switch case that determines the water consumes

Page 1 of 1

2 Replies - 2775 Views - Last Post: 15 August 2008 - 07:34 AM Rate Topic: -----

#1 macobix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-August 08

switch case

Post icon  Posted 15 August 2008 - 04:12 AM

please teach me what is the exact code or an easy to get ride easily about switch case conditional statement. pls.....
Is This A Good Question/Topic? 0
  • +

Replies To: switch case

#2 Xarver  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 60
  • Joined: 06-August 08

Re: switch case

Posted 15 August 2008 - 07:15 AM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post your code like this: :code:

Once you paste some code, we'll be glad to help. Look up online tutorials and you will learn a lot of C++. Here's a few good ones...

#1 - C++ Tutorial/Reference

#2 - C++ Reference

#3 - Free Ebooks on C++

~ Xarver :)
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: switch case

Posted 15 August 2008 - 07:34 AM

I think you want to know about the switch-case structure.

Well there are lots of resources out there. Try a little Google.

First result I found.

The syntax is easy enough:
switch ( ConditionVariable) {
	case ConditionValue:
		...some code...
	case SomeOtherValue:
		...some more code...
	.
	.
	.
	default:
		...and some more code...
}


Now the restriction is that the variable has to be an "integral numeric type" which mean it must be a boolean (c++), a char, a short int, an int, a long int, etc.. It can not be a string, or a double, or a JPG image -- integral numeric type only!

The other little catch is that switch has something called "fall though" which causes people to use the break statement inside their code blocks all the time. However fall though "is not a bug, its a feature!"

#include <iostream>
using namespace std;

int getANumber();

int main() {
    int fromNumber = getANumber();
    switch (fromNumber) {
        case 10:
            cout << 10 << endl;
        case 9:
            cout << 9 << endl;
        case 8:
            cout << 8 << endl;
        case 7:
            cout << 7 << endl;
        case 6:
            cout << 6 << endl;
        case 5:
            cout << 5 << endl;
        case 4:
            cout << 4 << endl;
        case 3:
            cout << 3 << endl;
        case 2:
            cout << 2 << endl;
        case 1:
            cout << 1 << endl;
            break; //exit switch when we get here
        default:
            cout << "I can't count from that number!" << endl;
    }
   		   

    return 0;
}


int getANumber() {
	int number;
    bool flag = false;
    do {
		cout << "Please enter a number: " ;
		cin >> number;
		if (!cin.good()) {
		    cout << "I SAID A NUMBER!!!" << endl;
		    flag = true;
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(),'\n');
		} else {
            flag = false;
		}
    } while (flag);   
	return number;
}
Play with that program for a bit. If you enter 10 it counts from 10 to 1, if you enter 5 it counts form 5 to 1, if you enter 11 or 20 (or any number greater than 10 or less than 1) it tells you that it can not count form that number.

This works because it starts execution at the first place where the ConditionVariable == ConditionValue, but then it continues to execute each line until it reaches a break (regardless of the other Condition Values which it no longer checks).

So basically your condition values determines where the code starts execution. To stop fall though just put break statements into your code blocks.

This post has been edited by NickDMax: 15 August 2008 - 07:35 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1