C++ 如何在我的代码中使用字符串

C++ 如何在我的代码中使用字符串,c++,c++11,visual-c++,C++,C++11,Visual C++,我试图修复我的代码,它运行平稳,但它只读取元素的第一个字母,所以像U,C,K,N,O。运行得很好,没有问题 但当试图进入时:铁、氯、铀、钯、铑。它只读了第一个字母,给出了错误的答案 所以我尝试不修复它,所以它读取所有元素都没有问题 #include <iostream> #include <Windows.h> int main() { using namespace std; char Index; cout<<"ple

我试图修复我的代码,它运行平稳,但它只读取元素的第一个字母,所以像U,C,K,N,O。运行得很好,没有问题

但当试图进入时:铁、氯、铀、钯、铑。它只读了第一个字母,给出了错误的答案

所以我尝试不修复它,所以它读取所有元素都没有问题

#include <iostream>
#include <Windows.h>


int main()

{

    using namespace std;


    char Index;

    cout<<"please type an Element to see its Atomic number"<<endl;


    cin>>Index;

    switch (Index)
    {

          case 'H':

            cout<<"tAtomic Number is 2"<<endl;

             break;

          case 'Li':

                cout<<"The full name is (Lithium) , The Atomic number is 3"<<endl;

                break;

               case 'Ac':

              cout<<"The full name is ( Actinium )The Atomic number is 89"<<endl;

              break;        


               case 'Al':

              cout<<"The full name is ( Aluminum )The Atomic number is 13"<<endl;

              break;

               case 'Am':

              cout<<"The full name is ( Americium )The Atomic number is 95"<<endl;


              break;

               case 'Sb':

              cout<<"The full name is ( Antimony )The Atomic number is 51"<<endl;

              break;

               case 'Ar':

              cout<<"The full name is ( Argon )The Atomic number is 18"<<endl;

              break;

               case 'As':

              cout<<"The full name is ( Arsenic )The Atomic number is 33"<<endl;

              break;

               case 'At':

              cout<<"The full name is ( Astatine )The Atomic number is 85"<<endl;

              break;

               case 'Ba':

              cout<<"The full name is ( Barium )The Atomic number is 56"<<endl;

              break;

               case 'Bk':

              cout<<"The full name is ( Berkelium )The Atomic number is 97"<<endl;

              break;

               case 'Be':

              cout<<"The full name is ( Beryllium )The Atomic number is 4"<<endl;

              break;

               case 'Bi':

              cout<<"The full name is ( Bismuth )The Atomic number is 83"<<endl;

              break;

               case 'Bh':

              cout<<"The full name is ( Bohrium ) The Atomic number is 107"<<endl;

              break;

               case 'B':

              cout<<"The full name is ( Boron )The Atomic number is 5"<<endl;

              break;

               case 'Br':

              cout<<"The full name is ( Bromine )The Atomic number is 35"<<endl;

              break;

               case 'Cd':

              cout<<"The full name is ( Cadmium  )The Atomic number is 48"<<endl;

              break;

               case 'Ca':


              cout<<"The full name is ( Calcium )The Atomic number is 20"<<endl;

              break;

               case 'Cf':

              cout<<"The full name is ( Californium )The Atomic number is 98"<<endl;

              break;

               case 'C':

              cout<<"The full name is ( Carbon )The Atomic number is 6"<<endl;

              break;

               case 'Ce':

              cout<<"The full name is ( Cerium )The Atomic number is 58"<<endl;

              break;

               case 'Cs':

              cout<<"The full name is ( Cesium )The Atomic number is 55"<<endl;

              break;

               case 'Cl':

              cout<<"The full name is ( Chlorine )The Atomic number is 17"<<endl;

              break;


              case 'Cr':

              cout<<"The full name is ( Chromium )The Atomic number is 24"<<endl;

              break;

              case 'Co':

              cout<<"The full name is ( Cobalt )The Atomic number is 27"<<endl;

              break;

              case 'Cu':

              cout<<"The full name is ( Copper )The Atomic number is 29"<<endl;

              break;

              case 'Cm':

              cout<<"The full name is ( Curium )The Atomic number is 96"<<endl;

              break;

              case 'Ds':

              cout<<"The full name is ( Darmstadtium )The Atomic number is 110"<<endl;

              break;

              case 'Db':

              cout<<"The full name is ( Dubnium )The Atomic number is 105"<<endl;

              break;

              case 'Dy':

              cout<<"The full name is ( Dysprosium )The Atomic number is 66"<<endl;

              break;

              case 'Es':

              cout<<"The full name is ( Einsteinium )The Atomic number is 99"<<endl;

              break;

              case 'Er':

              cout<<"The full name is ( Erbium )The Atomic number is 68"<<endl;

              break;

              case 'Eu':

              cout<<"The full name is ( Europium )The Atomic number is 63"<<endl;


              break;


              case 'Fm':

              cout<<"The full name is ( Fermium )The Atomic number is 100"<<endl;

              break;

              case 'F':

              cout<<"The full name is ( Fluorine )The Atomic number is 9"<<endl;

              break;

              case 'Fr':

              cout<<"The full name is ( Francium )The Atomic number is 87"<<endl;

              break;

              case 'Gd':

              cout<<"The full name is ( Gadolinium )The Atomic number is 64"<<endl;

              break;

              case 'Ga':

              cout<<"The full name is ( Gallium  )The Atomic number is 31"<<endl;

              break;

              case 'Ge':

              cout<<"The full name is ( Germanium )The Atomic number is 32"<<endl;

              break;

              case 'Au':

              cout<<"The full name is ( Gold )The Atomic number is 79"<<endl;

              break;

              case 'Hf':

              cout<<"The full name is ( Hafnium )The Atomic number is 72"<<endl;

              break;

              case 'Hs':

              cout<<"The full name is ( Hassium )The Atomic number is 108"<<endl;

              break;

              case 'He':

              cout<<"The full name is ( Helium )The Atomic number is 2"<<endl;

              break;

              case 'Ho':

              cout<<"The full name is ( Holmium  )The Atomic number is 67"<<endl;

              break;

              case 'In':

              cout<<"The full name is ( Indium )The Atomic number is 49"<<endl;

              break;

              case 'I':

              cout<<"The full name is ( Iodine )The Atomic number is 53"<<endl;

              break;

              case 'Ir':

              cout<<"The full name is ( Iridium  )The Atomic number is 77"<<endl;

              break;

                case 'Fe':

              cout<<"The full name is ( Iron )The Atomic number is 26"<<endl;

              break;

                case 'Kr':

              cout<<"The full name is ( Krypton )The Atomic number is 36"<<endl;

              break;

                case 'La':

              cout<<"The full name is ( Lanthanum )The Atomic number is 57"<<endl;

              break;

                case 'Lr':

              cout<<"The full name is ( Lawrencium )The Atomic number is 103"<<endl;


              break;

                case 'Pb':

              cout<<"The full name is ( Lead )The Atomic number is 82"<<endl;

              break;

                case 'Lu':

              cout<<"The full name is ( Lutetium )The Atomic number is 71"<<endl;

              break;

                case 'Mg':

              cout<<"The full name is ( Magnesium )The Atomic number is 12"<<endl;

              break;

                case 'Mn':

              cout<<"The full name is ( Manganese )The Atomic number is 25"<<endl;

              break;

                case 'Mt':

              cout<<"The full name is ( Meitnerium )The Atomic number is 109"<<endl;


              break;


                case 'Md':

              cout<<"The full name is ( Mendelevium  )The Atomic number is 101"<<endl;

              break;

                case 'Hg':

              cout<<"The full name is ( Mercury )The Atomic number is 80"<<endl;

              break;

                case 'Mo':

              cout<<"The full name is ( Molybdenum )The Atomic number is 42"<<endl;

              break;

                case 'Ne':

              cout<<"The full name is ( Neon )The Atomic number is 77"<<endl;

              break;

                case 'Nd':

              cout<<"The full name is ( Neodymium )The Atomic number is 60"<<endl;

              break;

                case 'Np':

              cout<<"The full name is ( Neptunium )The Atomic number is 93"<<endl;

              break;

                case 'Ni':

              cout<<"The full name is ( Nickel )The Atomic number is 28"<<endl;

              break;

                case 'Nb':

              cout<<"The full name is ( Niobium )The Atomic number is 41"<<endl;


              break;

              case 'N':

              cout<<"The full name is ( Nitrogen )The Atomic number is 7"<<endl;

              break;

              case 'No':

              cout<<"The full name is ( Nobelium )The Atomic number is 102"<<endl;

              break;

              case 'Os':

              cout<<"The full name is ( Osmium  )The Atomic number is 76"<<endl;

              break;

              case 'O':

              cout<<"The full name is ( Oxygen  )The Atomic number is 8"<<endl;


              break;

              case 'Pd':

              cout<<"The full name is ( Palladium )The Atomic number is 46"<<endl;

              break;

              case 'P':

              cout<<"The full name is ( Phosphorus  )The Atomic number is 15"<<endl;

              break;

              case 'Pt':

              cout<<"The full name is ( Platinum )The Atomic number is 78"<<endl;

              break;

              case 'Pu':

              cout<<"The full name is ( Plutonium )The Atomic number is 94"<<endl;

              break;

              case 'Po':

              cout<<"The full name is ( Polonium )The Atomic number is 84"<<endl;

              break;

              case 'K':

              cout<<"The full name is ( Potassium )The Atomic number is 19"<<endl;

              break;

              case 'Pr':

              cout<<"The full name is ( Praseodymium )The Atomic number is 59"<<endl;

              break;

              case 'Pm':

              cout<<"The full name is ( Promethium )The Atomic number is 61"<<endl;

              break;

              case 'Pa':

              cout<<"The full name is ( Protactinium )The Atomic number is 91"<<endl;

              break;

              case 'Ra':

              cout<<"The full name is ( Radium )The Atomic number is 88"<<endl;

              break;

              case 'Rn':

              cout<<"The full name is ( Radon )The Atomic number is 86"<<endl;

              break;

              case 'Re':

              cout<<"The full name is ( Rhenium )The Atomic number is 75"<<endl;

              break;

              case 'Rh':

              cout<<"The full name is ( Rhodium )The Atomic number is 45"<<endl;

              break;

              case 'Rg':

              cout<<"The full name is ( Roentgenium )The Atomic number is 111"<<endl;

              break;

              case 'Rb':

              cout<<"The full name is ( Rubidium )The Atomic number is 37"<<endl;

              break;


              case 'Ru':

              cout<<"The full name is ( Ruthenium )The Atomic number is 44"<<endl;

              break;


              case 'Rf':

              cout<<"The full name is ( Rutherfordium )The Atomic number is 104"<<endl;

              break;

              case 'Sm':

              cout<<"The full name is ( Samarium )The Atomic number is 62"<<endl;

              break;

              case 'Sc':

              cout<<"The full name is ( Scandium )The Atomic number is 21"<<endl;

              break;

              case 'Sg':

              cout<<"The full name is ( Seaborgium )The Atomic number is 106"<<endl;

              break;

              case 'Se':

              cout<<"The full name is ( Selenium )The Atomic number is 34"<<endl;


              break;

              case 'Si':

              cout<<"The full name is ( Silicon  )The Atomic number is 14"<<endl;

              case 'Ag':

              cout<<"The full name is ( Silver )The Atomic number is 47"<<endl;

              break;


              case 'Na':

              cout<<"The full name is ( Sodium )The Atomic number is 11"<<endl;

              break;

              case 'Sr':

              cout<<"The full name is ( Strontium )The Atomic number is 38"<<endl;

              break;

              case 'S':


              cout<<"The full name is ( Sulfur )The Atomic number is 16"<<endl;

              break;

              case 'Ta':

              cout<<"The full name is ( Tantalum )The Atomic number is 73"<<endl;

              break;

              case 'Tc':

              cout<<"The full name is ( Technetium )The Atomic number is 43"<<endl;

              break;

                  case 'Te':


              cout<<"The full name is ( Tellurium  )The Atomic number is 52"<<endl;

              break;

                  case 'Tb':

              cout<<"The full name is ( Terbium )The Atomic number is 65"<<endl;

              break;


                  case 'Tl':

              cout<<"The full name is ( Thallium )The Atomic number is 81"<<endl;


              break;

                  case 'Th':

              cout<<"The full name is ( Thorium )The Atomic number is 90"<<endl;


              break;

                  case 'Tm':

              cout<<"The full name is ( Thulium )The Atomic number is 69"<<endl;

              break;

                  case 'Sn':

              cout<<"The full name is ( Tin )The Atomic number is 50"<<endl;

              break;
                  case 'Ti':

              cout<<"The full name is ( Titanium )The Atomic number is 22"<<endl;

              break;
                  case 'W':

              cout<<"The full name is ( Tungsten )The Atomic number is 74"<<endl;

              break;
                  case 'Uub':

              cout<<"The full name is ( Ununbium )The Atomic number is 112"<<endl;

              break;
                  case 'Uuh':

              cout<<"The full name is ( Ununhexium )The Atomic number is 116"<<endl;

              break;
                  case 'Uuo':

              cout<<"The full name is ( Ununoctium )The Atomic number is 118"<<endl;

              break;
                  case 'Uup':

              cout<<"The full name is ( Ununpentium  )The Atomic number is 115"<<endl;

              break;
                  case 'Uuq':


                      cout<<"The full name is ( Ununquadium  )The Atomic number is 114"<<endl;
              break;



                  case 'Uus':

              cout<<"The full name is ( Ununseptium  )The Atomic number is 117"<<endl;

              break;

                  case 'Uut':

              cout<<"The full name is ( Ununtrium )The Atomic number is 113"<<endl;

              break;

                  case 'Uuu':

              cout<<"The full name is ( Ununium  )The Atomic number is 111"<<endl;

              break;

                  case 'U':

              cout<<"The full name is ( Uranium  )The Atomic number is 92"<<endl;

              break;

                case 'V':

              cout<<"The full name is ( Vanadium )The Atomic number is 23"<<endl;

              break;

                case 'Xe':

              cout<<"The full name is ( Xenon )The Atomic number is 54"<<endl;


              break;

                case 'Yb':


              cout<<"The full name is ( Ytterbium )The Atomic number is 70"<<endl;

              break;

                case 'Y':

              cout<<"The full name is ( Yttrium  )The Atomic number is 39"<<endl;

              break;

                case 'Zn':

              cout<<"The full name is ( Zinc )The Atomic number is 30"<<endl;

              break;
                case 'Zr':

              cout<<"The full name is ( Zirconium )The Atomic number is 40"<<endl;

              break;



                default:

                    cout<<"There is no element with that Symbol !! , please close the program and try again"<<endl;




    return 0;

    Sleep (5000);



 }
}
#包括
#包括
int main()
{
使用名称空间std;
煤焦指数;

cout您可以使用
std::map
,因为开关盒不处理
std::string

const std::map<std::string, std::string> m = {
    {"H", tAtomic Number is 2"},
    {"Li", "The full name is (Lithium) , The Atomic number is 3"},
    {"Ac", "The full name is ( Actinium )The Atomic number is 89"},
    // ...
};
std::cout << "please type an Element to see its Atomic number" << std::endl;
std::string input;
std::cin >> input;

const auto it = m.find(input);
if (it == m.end()) {
    std::cout << "There is no element with that Symbol !!"
        "Please close the program and try again" << std::endl;
} else {
    std::cout << it->second << std::endl;
}
const std::map m={
{“H”,十进制数为2“},
{“Li”,“全名是(锂),原子序数是3”},
{“Ac”,“全名是(锕)原子序数是89”},
// ...
};
std::cout输入;
const auto it=m.find(输入);
如果(it==m.end()){

std::cout-Well它只允许您键入一个字符,因为您将它存储在一个只包含一个字符的
char
中。将其设置为
std::string
。但是
开关将不起作用,因此您必须将其替换为一系列
if…else
。更好的解决方案可能是将所有信息添加到
std::map
,然后根据
std::string
不是您所认为的,具有实现定义值的多字节字符。您会得到(更好)如果您将其简化为a,则回答。可能包括一个或两个工作正常的案例,以及几个不正常的案例。拥有<20行代码将帮助人们重现您的问题,并能够进一步提供帮助。