如何使用子字符串查找数字行的正确部分?

时间:2013-01-31 20:56:43

标签: c++ substr

处理我需要实现substring知识的赋值,但是我遇到了编译错误。

#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

2 个答案:

答案 0 :(得分:1)

有几个问题:

  • 你太早地终止了main(),在你的else声明之前就没有了。[/ li>
  • 当语法不正确时,您继续使用std::string str# == something。请尝试str# == something,因为他们反正std::string

答案 1 :(得分:0)

在极端支架之上,不幸的是,这段代码充满了错误。

修复你想要在你的条件中从`std :: string str2 ==“1”'取出“std :: string”的代码。您正在重新初始化您的值,这会导致编译器出现问题。

其次考虑一个case语句,因为编译器期望在else之后的下一个条件,如果是这样的话

  

std :: cout&lt;&lt; “* s​​undis”&lt;&lt; str3&lt;&lt; “” &LT;&LT; str4&lt;&lt; “”;

也导致编译问题。

我发现的另一个问题是你有悬空“&lt;&lt;”所以改变 cout << "18" << str5 << ;cout << "18" << str5 << endl;

最后,你没有定义getch

希望这会有所帮助