C++ --- 错误 C2664:'int scanf(const char *,...)':无法将参数 1 从 'int' 转换为 'const char *'

C++ --- error C2664: 'int scanf(const char *,...)' : cannot convert argument 1 from 'int' to 'const char *'

我是 C++ 的新手,我正在尝试构建这个非常简单的代码,但我不明白为什么会出现此错误:

Error   1   error C2664: 'int scanf(const char *,...)' : cannot convert argument 1 from 'int' to 'const char *'

代码如下:

// lab.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h> 

int main(int argc, char* argv[])
{
    int row = 0;
    printf("Please enter the number of rows: ");
    scanf('%d', &row);
    printf("here is why you have entered %d", row);
    return 0;
}

scanf('%d', &row);更改为

scanf("%d", &row);

'%d'int.

类型的多字符文字 另一方面,

"%d" 是一个字符串文字,它与 scanf 第一个参数预期的 const char * 兼容。

如果您传递单引号 %d,编译器将尝试从 int'%d' 的类型)隐式转换为 const char *(如 scanf 所预期的那样) ) 并将失败,因为不存在此类转换。

希望您理解错误 now.And 您是用 C 而不是 C++ 完成此代码的。您需要包含 header iostream ...

#include<iostream>
using namespace std;

    int main()
    {
        int row = 0;
        cout<<"Please enter the number of rows: ";
        cin>>row;
        cout<<"entered value"<<row;

    }

希望这对您有所帮助..!