小编dra*_*sht的帖子

(如何读取箭头键)C编程中的作弊代码(从键盘输入时)

我正在用C进行游戏,当用户输入Konami代码(作弊代码)时,程序应打印出正确的答案。(编辑#3)发现我的程序在每次输入时都不会“读”箭头键,该怎么做?请 见下面我的评论

这是我更新的代码(编辑#2)://尝试konami

#include<stdio.h>
#include<string.h>

main() {

    int c;
    char cheat[] = {24,24,25,25,27,26,27,26,98,97}; //thanks to Vicky for clarifying this
    char guess[100];

    printf("Enter guess: "); 
    scanf("%c", &guess);

    //just checking if the cheat array is right, and yes it is. I'll remove this later
    for ( c = 0; c < 11; c++ ) {
        printf("%c", cheat[c]);
    }


    //comparison of the guess (input) to the cheat code
    if (strcmp (cheat, guess) == 0  )
       printf("\nYou win!");
    else
       printf("\nLol!");

}
Run Code Online (Sandbox Code Playgroud)

现在,我的问题是它总是打印Lol!即使我正确输入了作弊代码。。。

注意:这是针对我的编程入门课程(也是我第一次使用c语言)。到目前为止涵盖的课程是基础知识,循环,数组和字符串(尚无功能,但我现在知道没有结构和其他功能,谢谢:)

c arrow-keys

0
推荐指数
1
解决办法
2万
查看次数

为什么会发生内存泄漏?

我已经隔离了这些行的内存泄漏:

char* word_ptr;
while(read_word(fp, word)) {
    word_ptr = strdup(to_lower_case(word));
    // other stuff happens, loop closes properly, etc.
Run Code Online (Sandbox Code Playgroud)

由于我没有malloc word_ptr,我不需要释放它.如果您需要小写函数,它在这里:

char* to_lower_case(char *string) {
    for (int i = 0; i < strlen(string); i++) {
        string[i] = tolower(string[i]);
    }

    return string;
}
Run Code Online (Sandbox Code Playgroud)

c pointers

0
推荐指数
1
解决办法
73
查看次数

无法理解C代码的输出

#include<stdio.h>

int main()
{
    int a=5;
    printf("%d \n"+1,a);   // output : d
    printf("%%d \n"+1,a);  // output : 5
    printf("%q \n"+1,a);   // output : q
    printf("%%q \n"+1,a);  // output : q
    printf("%k \n"+1,a);   // output : k
    printf("%%k \n"+1,a);  // output : k
    printf("ABCD \n");     // output : ABCD
    printf("ABCD \n" +1);  // output : BCD
    printf("ABCD \n" +3);  // output : D
}
Run Code Online (Sandbox Code Playgroud)

我无法理解上述程序给出的输出.我是新手.如果我必须知道指针中的高级主题?

c

0
推荐指数
1
解决办法
111
查看次数

C:错误在将字符串传递给函数时期望')'

我正在Windows 7上使用Microsoft Visual Studio 2010编译一个小型C程序.这是一个小片段:

void test(char[] s)
{
  //some code here
}
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

期待')'

我该如何解决这个问题?

实际上完整的代码在这里,在C中实现基数排序MSD(本书第10章):

#define N // integers to be sorted with values from 0 -256 

void MSD (char[] s) {
    msd_sort(s, 0, len(s), 0)
}

msd_sort(char [][] s, int lhs, int rhs, int d )
{
    if (rhs<=lhs+1) return; 

    int * count =(int * )malloc(257*sizeof(int));
    for(int i = 0; i < N; ++i)
        count[s[i][d]+1]++;
    for(int k = 1; k < 256; ++k)
        count[k] += …
Run Code Online (Sandbox Code Playgroud)

c

0
推荐指数
1
解决办法
102
查看次数

C中的变量参数在Valgrind中创建错误

我试图运行一个使用函数的程序concat_str.它可以将多个参数作为字符串,参数的结尾表示为"quit".我的功能代码如下:

char *concat_str(char *str1, ...)
{
    va_list pstr;
    char *minion = NULL, *temp = NULL;
    minion = (char*) malloc (sizeof(str1));
    strcpy (minion,str1);
    va_start (pstr, str1);
    if ( strcmp ("quit",str1) == 0)
    {
        va_end (pstr);
        return minion;
    }
    while (1)
    {
        temp = va_arg (pstr, char *);
        if ( strcmp ("quit", temp) == 0)
        {
            break;
        }
        minion = (char*) realloc (minion, sizeof(temp));
        strncat (minion,temp,sizeof(temp));
    }
    va_end (pstr);
    return minion;
}
Run Code Online (Sandbox Code Playgroud)

对此的调用声明将是:

char *result;
result = concat_str("hello", …
Run Code Online (Sandbox Code Playgroud)

c variables valgrind arguments realloc

0
推荐指数
1
解决办法
388
查看次数

输入0后,为什么我的while循环不会中止?

char inputvalue=7;
char zerovalue=0;

while(inputvalue != zerovalue)
{
    scanf (" %c",&inputvalue);
    printf("%c\n",inputvalue);

}
Run Code Online (Sandbox Code Playgroud)

输入0后,为什么我的while循环不会中止?

谢谢您的回答.问题b:我如何解决这个问题,以便按预期工作?

char inputvalue[100]='48';
char zerovalue[100]='0';

while(inputvalue != zerovalue)
{
    scanf (" %c",&inputvalue);
    printf("%c\n",inputvalue);

}
Run Code Online (Sandbox Code Playgroud)

c

0
推荐指数
1
解决办法
96
查看次数

右值不起作用参考

我正在研究引用,我正在尝试一个程序将rvalue传递给函数作为引用参数,就像这样.

#include<iostream>
using namespace std;

int fun(int &x)
{
    return x;
}
int main()
{
    cout << fun(10);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用,当我试图通过左值时,它起作用了.

#include<iostream>
using namespace std;

int fun(int &x)
{
    return x;
}
int main()
{
    int x=10;
    cout << fun(x);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

谁能解释我为什么会这样?

c++ reference rvalue lvalue

0
推荐指数
1
解决办法
51
查看次数

如何计算云中每个点的法线

我的问题是:我有 3D 点云。我想将每个法线归因于每个点。来自 PCL 教程:

// Create the normal estimation class, and pass the input dataset to it
pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> ne;
ne.setInputCloud (cloud.makeShared());

// Create an empty kdtree representation, and pass it to the normal estimation object.
// Its content will be filled inside the object, based on the given input dataset (as no other search surface is given).
pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ> ());
ne.setSearchMethod (tree);

// Output datasets
pcl::PointCloud<pcl::Normal>::Ptr cloud_normals (new pcl::PointCloud<pcl::Normal>);

// Use all neighbors in a sphere …
Run Code Online (Sandbox Code Playgroud)

c++ normals point-clouds point-cloud-library

0
推荐指数
1
解决办法
7478
查看次数

堆溢出还是缓冲区溢出?

这段代码在 memset 上写了一个额外的字符,但为什么呢?

int main(int argc, char ?argv[]) {

    char ?a, ?b;
    a=(char ?)malloc(12);
    b=(char ?)malloc(12);

    if(!a || !b)
        err(2, "malloc error.\\n");

    bzero(a, 12); bzero(b, 12);
    printf("%x %x (%i)\n", a, b, b?a); // b?a is d.
    memset(a, (int)'a', 11);
    memset(b, (int)'b', 11);
    printf("a: %s\n", a);
    printf("b: %s\n", b);
    printf("???\n");
    memset(a, (int)'a', b?a); // Heap?Overflow? Is this happening here ?
    printf("a: %s\n", a);

    free(a);
    free(b);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

脆弱性?如果这是密码,每次打印后都会有一个额外的字符?

c

0
推荐指数
1
解决办法
140
查看次数

不是类,命名空间或枚举?

我的老师给了我一个学习的代码,我无法弄清楚我何时typedef在地图上(正如我在代码中评论的那样)它工作得很好但是当我定义没有typedef它似乎不起作用.如果有人能够善意解释我会很感激!我读了一些关于"循环依赖"的内容,但不确定是否就是这种情况.

int main (){

    map <string, string> ri; // typedef map<string, string> maps;
    //maps ri;
    ri.insert(pair<string, string>{"Smoljan", "Dragan"});
    ri.insert(pair<string, string>{"Smolver", "Tina"});
    ri.insert(pair<string, string>{"Mirkovic", "Sonja"});

    string in;
    cout<<"Input:";
    cin>>in;

    string high(in);
    high.back()++;

    auto low = ri.lower_bound(in);

    /*(maps)*/ ri::key_compare comp;  //<----- here is the error

    //....
}
Run Code Online (Sandbox Code Playgroud)

c++ cyclic-dependency

-1
推荐指数
1
解决办法
4717
查看次数

为什么我的功能打印为'0'?

这是我的代码,我不知道为什么会发生这种情况:

#include <stdio.h>

void foo(float *);

int main()
{
    int i = 10, *p = &i;
    foo(&i);
}

void foo(float *p)
{
    printf("%f\n", *p);
}
Run Code Online (Sandbox Code Playgroud)

输出:

0
Run Code Online (Sandbox Code Playgroud)

c c++ floating-point int format-specifiers

-3
推荐指数
1
解决办法
252
查看次数

我想打印1到10个数字,如果我输入1,然后1到100,如果2,1到1000为3

void main()
{
    int r,y,j;
    clrscr();
    printf("Enter your number choice please:");
    scanf("%d",&y);
    r = pow(10,y);
    printf("the multiplier is: %d",r);
    for(j=1;j<r;j++)
    {
        printf("\n The number is %d",j);
        r=r-1;
    }
    getch();
}
Run Code Online (Sandbox Code Playgroud)

输出是:

Enter your number choice please:1
"The multiplier is:10
 The number is 1
 The number is 2
 The number is 3
 The number is 4
 The number is 5
Run Code Online (Sandbox Code Playgroud)

直到10我才输出输出.逻辑错误是什么?

c

-5
推荐指数
1
解决办法
134
查看次数

C++中的多个构造函数

问题是关于多个让我困惑的构造函数.

#include "complex.h"
#include <iostream>

using namespace std;

Complex::Complex(double realPart, double imPart)
    : m_R(realPart), m_I(imPart)
{
    cout << "complex(" << m_R << "," << m_I << ")" << endl;
}

Complex::Complex(double realPart)
{
    Complex(realPart, 0);
}

Complex::Complex() : m_R(0.0), m_I(0.0)
{
}

int main() {
    Complex C1;
    Complex C2(3.14);
    Complex C3(6.2, 10.23);
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释编译器如何知道使用哪三个构造函数定义?Primer来自本书,第58页.

c++ constructor

-5
推荐指数
2
解决办法
1万
查看次数