当我尝试编译该程序时,遇到关于strcpy第二个参数的错误(包括在代码下面)。老实说,我很困惑如何解决它。很抱歉,我的代码效率不高或看起来很漂亮;我只是一个初中CS学生。

#include "stdafx.h"
#include <iostream>
#include <ctime>
using namespace std;

int main(){


 int r = 0;
 char *article[]={"the", "a", "one", "some", "any"};
 char *noun[]={"boy","girl","dog","town","car"};
 char *verb[]={"drove","jumped","ran","walked","skipped"};
    char *preposition[]={"to","from","over","under","on"};
    char sentence [80];

 srand(time(NULL));
 for(int i=0;i<=20;i++){

    r = (rand()%5);
 strcpy(sentence,*article[r]);
 strcat(sentence," ");
    r = (rand()%5);
 strcat(sentence,*noun[r]);
 strcat(sentence," ");
 r = (rand()%5);
 strcat(sentence,*verb[r]);
 strcat(sentence," ");
 r = (rand()%5);
 strcat(sentence,*preposition[r]);
 strcat(sentence," ");
 r = (rand()%5);
 strcat(sentence,*article[r]);
 strcat(sentence," ");
 r = (rand()%5);
 strcat(sentence,*noun[r]);
 strcat(sentence,".");
 }

 sentence[0]= toupper(sentence[0]);
 cout<<sentence <<endl;


 system("pause");
 return 0;}




1>Compiling...
1>assignment 8.cpp
1>e:\assignment 8\assignment 8\assignment 8.cpp(16) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>e:\assignment 8\assignment 8\assignment 8.cpp(20) : error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *'
1>        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>e:\assignment 8\assignment 8\assignment 8.cpp(23) : error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
1>        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>e:\assignment 8\assignment 8\assignment 8.cpp(26) : error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
1>        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>e:\assignment 8\assignment 8\assignment 8.cpp(29) : error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
1>        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>e:\assignment 8\assignment 8\assignment 8.cpp(32) : error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
1>        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>e:\assignment 8\assignment 8\assignment 8.cpp(35) : error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
1>        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

最佳答案

危险。 strcat()strcpy()是代码癌症的主要起因。使用它们会使您面临各种缓冲区溢出。使用strncat() / strncpy(),或者(甚至更好)只使用std::string,因为您使用的是C ++!

strcat()strcpy()期望它们的参数为字符串。 *article[r]是单个char-article[r]是所需的字符串。因此,删除前导星号。

关于c++ - strcpy及其第二个参数出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4158567/

10-13 06:21