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
4

2 回答 2

1

几个问题:

  • 您过早终止 main() ,多余的大括号就在您的else声明之前。
  • std::string str# == something当语法不正确时,您继续使用, 。试试吧,因为无论如何str# == something它们都是s。std::string
于 2013-01-31T21:01:42.623 回答
0

除了多余的括号之外,不幸的是,这段代码充满了错误。

要修复您的代码,您希望在您的条件下从 `std::string str2 == "1"' 中取出 "std::string"。您正在重新初始化导致编译器出现问题的值。

其次考虑一个 case 语句,因为编译器期望 else if 之后的下一个条件

std::cout << "* sundis " << str3 << "." << str4 << ".";

也导致编译问题。

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

最后,你 getch 没有定义

希望这可以帮助

于 2013-01-31T23:31:15.623 回答