如果else子句连接到子串使用导致else语句只有用户输入

时间:2013-01-31 21:28:39

标签: c++ if-statement

当我编译代码时,它始终导致最后的else语句,原因不明。我的时间不多了,所以快速的帮助会派上用场。我保证在我在电子课程中提交作业后进行研究。

#include <iostream> 
#include <iomanip> 
#include <conio.h> 
#include <string> 
#include <string.h>
using namespace std;

int main()
{   
    string K; 
    cout << "Sisestage isikukood:\n "; 
    getline(cin, K);
                  /* Küsin kasutajalt isikukoodi*/
    std::string(str2) = K.substr (1,1);  
    std::string(str3) = K.substr (2,2);  
    std::string(str4) = K.substr (4,2); 
    std::string(str5) = K.substr (6,2);  
    std::string(str6) = K.substr (8,2);
    cout << "Isikukoodi I andmed:\n ";

    if (str2 == "1") 
    {                  
        cout << " *mees\n"; 
    } 
    else if (str2 == "2") 
    {                  
        cout << " *naine\n"; 
    } 
    else if (str2 == "3") 
    {                  
        cout << " *mees\n";
    } 
    else if (str2 == "4") 
    {                  
        cout << " *naine\n"; 
    } 
    else if (str2 == "5") 
    {                  
        cout << " *mees\n"; 
    } 
    else if (str2 == "6") 
    {   
        cout << " *naine\n";           
    } 

    if (str2 == "2" || str2 == "1") 
    {                  
         cout << "* sundis " << str3 << "." << str4 << "." << "18" << str5; 
    }
    else if (str2 == "3" || str2 == "4") 
    {                  
         cout << "* sundis " << str3 << "." << str4 << "." << "19" << str5; 
    }
    else if (str2== "5" || str2 == "6") 
    {                  
        cout << "* sundis " << str3 << "." << str4 << "." << "20" << str5; 
    }

    if (str6 == "00") 
    {                  
        cout << " * Kuressaare Haigla\n"; /* Leian haigla, kus on isikukoodi omanik sundinud */
    }  
    else if (str6== "02") 
    {                  
        cout << " * Ida-Tallinna Keskhaigla, Hiiumaa, Keila, Rapla haigla\n";
    }     
    else if (str6 == "22") 
    {                  
        cout << " *  Ida-Viru Keskhaigla\n";
    }  
    else if (str6 == "27") 
    {                  
        cout << " *  Maarjamõisa Kliinikum,Jõgeva Haigla \n";
    }  
    else if (str6 == "37") 
    {                  
        cout << " *  Narva  Haigla \n";
    }     
    else if (str6 == "42") 
    {                  
        cout << " *  Pärnu Haigla \n";
    }    
    else if (str6 == "47") 
    {                  
        cout << " * Pelgulinna Sünnitusmaja , Haapsalu haigla \n";
    }              
    else if (str6 == "49") 
    {                  
        cout << " * Järvamaa haigla \n";
    }  
    else if (str6 == "52") 
    {                  
        cout << " * Rakvere, Tapa haigla \n"; 
    } 
    else if (str6 == "57") 
    {                  
        cout<<" * Valga haigla \n"; 
    } 
    else if  (str6 == "60") 
    {                  
        cout << " * Viljandi haigla \n"; 
    }
    else if  (str6 == "65") 
    {                  
        cout<<" *  Lõuna-Eesti Haigla (Võru), Põlva Haigla \n"; 
    }                       
    else
    {
        /*Tulemus,kui isikukood on sisestatud valesti*/ 
        cout << " Sisestasite isikukoodi " << K << " .Palun proovige uuesti.\n";  
    }  
    getch();
    return 0; 
}

1 个答案:

答案 0 :(得分:2)

您可以执行以下操作:

  static std::map<std::string, std::string>  message1 = /* Initialize once */;
  static std::map<std::string, std::string>  message2 = /* Initialize once */;

  cout << message1[str2] << "\n";
  cout << message2[str2 + ":" + str6] << "\n";