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

I am very new to C ++ and I am trying to create this very simple code, but I do not understand why I am getting this error:

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

Here is the code:

// 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;
}
+4
source share
2 answers

Change scanf('%d', &row);to

scanf("%d", &row);

'%d'is a multi-channel literal that has a type int.

"%d", on the other hand, is a string literal that is compatible with const char *, as expected with the first argument scanf.

%d, int ( '%d') const char * ( scanf) , .

+6

, . C, ++. iostream...

#include<iostream>
using namespace std;

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

    }

, ..!

+1

All Articles