当我尝试编译这个程序时,我收到关于 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