seventheyejosh Posted October 10, 2009 Share Posted October 10, 2009 So i seem to be terrible with char and strings... can anyone lend me a hand? #include <iostream> #include <string> using namespace std; char repeatString(char what[10], int times); int main(){ cout << repeatString('Ok',10); return(0); }//end main char repeatString(char what[10], int times){ for(int i=0; i<times; i++){ cout << what << " "; }//end for }//end repeatString should be out putting: "Ok Ok etc"; ten times instead I get: Joshs-Mac-2:week7 MacBook$ g++ mid.cpp -o mid.out mid.cpp:10:23: warning: multi-character character constant mid.cpp: In function ‘int main()’: mid.cpp:10: error: invalid conversion from ‘int’ to ‘char*’ mid.cpp:10: error: initializing argument 1 of ‘char repeatString(char*, int)’ Thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/177207-c-char-help/ Share on other sites More sharing options...
corbin Posted October 10, 2009 Share Posted October 10, 2009 In C++ (and C) ' and " are not the same. '' means a character constant and "" is a string. A string is basically an array of null terminated characters, and a character is a 1 byte chunk of memory. In other words, in hex each thing would look as follows: 'a' 61 "a" 61 0 So, you should be using "Ok". Also, you might want to make it a pointer so you're not limited to 9 characters. (The 10th is the null byte.) Quote Link to comment https://forums.phpfreaks.com/topic/177207-c-char-help/#findComment-934446 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.