0

Why does it make an error? Please! Help me correct it?

#include <iostream> #include <stdlib.h> #include <string.h> #include <stdio.h> using namespace std; int main() { char strArr[20]; char numArr[20]; int num; string str; cin >> str; strcpy(strArr, str); // convert string to int num = atoi(strArr); printf(" Your string: %s\n Converted string: %d", strArr, num); return 0; }

16th Jun 2017, 1:58 AM
School Dog
3 Answers
+ 1
strcpy() takes a const char* (C String) as its second argument and a char* as its first. You are trying to give it a string type. You need to convert it to a C string. After doing so your code will compile and run. I'm guessing by the atoi() call that you're expecting to get an int as input. strcpy(strArr, str.c_str()); // convert str (string) to a C string (.c_str())
16th Jun 2017, 6:11 AM
ChaoticDawg
ChaoticDawg - avatar
0
Please post the error.
16th Jun 2017, 4:31 AM
Igor B
Igor B - avatar
- 1
I got it. Thank you so much!
17th Jun 2017, 8:12 AM
School Dog