easy question about switch

This is a discussion on easy question about switch within the C++ Programming forums, part of the General Programming Boards category; Hi! Code: int sss = 2; switch (sss) { case 0: cout<<"0"; break; case 1 || 2: cout<<"1 or 2"; ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    9

    easy question about switch

    Hi!

    Code:
        int sss = 2;
        switch (sss)
        {
           case 0: cout<<"0"; break;
           case 1 || 2: cout<<"1 or 2"; break;
        }
    I don't know how to use multiple condition in case (so that it'll enter second case in case of 1 or 2). I checked || and , and && but I guess no one of them is the proper way to do it.
    Thanks in advance for help

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,605
    Code:
    case 1:
    case 2:
    Unless you put "break", it will fall down to the next one, so you can handle multiple values with multiple cases with no break at the end.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    9
    Thanks!
    I've got other question.
    This is what I can do:
    Code:
    string text = "My text.";
    cout << "Found on  the position: " << text.find("text") << endl;
    But I don't know how to check how many letters 't' there are in the text. In this case there are two of them - I want to obtain number of symbols 't in the string.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can write a loop that inspects each character and increments a counter if the character is 't' (or whatever you want to count).

    Or, if this is not a homework, you might use the count algorithm.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The "easy" solution is to just iterate through the string and check if each character is 't'.

    Another solution would be to sort all the letters, and then use find to locate "t", and then count on from there to see how many they are.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another Embarassingly Easy Question
    By almo89 in forum C Programming
    Replies: 2
    Last Post: 02-11-2006, 03:59 PM
  2. This is hopefully an extremely easy question...
    By rachaelvictoria in forum C Programming
    Replies: 2
    Last Post: 11-07-2005, 12:36 AM
  3. 4 easy question
    By Zeratulsdomain in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2005, 10:43 PM
  4. switch structure question
    By MyntiFresh in forum C++ Programming
    Replies: 4
    Last Post: 06-28-2005, 05:18 PM
  5. Light switch question
    By C_Coder in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 12-05-2001, 06:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21