Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strcpy was not declared in this scope?

Tags:

c++

strcpy

#include <iostream>
#include <string>
using namespace std;

int main()
{
    char Buffer[20] = {'\0'};

    cout << "Enter a line of text: " << endl;
    string LineEntered;
    getline (cin, LineEntered);

    if ( LineEntered.length() < 20 ){
        strcpy(Buffer, LineEntered.c_str()); // This strcpy. is not declared in scope for some reason.
        cout << "Buffer contains: " << Buffer << endl;
    }

    return 0;
}

This is the error:

main.cpp:14:43: error: 'strcpy' was not declared in this scope

Why is it having this error?

like image 715
Dawry Jones Avatar asked Mar 15 '23 10:03

Dawry Jones


1 Answers

The strcpy function is in the include file string.h. So add:

#include <string.h>

Or, alternatively if you want to be more C++ about it,

#include <cstring>

and use std::strcpy().

like image 113
Greg Hewgill Avatar answered Mar 24 '23 09:03

Greg Hewgill