2013-01-31 35 views
0

在我需要實現子字符串知識的工作上,但我遇到了編譯錯誤。如何使用子串查找數字的正確部分?

#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); 
    cout << "Isikukoodi I andmed:\n "; 
    if(std::string str2 == "1") { 
     cout << " *mees\n"; 
    } else if(std::string str2 == "2") { 
     cout << " *naine\n"; 
    } else if(std::string str2 == "3") { 
     cout << " *mees\n"; 
    } else if(std::string str2 == "4") { 
     cout << " *naine\n"; 
    } else if(std::string str2 == "5") { 
     cout << " *mees\n"; 
    } else if(std::string str2 == "6") { 
     cout << " *naine\n"; 
    } 
    std::cout << "* sundis " << str3 << "." << str4 << "."; 
    else if(std::string str2 == "2" || std::string str2 == "1") { 
     cout << "18" << str5 << ; 
    } else if(std::string str2 == "3" || std::string str2 == "4") { 
     cout << "19" << str5 << ; 
    } else if(std::string str2 == "5" || std::string str2 == "6") { 
     cout << "20" << str5 << ; 
    } 


} else { /*Tulemus,kui isikukuud on sisestatud valesti*/ 
    cout << " Sisestasite isikukoodi " << K << " nime valesti.Palun proovige uuesti.\n"; 
} 
getch(); 
return 0; 
} 
C:\programs\c\Untitled1.cpp: In function `int main()': 
C:\programs\c\Untitled1.cpp:19: error: expected primary-expression before "str2" 
C:\programs\c\Untitled1.cpp:19: error: expected `)' before "str2" 
C:\programs\c\Untitled1.cpp:24: error: expected primary-expression before "str2" 
C:\programs\c\Untitled1.cpp:24: error: expected `)' before "str2" 
C:\programs\c\Untitled1.cpp:28: error: expected primary-expression before "str2" 
C:\programs\c\Untitled1.cpp:28: error: expected `)' before "str2" 
C:\programs\c\Untitled1.cpp:32: error: expected primary-expression before "str2" 
C:\programs\c\Untitled1.cpp:32: error: expected `)' before "str2" 
C:\programs\c\Untitled1.cpp:36: error: expected primary-expression before "str2" 
C:\programs\c\Untitled1.cpp:36: error: expected `)' before "str2" 
C:\programs\c\Untitled1.cpp:40: error: expected primary-expression before "str2" 
C:\programs\c\Untitled1.cpp:40: error: expected `)' before "str2" 

C:\programs\c\Untitled1.cpp:45: error: expected primary-expression before "else" 
C:\programs\c\Untitled1.cpp:45: error: expected `;' before "else" 
C:\programs\c\Untitled1.cpp:49: error: expected primary-expression before "else" 
C:\programs\c\Untitled1.cpp:49: error: expected `;' before "else" 
C:\programs\c\Untitled1.cpp:53: error: expected primary-expression before "else" 
C:\programs\c\Untitled1.cpp:53: error: expected `;' before "else" 
C:\programs\c\Untitled1.cpp: At global scope: 
C:\programs\c\Untitled1.cpp:59: error: expected unqualified-id before "else" 
C:\programs\c\Untitled1.cpp:59: error: expected `,' or `;' before "else" 
C:\programs\c\Untitled1.cpp:62: error: expected constructor, destructor, or type conversion before ';' token 
C:\programs\c\Untitled1.cpp:63: error: expected unqualified-id before "return" 
C:\programs\c\Untitled1.cpp:63: error: expected `,' or `;' before "return" 

C:\programs\c\Untitled1.cpp:64: error: expected declaration before '}' token 

Execution terminated 

回答

1

幾個問題:

  • 您終止main()的太早,外來的支柱是你else語句之前。
  • 如果該語法不正確,則繼續使用std::string str# == something。無論如何,請嘗試str# == something,因爲它們是std::string
+0

的http:// v2.cplusplus.com/reference/string/string/substr/這是我基於我的工作,我是否定位substr錯誤或K.substr不是這樣寫的 – wutimbad

+0

@wutimbad不,不介意 - 它似乎你正確地做到了。忽略最後一點。另外兩個是正確的。 – Rapptz

0

在這個令人費解的括號之上,這段代碼不幸的是充滿了錯誤。

修復你的代碼,你想從你的條件中的s​​td :: string str2 ==「1」'中取出「std :: string」。您正在重新初始化導致編譯器出現問題的值。

其次考慮的情況下,語句編譯一個else後預計,未來條件如果是這樣

的std ::法院< < 「* sundis」 < < STR3 < < 「」 < < str4 < <「。」;

也引起編譯問題。

我發現的另一個問題是,你已經晃來晃去 「< <」 所以更改 cout << "18" << str5 << ;cout << "18" << str5 << endl;

最後,您殘培沒有定義

希望這有助於

相關問題