c++ - I'm getting invalid conversion from char to const char error.How to fix this program? -
i'm getting error in few lines , don't know how program work. beginer programmer here. appreciated.
#include <iostream> #include <cstring> using namespace std; char* find_greater(char* a, char b) { char* result; if (strcmp(a, b) > 0) result = a; else result = b; return result; } int main() { char str1[10], str2[10]; str1[] = "zebra"; str2[] = "man"; cout << "greater string :" << find_greater(str1, str2) << endl; return 0; }
a silly typo:
char* find_greater(char* a, char* b) // ^
pay more attention you're doing, , read code.
parse error here:
char str1[10], str2[10]; str1[] = "zebra"; str2[] = "man";
you can't assign string literals arrays and, if could,
str1[]
not thing. special case can initialise (only) that, so:char str1[10] = "zebra", str2[10] = "man";
fixing those, the code compiles , runs (though, explored in other answers, not best approach).
Comments
Post a Comment