What's new
  • Welcome to PHCorner forums. Take a moment to sign up and gain unlimited access and extra privileges that guests are not entitled to, such as: All that and more! Registration is quick, simple and absolutely free. Join our community today!

C++ switch case using char type

Joined
Sep 11, 2016
Messages
1,429
Reaction
636
Age
20
anong sample code boss sa switch case gamit ang character type sa c++, ayaw kasi guman saakin, wala kasi akong makita sa google kakailang search nko laging interger gamit nila may char pero ayaw gumana
1610615584464.png
 

Attachments

CEREZO

Elite
okay lang if else..kaso char at integer yan ehh...hahahah kailangan char at integer din input nong iba..😅😂

or integer nalang kaya input mo...hahaha suggestion lang papss..☺️😊
 
1 Comment
C
int or char sana ehh pero binigay saamin double qoute, kung sa string hindi ko naman alam kung papaano
 
kaya kung char isa lang pero may 1A sa switch. tsaka if icompare mo yung char sa integer may gagawin ka pa dun. baka you mean case '2': instead of case 2
 
1 Comment
C
yung sa first part lang po yung problem ko ayaw kasi gumana string sa switch, e double qoute sana yung sa switch
 
pa try nito ts,
Test:
#include <iostream>
#include <map>
using namespace std;
int main(void)
{
    map<string, int> choice_map;
    choice_map.insert({"1A", 1});
    choice_map.insert({"2", 2});
    choice_map.insert({"3", 3});
    choice_map.insert({"4", 4});
    string choice = "1A";
    switch(choice_map[choice]) {
        case 1:
            cout<<"1\n";
            break;
        case 2:
            cout<<"2\n";
            break;
        case 3:
            cout<<"3\n";
            break;
        case 4:
            cout<<"4\n";
            break;
    }
}
 
A
Arcturus
hindi po kami nag focus sa cpp paps, pero gumamit kami isang sem, 2 sem kami nag java.
 
ts kung yan ang form ng code mo, wag ka gumamit ng char datatype ang gamitin mo is string datatype mag include ka ng preprocessor para sa string
 
Comment

PHC-KILATIS12

Honorary Poster
dyan sa program na ginagawa mo hindi na char yung variable na kelangan mo gamitin , String na kase "1A" yung case mo so para mapagana yan need mo lang ideclare yung choice as " String choice; "

then gagana na yan
 
Comment

Online statistics

Members online
570
Guests online
1,777
Total visitors
2,347

Forum statistics

Threads
1,044,461
Messages
17,783,809
Members
1,503,838
Comments
888,783
Attachments
1,331,900
Profile posts
97,091
Top