سؤال

When I try and compile this program, I get errors (included below the code) about strcpy's second argument. I'm honestly stumped on what to do to fix it. And I'm sorry if my code is not efficient or pretty to look at; I'm just a beginning CS student.

#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
هل كانت مفيدة؟

المحلول

DANGER. strcat() AND strcpy() ARE THE LEADING CAUSES OF CODE CANCER. Using them exposes you to all kinds of buffer overflows. Use strncat()/strncpy(), or (even better) just use std::string, since you're using C++!

strcat() and strcpy() expect their arguments to be strings. *article[r] is a single char--article[r] is the string you want. So, drop the leading asterisks.

نصائح أخرى

You have one asterisk too many - noun[r] already gives you a char* so you don't need to put an additional * in the second parameter.

Also, strcat is an unsafe function and can crash your program unexpectedly if your buffer (in your case, sentence) is ever too small for the content.

Please use strncat instead - you'll need to add one more parameter to that function, which is the buffer size - in this case, 80. Then in case of undersized buffer instead of program crash you would just notice that your sentence is clipped at the end.

Your articles, nouns, and verbs are arrays of char pointers. When selecting the item in the array to use, you get a char* to the word to use. This char* is what strcpy expects - when you dereference the char* (i.e. article[r]), you end up with a char, not a char.

Also, strcpy is an unsafe string operator, so it can overwrite large clumps of memory or otherwise open gaping security holes. Is there any reason you're not allowed to use std::string for this assignment?

Too much de-referencing, e.g. change:

strcpy(sentence,*article[r]);

to

strcpy(sentence, article[r]);

and similarly for the other instances.

*article[r] is a value of type char. It's the first character of the string. strcpy expects the address of the string which is simply article[r].

Instead of

 strcpy(sentence,*article[r]);

you want

 strcpy(sentence,article[r]);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top