C ++角色管理

时间:2015-10-19 08:00:16

标签: c++ pointers char

我正在尝试创建一个程序,它将收到一个10位数的密钥,并检查字符是否' c'和' r'在密钥中的特定位置包含它,例如。 :c到位3和r在10中会接受这样的键:** c ****** r,其中*可以是任何字符,或字母或数字。我已经达到了这一点,但我无法理解为什么我会用指针获得错误。我在MSVS EXPRESS 10上使用C ++。感谢。

CREATE OR ALTER VIEW master_with_detail_count
AS
SELECT master_table.id, coalesce(c.detail_count, 0) as detail_count
FROM master_table
LEFT JOIN (SELECT id, count(*) as detail_count FROM detail GROUP BY id) c
    ON c.id = master.id

2 个答案:

答案 0 :(得分:1)

您必须指定填充的char缓冲区 public class Message { private UUID mId; private String mTopic; private String mPayload; public Message() { mId = UUID.randomUUID(); } public UUID getId() { return mId; } public String getTopic() { return mTopic; } public void setTopic(String topic) { mTopic = topic; } public String getPayload() { return mPayload; } public void setPayload(String payload) { mPayload = payload; } } 的大小。为此,您应该将行code替换为char *code = new char[];(静态数组)。大小为11,因为C ++行始终具有行尾char code[11]的符号。

此外,您可以使用'\0'代替std::string。它有助于处理字符串而不必担心缓冲区大小。

答案 1 :(得分:0)

首先你的程序崩溃了,因为你还没有给出你要创建的字符数组的大小

char *code = new char[]

正确的方式是

char *code = new char[11] 

第二件事你应该注意到你在堆上创建内存但你没有删除它。更好的方法是使用std::string

我已使用std :: string

更正了您的代码
    #include <iostream>
    #include "stdafx.h"
    #include <string>
    #include <cstdio>

    using namespace std;

    int main()
    {

       string code; // Using string type

       int flag = 0;
       printf("                !!!!!!!!\n");
       printf("               !!      !!\n");
       printf("              !!        !!\n");
       printf("              !!        !!\n");
       printf("              !!        !!\n");
       printf("            !!!!!!!!!!!!!!!!\n");
       printf("            !!!!!!!!!!!!!!!!               ####\n");
       printf("            !!!!!!!  !!!!!!!   #############  #\n");
       printf("            !!!!!!!  !!!!!!!   # #         ####\n");
       printf("            !!!!!!!  !!!!!!!   # #\n");
       printf("            !!!!!!!!!!!!!!!!\n");
       printf("            !!!!!!!!!!!!!!!!\n");
       printf("\n     This program is key-protected.\n");
       printf("  Please enter the 10-digit key to unlock:  ");
       do {
         std::cin >> code; 
         if(code.length() == 10){
         size_t pos1 = code.find('c'); // If find function couldn't find character it will return string::npos
         size_t pos2 = code.find('r');

         if(pos1 != string::npos)
            std::cout << code[pos1];
        if(pos2 != string::npos)
            std::cout<<code[pos2];
         flag = 1;
       }
       else
         std::cout << "Wrong key. Try again:  ";
       } while(flag == 0);
       printf("\n                !!!!!!!!\n");
       printf("               !!      !!\n");
       printf("              !!        !!\n");
       printf("              !!        !!\n");
       printf("              !!        !!\n");
       printf("              !!\n");
       printf("            !!!!!!!!!!!!!!!!\n");
       printf("            !!!!!!!!!!!!!!!!   ####\n");
       printf("            !!!!!!!#############  #\n");
       printf("            !!!!!!!##!!!!!!!   ####\n");
       printf("            !!!!!!!##!!!!!!!\n");
       printf("            !!!!!!!!!!!!!!!!\n");
       printf("            !!!!!!!!!!!!!!!!\n");
       printf("\n     Congratulations! You made it!");

    }