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; }
3 Respuestas
+ 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())
0
Please post the error.
- 1
I got it.
Thank you so much!